socket bind convert ipv4 to support both ipv4 and ipv6

Posted on 2013-10-01
Medium Priority
Last Modified: 2013-10-02
What is the difference between these two bind methods?
1. In the first one I am setting sin_addr to 0 and the application was accepting IPv4 only.
2. in the second one I am using getaddrinfo as I need to start listening on IPv6 also.

Case 1 is older code and I am not sure why the sin_addr was set to 0 and not the actual ip address of server. I need to convert to support ipv6 also but not sure if i convert to getaddrinfo, i would break something?

/************ METHOD 1 **********/
int fd;
       int sock_type =  SOCK_STREAM;

        if ((fd = socket(AF_INET, sock_type, IPPROTO_TCP)) < 0) {
              // ERROR 

        int port  = 5000;
        struct sockaddr_in sin;
        memset((char *)&sin, 0, sizeof (sin));
        sin.sin_family = AF_INET;
        sin.sin_addr.s_addr = 0;  // SET to ZERO
        sin.sin_port = htons(port);

        while (bind(fd, (struct sockaddr *)&sin, sizeof(sin)) < 0)
                // sleep for fews and retry and succeed

/********************* METHOD 2 ***************/
	struct addrinfo hints, *start, *results;
	bzero(&hints, sizeof(struct addrinfo));
	hints.ai_flags = AI_PASSIVE; // Use address return by getaddrinfo in bind() return addrinfo will have INADDR_ANY set to IPv4 adress and INADDR6_ANY_INIT for IPv6 address
	hints.ai_family = AF_UNSPEC;  // return both the family AF_INET and AF_INET6 address structures
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_protocol = IPPROTO_TCP;

        getaddrinfo(NULL, "5000", &hints, &results));

start = results;
	do {
		if (results->ai_family == AF_INET || results->ai_family == AF_INET6 ) {
                      int  fd = socket(results->ai_family, results->ai_socktype, results->ai_protocol)

                      while (bind(fd, results->ai_addr, results->ai_addrlen)  < 0) {
                          // sleep for fews and retry and succeed
         } while ( (results=results->ai_next) != NULL);

Open in new window

Question by:perlperl
1 Comment
LVL 35

Accepted Solution

sarabande earned 2000 total points
ID: 39539653
method 1 returns only ipv4 cause AF_INET is ipv4 family.

the s_addr == 0 means ADDR_ANY as answered by mccarl in your previous question. so all clients of the given family could connect.

to support ipv6 you need to specify a family that includes ipv6. that was done with 2nd method and therefore it worked.


Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question