CyberArmy University | Open Source Institute | CyberArmy Intelligence & Security | CyberArmy Services & Projects

[Library Index]

[View category: Programming] [Discuss Article]

Introduction to WinSock

Article is yet to be rated
Author:      Rae
Submitted:      28-Apr-2007 19:41:03
Imported From:      The CyberArmy University (original author: rae)


Introduction to WinSock
Most of the network programming done on Win32 platform is done using the WinSock API. API stands for Application Programming Interface. WinSock is actually a short form for Windows Sockets. They are conceptually similar to Berkeley (BSD) Sockets, which are commonly used on the UNIX platform. It is important for the beginner to realise that WinSock is actually just an interface and not a protocol by itself. A socket is conceptually similar to a file descriptor, but has some minor differences. By definition, it is a handle to a transport provider. This article assumes you have intermediate knowledge of networking concepts and C/C++ programming.

Every Windows system has its own version of the WinSock DLL. I will assume you are using version 2.2 of the WinSock API, since it is the one found in Windows 98 and 2000. To start using WinSock functions, you must load the WinSock API library. This is done using the following code,
int WSAStartup(

    WORD  wVersionRequested,

    LPWSADATA  lpWSAData

);
The initialisation is done by using by calling the 'WSAStartup' function. 'wVersionRequested' is used to specify the version of the WinSock you are going to use. If the library is not loaded, a 'SOCKET_ERROR' will be generated, and the details would contain 'WSANOTINITIALISED'.

The next step in making a WinSock application is to bind a socket of the given protocol. This is done using the 'bind' API call. The code for this is,
int bind(

     SOCKET s, 

     const struct sockaddr FAR* name, 

     int namelen

);
Now we have to understand how this particular function is used. Here is an example to help you understand the code better, using port 1000.
 SOCKET s; 
   
struct sockaddr_in   tcpaddr;

int port = 1000; [/quote]

[quote] s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
[/quote]
 
[quote] tcpaddr.sin_family = AF_INET;

tcpaddr.sin_port = htons(port);
    
tcpaddr.sin_addr.s_addr = htonl(INADDR_ANY);

bind(s, (SOCKADDR *)&tcpaddr, sizeof(tcpaddr));
Here we are using the TCP protocol, since it is a connection oriented protocol, unlike UDP, which is connectionless. The 'bind' function call establishes an association between the socket and the port, which in this case is 1000. You may encounter some errors while implementing this code. Most probably it will be the 'WSAEADDRINUSE' error. This means that another process or application is already using that port. Further coding is based upon the decision, whether the program you are making, is a server program or a client program.
References :-

Network Programming for Microsoft Windows by Anthony Jones and Jim Ohlund (Microsoft Press)

www.codeguru.com

www.programmersheaven.com

<HR>
Written by Rae (03, August 2005)

Rae is Member of zZine Media Group (#965)


This article was originally published by CyberArmy.net in the CyberArmy Library.

You must be logged in to vote on an article

About Us | Privacy Policy | Mission Statement | Help