UDP socket programming in winsock

UDP sockets

UDP stands for User Datagram Protocol and is an alternative protocol to TCP the most common protocol used for data transfer over the internet. UDP is different from TCP in a number of ways. Most importantly UDP is a connectionless protocol.

In the TCP protocol first a connection is established by performing the 3 step handshake. This is done by calling the connect() socket function. However there is no such connection established in UDP. In simple terms when using the udp protocol, the client throws a packet at the server and its upto the server whether it catches it or not. If it fails then the udp protocol is not concerned.

This is opposite to what happens in the TCP protocol. If the receiver side fails to receive a packet then the sender side will find it out and resend the packet till it is properly received by the receiver. This is precisely the concept of connection.

In this article we are going to do some very simple udp socket programming by making a server and a client. We shall be doing this on the windows platform and on windows the socket api is called winsock. For coding its recommended to use VC++ 6.0 or 2010 express edition which is free and can be downloaded from microsoft.com

Now this server can be tested with an application called netcat. Over here we shall use the ncat implementation of netcat. It comes with nmap. Download and install nmap. Then do the following in the terminal.

Now the line "----------:8888". It is our udp server that is listening on port 8888.
It is interesting to note that the netstat command will not show any connections for any client that is connected to the udp server, for example ncat.

Client

Now that we have tested our server with ncat, its time to make a client program which shall connect to the server and do the same things that the ncat program did earlier.

The client will ask user to input a message which it will send to the udp server and the udp server shall reply back with the message

Initialising Winsock...Initialised.
Enter message : hello
hello
Enter message : how are you
how are you
Enter message :

Conclusion

So communication with udp sockets is quite simple. And therefore udp sockets are used where the communication itself is very simple, for example dns requests/response etc. Or when doing some kind of multicast/broadcast. Where its not a big issue if data fails to transfer, or resending the packet is not expensive, udp can be used.

UDP has other benefits as well. Since there is not establishment of connection, no verification that the packet reached or not, it uses lesser bandwidth and is faster than TCP.

11 thoughts on “UDP socket programming in winsock”

I want to use UDP to communicate between two program (script and SIMULINK) . So I tried to open one Socket (IP (127.0.0.1 ) ,remote address 1234 Local port 25) for transmit and one socket for Receive (IP (127.0.0.1 ) ,remote address 4321 Local port 88).
but when i open two sockets and try a send to im getting a WSAERROR (10047)) .
If i remove the receive socket and try it starts to send .
Can you please help me ??

This site, binarytides.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com.