Geeks With Blogs
Mike Flasko's Blog 38% less carbs than a regular blog

Winsock is the socket-based API on the Windows platform.  Winsock (on Windows) provides more functionality than a standard BSD socket interface and also has additional MS only functions above what is defined in the WinSock spec (WinSock is not a strictly MS thing).  Maybe in a future post I will talk about how Winsock is really just a layer on top of a much more general API allowing Windows to easily move to the next API of the decade.  Ok lets get back on track and start talking about IP independent client apps.

When writing a client today we must be thinking of both IPv4 and IPv6 as a possible network layer.  Yes, I know v6 has been “just around the corner” for quite some time now, but we are finally seeing signs it is starting to take real flight.  For example, Longhorn will choose to use a IPv6 address over an IPv4 one if both are available.

The following is a list of DOs and DO NOTs when writing IP independent apps:

  1. Avoid making reference to an address family anywhere in your code (AF_INET, AF_INET6)
  2. Do not allocate protocol specific structures (SOCKADDR_IN, SOCKADDR_IN6), but instead use SOCKADDR_STORAGE, which is as large as the largest protocol specific address structure and provides padding for 64-bit alignment issues
  3. Avoid hard-coded addresses.  Winsock provides constants for any that you will need (Loopback, wildcard for binding, etc)
  4. When passing address information around in your functions, pass the entire address structure (SOCKADDR) and not just the protocol specific one. 
  5.  Avoid the legacy IPv4 only name resolution API calls and use getaddrinfo and getnameinfo instead as they work with both v4 and v6.

The following is a small piece of sample code demonstrating an IP independent client:


struct addrinfo hints, *result=NULL;                
char *remoteAddr, *portNum;
SOCKADDR dest;
int res;

// set remoteAddr and portNum to some values via user input, config file, etc
// remoteAddr = "foobar.com"  portNum="5000";

memset(&hints,0,sizeof(hints));
hints.ai_flags = AI_CANONNAME;
hints.ai_family = AF_UNSPEC;    // resolution could return v4 or v6 address
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;

res = getaddrinfo(remoteAddr, portNum,&hints,&result);
if(res == WSANO_DATA){
  //resolution failed - return error
}

s = SOCKET(result->ai_family, result->socktype, result->ai_protocol);
if(s == INVALID_SOCKET){
   //failure when creating the socket - return error
}

res = connect(s, result->ai_addr, result->ai_addrlen);
if(res == SOCKET_ERROR){
   //error connecting to foobar.com - return error
}

//must be freed because it is dynamically allocated by the getaddrinfo call
freeaddrinfo(result);  

// you are connected ....send and receive data 

//graceful TCP socket closure
shutdown(s, SD_BOTH);
closesocket(s);
Posted on Monday, May 2, 2005 12:20 PM Programming , Networking | Back to top


Comments on this post: Writing an IP Independent Client using Winsock

# re: Writing an IP Independent Client using Winsock
Requesting Gravatar...
Thanks! This was exactly what I needed to know.
Left by Bart on Jul 23, 2005 5:17 AM

Your comment:
 (will show your gravatar)


Copyright © Mike Flasko | Powered by: GeeksWithBlogs.net