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

[Programming] Introduction to WinSock


[Reply] [View by Thread] [Help]
[Back To Article Discussion Forum]

Posted by Author Rae On 2007-04-29 10:02:32




View and vote on the article here: Introduction to WinSock


Introduction to WinSock

Category
Programming
Summary
Body
<CENTER><H3>Introduction to WinSock</H3></CENTER><HR>
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.


<CITE>References :-


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


www.codeguru.com


www.programmersheaven.com
</CITE>

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

Rae is Member of zZine Media Group (#965)


This article was imported from the CyberArmy University site. (original author: rae)


There are no replies to this post yet.



Guest:
Subject:
Message:
Signature:
Optional Image Link:
http://

CyberArmy::Forum v0.6
Generated In 0.00537 seconds


About Us | Privacy Policy | Mission Statement | Help