Porting Unix Socket code to Windows

Windows sockets and UNIX type berkeley sockets provide pretty much similar interface which eases the porting effort. Apart from regular network system calls such as socket(), bind(), listen(), accept(),etc two API calls are important in Windows sockets

WSAStartup()-> should be called before calling any other winsock APIs

WSACleanup()-> should be called when program exit

After a socket connection is established, data can be transferred using the standard read() and write() calls as in UNIX sockets. Sockets must be closed by using the closesocket() function in Windows instead of close() in case of Windows.

Header File:

winsock2.h

Library:

Ws2_32.lib

Alternatively, you can add below line to header file

#pragma comment(lib, “Ws2_32.lib”);

More information on this topic can be found here @ msdn

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s