Docsity

Establishing a Network Connection Using Winsock API: A Step-by-Step Guide, Lecture notes of Data Communication Systems and Computer Networks

Toggle sidebar
bg1
Data Communication
Establishing a Network Connection Using Winsock API
1. Include the following header files
Windows.h, winsock.h, stdio.h
2. Initializing WinSock
The first step in using the WinSock API is to initialize WSA. I'm not positive what WSA is, I'm assumng
its short for WinSockApiThis function takes two parameters a version number in a WORD value and a
WSADATA structure, it returns an integer the return will be 0 if initialization is successful. Here is an example
of the initialization process:
int result;
WSADATA WsaDat;
result=WSAStartup(MAKEWORD(1, 1), &WsaDat) ;
if (result!= 0)
cout<<"WSA Initialization failed";
3. Creating Sockets
After you have initialized WinSock the next step is to create a socket. Sockets are of two types stream sockets
and datagram sockets. Stream sockets are easier to use so I'll demonstrate them. All sockets are of type
SOCKET, and you create them with the socket() function. The socket() function takes three parameters. The
first is the type of connection you would like to use, for this use AF_INET this designates you want to use an
Internet style connection (or in other words use TCP/IP) as far as I know this is the only connection permitted
through WinSock. The second parameter is the type of socket to use, for stream sockets use SOCK_STREAM,
or for datagram sockets use SOCK_DGRAM. The thrid parameter is some value for the protocol from what I
have read this value has very little meaning and is usually ignored so I always pass zero here. The socket()
function will return the socket or INVALID_SOCKET if it can't create the socket. Here is an example of that:
SOCKET Socket;
Socket = socket(AF_INET, SOCK_STREAM, 0);
if (Socket == INVALID_SOCKET)
cout<<"Socket creation failed";
4. Binding Socket
First we bind the socket to a TCP/IP port. This is done with the bind() function. The bind() function takes three
parameters, a socket to bind to, a pointer to a data structure that has the port information (structure type
STRUCTADDR), and the size of the structure with the port information. There are a few points of interest in
this process so i'll just explain inside an example.
//declaring SOCKETADDR instance
SOCKADDR_IN SockAddr;
//We want to use port 50
SockAddr.sin_port = 50;
//We want an internet type connection (TCP/IP)
SockAddr.sin_family = AF_INET;
//We want to listen on IP address 127.0.0.1
//I'll give a few better ways to set thi // s value later
SockAddr.sin_addr.S_un.S_un_b.s_b1 = 127;
SockAddr.sin_addr.S_un.S_un_b.s_b2 = 0;
Note: Variable of type SOCKETADDR_IN is created
instead of SOCKETADDR, the reason is that struct
SOCKADD_IN holds the same information in the same
way as struct SOCKADDR does, the difference is that
struct SOCKADDR_IN is easier to work with.
docsity.com
pf3

Partial preview of the text

Download Establishing a Network Connection Using Winsock API: A Step-by-Step Guide and more Lecture notes Data Communication Systems and Computer Networks in PDF only on Docsity!

Data Communication

Establishing a Network Connection Using Winsock API

1. Include the following header files

Windows.h, winsock.h, stdio.h

2. Initializing WinSock

The first step in using the WinSock API is to initialize WSA. I'm not positive what WSA is, I'm assumng

its short for WinSockApiThis function takes two parameters a version number in a WORD value and a

WSADATA structure, it returns an integer the return will be 0 if initialization is successful. Here is an example

of the initialization process:

int result; WSADATA WsaDat; result=WSAStartup(MAKEWORD(1, 1), &WsaDat) ;

if (result!= 0) cout<<"WSA Initialization failed";

3. Creating Sockets

After you have initialized WinSock the next step is to create a socket. Sockets are of two types stream sockets

and datagram sockets. Stream sockets are easier to use so I'll demonstrate them. All sockets are of type

SOCKET, and you create them with the socket() function. The socket() function takes three parameters. The

first is the type of connection you would like to use, for this use AF_INET this designates you want to use an

Internet style connection (or in other words use TCP/IP) as far as I know this is the only connection permitted

through WinSock. The second parameter is the type of socket to use, for stream sockets use SOCK_STREAM,

or for datagram sockets use SOCK_DGRAM. The thrid parameter is some value for the protocol from what I

have read this value has very little meaning and is usually ignored so I always pass zero here. The socket()

function will return the socket or INVALID_SOCKET if it can't create the socket. Here is an example of that:

SOCKET Socket; Socket = socket(AF_INET, SOCK_STREAM, 0);

if (Socket == INVALID_SOCKET) cout<<"Socket creation failed";

4. Binding Socket

First we bind the socket to a TCP/IP port. This is done with the bind() function. The bind() function takes three

parameters, a socket to bind to, a pointer to a data structure that has the port information (structure type

STRUCTADDR), and the size of the structure with the port information. There are a few points of interest in

this process so i'll just explain inside an example.

//declaring SOCKETADDR instance SOCKADDR_IN SockAddr;

//We want to use port 50 SockAddr.sin_port = 50;

//We want an internet type connection (TCP/IP) SockAddr.sin_family = AF_INET;

//We want to listen on IP address 127.0.0. //I'll give a few better ways to set thi // s value later SockAddr.sin_addr.S_un.S_un_b.s_b1 = 127; SockAddr.sin_addr.S_un.S_un_b.s_b2 = 0;

Note : Variable of type SOCKETADDR_IN is created instead of SOCKETADDR, the reason is that struct SOCKADD_IN holds the same information in the same way as struct SOCKADDR does, the difference is that struct SOCKADDR_IN is easier to work with.

SockAddr.sin_addr.S_un.S_un_b.s_b3 = 0; SockAddr.sin_addr.S_un.S_un_b.s_b1 = 1;

result=bind(Socket, (SOCKADDR *)(&SockAddr), sizeof(SockAddr)); //attempting to bind

if (result== SOCKET_ERROR) cout<<"Attempt to bind failed"; //SOCKET_ERROR is a pre-defined constant

5. Listening Connections

Now that we have a valid socket bound to a TCP/IP port we need to listen on that socket for incoming

connections. We use the listen() function to accomplish that. The listen() function takes two parameters a bound

socket and the number of connections to accept. Here is how that looks:

listen(Socket, 1);

6. Accepting Connections

Not much to listen(). Just to clarify the listen() function does not accept the incoming connections, it just sets

your socket to listening on the specified port, no more no less. To accept the incoming connection you use

accept(). The accept() function will will watch the port for a breif time then return an error. So unless you know

exactly when the connection is coming and can start accept at just the right time you are going to miss the

connection. One way around this is to place accept() in a while loop until a connection is received. There is a

problem with this technique, in a DOS or console application its fine since nothing else can be happening it

doesn't matter, but in a windows program it will stop responding until it gets out of that loop. You may be able

to set the accept() function to run on a short timer or in a loop that is called in a thread. At any rate here is how

it would look if it were in a while loop until it received a connection:

SOCKET TempSock = SOCKET_ERROR; //declaring temporary socket while (TempSock == SOCKET_ERROR) TempSock = accept(Socket, NULL, NULL); //Accept will return error if there is no connection Socket = TempSock; //The loop breaks when a connection is accepted, // copying it to original socket

7. Sending Data

Now that we have a server with a connected client they need to exchange information. This is done exactly the same for the client as it is for the server. The functions to use are send() and recv(). They both take four parameters the socket to send on, the data to send, and the number of bytes in the data. The way they expect the data is in a pointer to a char

char String[50]; result= send(Socket, String, 50, 0); if (result==0) cout<<"Connection closed at other end."; else cout<<”Data sent = “<<String<<endl;

8. Receiving Data

result= recv(Socket, String, 50, 0); if (result==0) cout<<"Connection closed at other end."; else