Ayse
Ayse

Reputation: 2754

creating two sockets at udp server at a time, how to bind()?

I want two functionalities to be implemented on my udp server application.

  1. Creating thread that continuously receives data coming from any client.

  2. Creating a thread that continuously sends data on server socket after specific time period and waits for reply from client. (I implemented this to make aure that whenever any client goes down, the data is not received back from client and server comes to know that client is down.)

Now, the problem I am facing is that since two threads are sharing same connected socket, whenever both threads try to access this socket simultaneously, a deadlock is established.

One of the solution I found was to create two sockets. One that continuously receives data, and the other socket that is meant for sending data from server time to time to clients and wait for their response, but since Server can must be bind()ed and I have bind()ed my socket to INADDR_ANY once, how would I create a separate socket for sending data from server and waiting for replies from client.

Please help me with this complication.

Also do let me know if there is some other better way of its implementation.
Thanks in advance :)

Upvotes: 0

Views: 419

Answers (1)

Dariusz
Dariusz

Reputation: 22241

You will have to use non-blocking net functions and use a mutex to ensure no two threads access the socket at once.

A single thread may, however, be enough, if you use non-blocking functions. Using many threads will probably not improve performance, but may make the code more readable.

Upvotes: 1

Related Questions