General Purpose Winsock Utility Functions  

These functions are convenient wrappers for commonly used WinSock APIs, as well as standard Windows network APIs.  They are used by all the WinSock tutorials.  There are 6 of these functions.  They are: 

 int           InitSockets_1_1(); 
 void          RemoveSockets(); 
 char*         GetHostName(char* buf, int len); 
 unsigned int  uGetLocalIP(); 
 unsigned int  uGetIPFromSockAddr_In(sockaddr_in* pSockAddr); 
 char*         IPString(unsigned int addr, char* buf, int len); 


InitSockets_1_1 

This functions initializes the WinSock APIs to allow the program to make WinSock function calls.  The key line in this function is the call to WSAStartup().  This must be called before any other WinSock APIs.  This particular function tries to initialize WinSock version 1.1 or later. 

int CVWSocket::InitSockets_1_1()  
{ 
    WORD     wVersionRequested = MAKEWORD(1, 1);  
    WSADATA  wsaData;  
    int      res;  
  
    res = WSAStartup(wVersionRequested, &wsaData);  
//-- If WSAStartup fails, abort 
    if (res != 0)  
        return 0;  
//-- Confirm that the Windows Sockets DLL supports 1.1. 
//-- Although the DLL may support higher versions, wVersion will hold 1.1 
//-- because that's the one we requested.  
    if (LOBYTE(wsaData.wVersion)!= 1 || HIBYTE(wsaData.wVersion)!= 1)  
    {  
    //-- Version 1.1 is not supported, abort 
        WSACleanup();  
        return 0;  
    }  
//-- All's good 
    return 1; 
} 


RemoveSockets 

This function uninitializes the WinSock APIs.  It should be called after the last WinSock function call in your program, usually as the application is exiting.  No WinSock calls can be made after this one. 

void CVWSocket::RemoveSockets()  
{ 
    WSACleanup();  
} 


GetHostName 

This function returns the network name of the machine that the program is running on.  This is not the IP address.  It is the Windows name that would appear in such places as Network Neighborhood. 

char* CVWSocket::GetHostName(char* buf, int len)  
 
    if (gethostname(buf, len) != 0)  
        return NULL;  
    return buf;  
 


uGetLocalIP 

This function returns the IP address of the local host in the form of a 4 byte unsigned integer.  The returned integer is in network byte order so it can be passed directly to other WinSock APIs without further conversion.  Unsigned integer is the standard format used for IP addresses in the WinSock Programming tutorials. 

unsigned int CVWSocket::uGetLocalIP() 
{ 
    char      szHostName[255]; 
    hostent*  HostData; 
  
    GetHostName(szHostName, 255); 
    HostData = gethostbyname(szHostName); 
    if (HostData == NULL) 
        return NULL; 

    return *((unsigned int*)HostData->h_addr); 
} 


uGetIPFromSockAddr_In 

This function extracts the IP address from a sockaddr_in structure.  This is useful because TCP/IP WinSock API's use this structure when recieving information from remote hosts.  This function provides an easy way of determining where the message came from.  Once again, the IP address is returned in unsigned integer format. 

unsigned int CVWSocket::uGetIPFromSockAddr_In(sockaddr_in* pSockAddr) 
{ 
    return (unsigned int)pSockAddr->sin_addr.S_un.S_addr; 
} 


IPString 

This function provides a convenient way to convert an IP address from unsigned integer format to a dotted-decimal format.  It is used anytime you want to show an IP address to the user. 

char* CVWSocket::IPString(unsigned int addr, char* buf, int len) 
{ 
    char* p = (char*)&addr; 

    if (len < 16) 
        return NULL; 
  
    memset(buf, 0, len); 
    sprintf(buf, "%d.%d.%d.%d", (unsigned char)*(p+0), (unsigned char)*(p+1),  
                                (unsigned char)*(p+2), (unsigned char)*(p+3));  

    return buf; 
} 



Back to Winsock Tutorials Main Page