Window Socket Programming
Window: Socket programming with winsock
Requirement
Window OS only
C programming
Link:
ws2_32.libInclude:
winsock2.h

Reading List
Client
We wil learn how to :
Initialize Winsock
Create a socket
Connect to remote server
Send some data
Receive a reply
Initialising Winsock
Winsock first needs to be initialiased with
int WSAAPI WSAStartup( [in] WORD wVersionRequested, [out] LPWSADATA lpWSAData );
Second one is a WSADATA structure
Creating Socket
The socket() function is used to create a socket.
SOCKET WSAAPI socket( [in] int af, [in] int type, [in] int protocol );
Address Family : AF_INET (this is IP version 4)
Type : SOCK_STREAM (this means connection oriented TCP protocol)
Protocol : 0 [ or IPPROTO_TCP , IPPROTO_UDP ]
Connect to Server
Need (1) IP address (2) Port number to connect
Use sockaddr_in structure with proper values filled in.
Function inet_addr is a very handy function to convert an IP address to a long format.
``
Sending Data, Receive Data
Use send( ) to send some data
Use recv() to receive data
Close Socket
Use closesocket() and WSACleanup to unload the library
Example code for Client
Server
Open a socket (see client)
Bind to address and port.
Listen for incoming connections.
Accept connections
Read/Send
Bind
We bind a socket to a particular IP address and a certain port number. We ensure that all incoming data which is directed towards this port number is received by this application
Listen for connection
we need to do is listen for connections. For this we need to put the socket in listening mode. Function listen is used to put the socket in listening mode
Accept connection
Sending Receiving data
Replying to the client
We had to use a getchar because otherwise the output would scroll out of the client terminal without waiting
Example code for Live Server
Now run the program in 1 terminal , and open 3 other terminals. From each of the 3 terminal do a telnet to the server port.
Run telnet like this. It will launch the interactive prompt.
At the telnet shell, run the command "open localhost 8888". This command will try to connect to localhost on port number 8888.
Next you should see the following message at the telnet prompt. This message is received from the socket server running on port 8888.
On the other hand, the server terminal would show the following messages, indicating that a client connected to it.
So now the server is running nonstop and the telnet terminals are also connected nonstop. Now close the server program.
All telnet terminals would show "Connection to host lost." Good so far. But still there is not effective communication between the server and the client.
The server program accepts connections in a loop and just send them a reply, after that it does nothing with them. Also it is not able to handle more than 1 connection at a time. So now its time to handle the connections , and handle multiple connections together.
Handle multiple connections
To handle every connection we need a separate handling code to run along with the main server accepting connections. One way to achieve this is using threads. The main server program accepts a connection and creates a new thread to handle communication for the connection, and then the server goes back to accept more connections.
On Linux threading can be done with the pthread (posix threads) library. It would be good to read some small tutorial about it if you dont know anything about it. However the usage is not very complicated.
We shall now use threads to create handlers for each connection the server accepts.
Linux
This one looks good , but the communication handler is also quite dumb. After the greeting it terminates. It should stay alive and keep communicating with the client.
One way to do this is by making the connection handler wait for some message from a client as long as the client is connected. If the client disconnects , the connection handler ends.
So the connection handler can be rewritten like this
The above connection handler takes some input from the client and replies back with the same. Simple! Here is how the telnet output might look
So now we have a server thats communicative. Thats useful now.
Linking the pthread library
When compiling programs that use the pthread library you need to link the library. This is done like this :
Last updated
Was this helpful?