This is by no means a complete tutorial and is ment to be short and sweet. The reason for posting this is because during my journey of exploring sockets in C I came across many problems that I would like to help anyone else with who may be haveing the same dificulties.

I am no expert Socket programing as I am just learning sockets, as well I dont claim to be an expert at C. If there are mistakes or things you would like to add, feal free.

A while ago I posted a post "unidentified flying function" and with the help of nebulus200 and SodaMoca5 I was able to solve my problem.


I asume you have a good understanding of the C language and/or c++ and are somewhat familiar with sockets and TCP/IP

First off, Windows uses what is called the Winsock API for implementing sockets. Firstly Sockets in UNIX/Linux may be implemented by first includeing some header files, mainly:

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <netdb.h>
#include <unistd.h>
#include <signal.h>

But since Windows uses Winsock, these will change. To use sockets in windows enviroment we must include:

#include <winsock2.h> OR <winsock.h>
#include <process.h>

io.h may also be included if nessisary.

NOTE: AF_INET is the only family implemented in Winsock.

Now, this will still present compile erors, as we have yet to tell our compiler to include the nessisary librarys,

wsock32.lib or winsock32.lib or ws2_32.dll

Note that ws2_32.lib is for the Winsock V2.x implementation.

Now that we have set all that up, we still have some things to remember. While in *nix we can begin by declareing structors and creating a socket, in Windows we must first initialize the Winsock API.


WSAStartup(wVersionRequested, &wsaData);

where wVersionRequired is a WORD containing the verion of Winsock to be implemented. Example:

wVersionRequested = MAKEWORD( 2, 0 );

where we want to use version 2.0 of Winsock.

&wsaData is the address of our structure containing version information and other relevant information:

struct WSAData {
WORD wVersion;
WORD wHighVersion;
char szDescription[WSADESCRIPTION_LEN+1];
char szSystemStatus[WSASYSSTATUS_LEN+1];
unsigned short iMaxSockets;
unsigned short iMaxUdpDg;
char FAR * lpVendorInfo;


WSAData wsaData;
/*declares a structure of type WSAData, WSAStartup will fill this in */

Now in *nix after we are done with sockets we may simply exit(x)
(of cource closeing the socket first)
with the apropriate error level,

In Windows since we Initialized Winsock API we must explicitly close it:


So there underline the major difernces,

More difernces can be found at

The remainder difernces lay in the data types used in functions, and the Data types themselfs.