Linux Socket Programming Notes

Invalid parameter on accept() calls

In RedHat 7.3, the socket accept() call could be called with an undefined length parameter (socklen_t):

 int socket;
 struct sockaddr_in connectaddr;
 socklen_t connectaddrlen;
 socket = accept( server_socket, (struct sockaddr *) &connectaddr, &connectaddrlen );

In Redhat Enterprise Linux ES3, however, the above code fragment causes an invalid parameter error. To fix the problem, all you need to do is define socklen_t as the initial length of the sockaddr_in.

 int socket;
 struct sockaddr_in connectaddr;
 socklen_t connectaddrlen = sizeof(connectaddr);
 socket = accept( server_socket, (struct sockaddr *) &connectaddr, &connectaddrlen );

The man page makes this pretty clear if you are patient enough to read it. Here's the excerpt:

The addrlen argument is a value-result parameter: it should initially contain the size of the structure pointed to by addr; on return it will contain the actual length (in bytes) of the address returned. When addr is NULL nothing is filled in.