Click here to Skip to main content
15,041,025 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello all.
I have a TCP/IP multithreading server /client application. i want to convert it to UDP protocol for testing performance issues.

I have a question :
As i found on UDP server i have one socket that send and recive data by using sendto and recivefrom but on TCP/ip you have a listening socket and each client connected has its own socket. how to do this using UDP protocol ? on other words how to make multithreading in UDP and only we have one server socket that recive data and as we can say accept connections .Also in sending data can i make for each client socket ? as in TCP /IP?
Posted
Updated 9-Nov-10 4:18am
v2
Comments
Sergey Alexandrovich Kryukov 14-May-13 14:28pm
   
The question is reduced to the following: implement TCP based on UDP.
—SA

1 solution

Not exactly certain of your questions, but I will answer given my use of the UDP protocol in my apps.

I have a server and many clients on a LAN that use UDP for comms. The server broadcasts data to the clients every so often, and the clients also broadcast data to the server every so often. The reason I do the clients to the server using UDP instead of TCP/IP is because of the ability to restart the UDP socket easily when errors occur. I also am able to easily have either the clients or the server start up in any order that they happen to work.

Since you don't know where the source of the data is from, you must include some kind of indicator in your message as to where it is coming from. In my case, the clients include their serial number in the UDP messages going back to the server.

When receiving data, I use the BeginReceive method of the UDP class in order to give it my callback - this is the asynchronous, or multithreaded, way of doing it.

For sending data, I use a thread I create that runs every so often, and in that thread I compose my message then use the Send method in order to send the data. If I get a SocketException when sending the data, I then create a new UdpClient and retry again in 250 ms. This helps with unexpected errors.

You seem to be asking about using multiple client sockets. I don't use multiple sockets for anything other than different messages. I use the same sockets for the same types of messages. In my case, the clients all send the same type of messages to the server, so I use one socket on the server for all these messages. They can come from many different clients at the same time.

Hope this helps.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900