Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8051679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:21:13+00:00 2026-06-05T07:21:13+00:00

Ok – so I’m trying to get a welcome socket running for IPv6 with

  • 0

Ok – so I’m trying to get a welcome socket running for IPv6 with C++.
The problem is that the port it listens on seems to change every time I start the program.

#include <iostream>
#include <stdlib.h>
#include <string>
#include <string.h>
#include <arpa/inet.h>

int main(int argc, char *argv[]){
  //Setting up the serverAddr:
  struct sockaddr_in6 serverAddr;
  memset((char *)&serverAddr, 0, sizeof(sockaddr_in6));
  serverAddr.sin6_family  = AF_INET6;     // IP6 is best current practise.
  serverAddr.sin6_addr    = in6addr_any;  // Don't care for interfaces.
  serverAddr.sin6_port    = htons(7979);  // FIXME change port later.
  //Opening the welcomeSocket:
  std::cout << "Opening socket:\t";
  int welcomeSocket = socket(AF_INET6, SOCK_STREAM, 0);
  if(welcomeSocket < 0){
    std::cout << "[FAIL]" << std::endl;
    return 1;
  }else{
    std::cout << "[ OK ]" << std::endl;
  }
  //Listening for clients:
  while(welcomeSocket >= 0){
    std::cout << "Listening for connections…" << std::endl;
    listen(welcomeSocket, 1);
    struct sockaddr_in6 clientAddr;
    memset((char *)&clientAddr, 0, sizeof(sockaddr_in6));
    int clientAddrLength = sizeof(clientAddr);
    int clientSocket = accept(welcomeSocket, (struct sockaddr*) &clientAddr, (socklen_t*) &clientAddrLength);
    char caddr[INET6_ADDRSTRLEN]; //caddr is not lisp here.
    inet_ntop(AF_INET6, &clientAddr, caddr, INET6_ADDRSTRLEN);
    std::cout << "Request from '" << caddr << "'" << std::endl;
    shutdown(clientSocket, SHUT_RDWR);
    clientSocket = -1;
    shutdown(welcomeSocket, SHUT_RDWR);
    welcomeSocket = -1;
  }
  return 0;
}

I compile the code via g++ server.cpp -o server, and when I run ./server I get the following output:

$ ./server 
Opening socket: [ OK ]
Listening for connections…
Request from 'a00:c158::'

To test the code I run nmap:

$ nmap -6 -p 1-65535 ::1

Starting Nmap 5.21 ( http://nmap.org ) at 2012-06-09 16:43 CEST
Nmap scan report for ip6-localhost (::1)
Host is up (0.00023s latency).
Not shown: 65532 closed ports
PORT      STATE SERVICE
22/tcp    open  ssh
631/tcp   open  ipp
56160/tcp open  unknown

Nmap done: 1 IP address (1 host up) scanned in 1.41 seconds

I think I’m overlooking something simple.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T07:21:14+00:00Added an answer on June 5, 2026 at 7:21 am

    Yes, I think you are too. Before you can use a socket for anything, there are a few other calls you have to make. If you want to be a server, you have to call bind() to set the local address, and listen() to inform the OS that this is a server. Then you can use accept. It’s a bit of a pain sometimes.

    • bind(2) man page
    • listen(2) man page
    • A great guide from Beej.us on Linux/Unix/Winsock programing. It gives great examples, and comes in a fully categorized PDF.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
Have a webpage that will be viewed by mainly IE users, so CSS3 is
I'm trying to craft a Java regular expression to split strings of the general
Welcome to Spring Roo. For assistance press CTRL+SPACE or type hint then hit ENTER.
I have a simple restful service that transforms a JAXB-anntotated beans to response XML
My NSXMLParser breaks on this string: <title>AAA &#8211; BCDEFGQWERTYUIO</title> I parsed it in this
I need a function that will clean a strings' special characters. I do NOT
I'm having troubles with my meta tags with Open Graph. It seems as though
I have a PHP application that stores PHP datetime to my MySQL table. I
I am getting notice on line 30 that twetout is undefined variable <?php $username

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.