xbdev - software development
Wednesday February 21, 2024
home | contact | Support

WinSock At A Glance

(author: bkenwright@xbdev.net)



As far as this document goes, I thought I'd put together a quick read doc, you can glance through this in a few seconds...and either get a feel for the Winsock or refresh your memory.


Header File and Import Library


To use any of the WinSock API's, you are required to include the header file 'winsock2.h' and link the file 'ws2_32.lib' to the final exectuable.


#include <windows.h>

#include <winsock2.h>


WinSock: Initialization


Well before you can use WinSock, you have to initialize it, it can't get any more simple than this.  Simply call WSAStartup to startup the interface to WinSock.



WORD version;

int error;


version = MAKEWORD{ 2, 0};  // e.g. 0x02000


error = WSAStartup( version, &wsa );


/* check for error */

if( error != 0 )


      /* error occured */

      return false;



/* check for the correct version */

if(  LOBYTE( wsa.wVersion ) != 2  ||  HIBYTE( wsa.wVersion ) != 0 )


     /* incorrect WinSock version */


     return false;



/* WinSock has been initialized */



Server: Create Socket


SOCKET server;

server = socket ( AF_INET, SOCKET_STREAM, 0 );


Server: Starting Server


struct sockaddr_in sin;


memset( &sin, 0, sizeof(sin) );


sin.sin_family = AF_INET;

sin.sin_addr.s_addr = INADDR_ANY;

sin.sin_port = htons( 21 );


if ( bind( server, &sin, sizeof(sin) = = SOCKET_ERROR )


    /* could not start server */

    return false;




Server: Listen for Client


while(   listen( server, SOMAXCONN ) = = SOCK_ERROR  );



SOMAXCONN (#defined as 5) - is the backlog, so if you set this to 1, instead of saying SOMAXCONN, you can only have one connection, and the other connections are just ignored.  SOMAXCONN let the system decide the maximum number.



Server: Accepting Connection


SOCKET client;

int length;


length = sizeof( sin );

client = accept( server, &sin, &length );



Client: Create Socket


SOCKET client;


client = socket ( AF_INET, SOCK_STREAM, 0 );



Client: Get Host

Well as a client, for example a web browser, we dont' remember IP's what we do remember is host address'es....for example, www.xbdev.com  now using this URL we can get our IP number.  We do this magic, by using the function gethostbyname


struct hostent host;


host = gethostbyname ( "www.xbdev.net" );


Client: Connecting to Server


struct sockaddr_in sin;


memset( &sin, 0, sizeof(sin) );


sin.sin_family = AF_INET;

sin.sin_addr.s_addr = ((struct in_addr* ) (host->h_addr)) ->s_addr;

sin.sin_port = htons( 21 );


if ( connect( client, &sin sizeof(sin) ) = = SOCKET_ERROR )  // SOCKET_ERROR defined as -1


    /* could not connect to server */

    return false;




Send and Receive Data


Once you've connected your sockets...its just a matter of sending the data...think of it as connecting a tube between two points.  Once you've connected them, you can push water down your tube...hehe...I mean send data.

The functions for sending and receiving data are 'send' and 'recv'


int send( SOCKET s, const char FAR *buf, int len, 0 );

int recv( SOCKET s, char FAR *buf, int len, 0 );



char buf[10] = "test";

send( client, buf, strlen(buf), 0 );


Closing Socket


When we are finished with a socket, we can close it simply by calling the fucntion closesocket.  Easy ;)


closesocket( server );



WinSock: Shutdown


Once finished with the WinSock system, we need to shut it down by calling the function WSACleanup.









Advert (Support Website)

Copyright (c) 2002-2024 xbdev.net - All rights reserved.
Designated articles, tutorials and software are the property of their respective owners.