简体   繁体   中英

Is there an advantage or disadvantage to using the same socket for UDP send and receive? Using C and C#

I have a UDP server in C on a Linux VM and a UDP client in C# in the host Windows 7 machine.

The UDP server listens for connections. UDP client connects then sends a request. The server receives the request, processes it, then sends back a reply (of less than 100 bytes). The UDP client receives the reply and does some work. This process repeats over and over again, at the rate of about 10 request/reply pairs per second continuously.

Currently, I have the UDP server listening and receiving on port 11000 and sending on port 10001, and the client listening and receiving on port 10001 and sending on port 11000. The socket that is being used to listen is kept open on both sides. With sending, each side is opening the send socket, sending data, then closing until the next request is received. So far, this is working.

I understand that it should be possible to use the SAME socket for both sending and receiving. I haven't been able to get this to work yet, but that isn't my question. My question is, is there an appreciable advantage, in my situation, to using the same socket, if it's working as it currently stands? Is there any disadvantage? Or any advantage to having two separate sockets as in my current implementation?

Thank you.

Of course there are penalties doing what you are doing, resource wasting.

Each time you create a socket, send the data and destroy it you are allocating/deallocating resources unnecesarily.

Supose you have a high message rate, each time you send a message you create/destroy one socket, and sockets are not destroyed immediately (at least in TCP, maybe in UDP i'm wrong).

If you can use just one socket, do it, when you are talking to someone with your cell phone you don't buy a new one each time you want to say something in a conversation and throw it to the trash, true? ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM