Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TCPServer with simultaneous full duplex communication

Tags:

c#

.net

tcp

sockets

I am trying to write a c# server/client that will simultaneously send byte arrays over TCP across each other. I'm trying to wrap my head around how to accomplish this. All of the example I have seen wait for a message, then send a response. I need communication to happen simultaneously.

Would I need to create 2 separate TCP socket connections for ingoing & outgoing on both the server & client? Can I pass data simultaneously with 1 connection in a "full duplex" fashion? Any help is appreciated.

like image 784
user547794 Avatar asked May 28 '26 06:05

user547794


1 Answers

I would advise you to look at the asynchronous sockets. The reason is, that they don't block threads while receiving or sending data.

Socket.BeginReceive(buffer, offset, size, endReceiveMethod);

The endreceive method will be called when there are bytes received. (on a other thread) This is the same for sending.

Socket.BeginSend(buffer, offset, size, endSendMethod);

I remember in the early days I was worried about reading and writing on the same thread, creating difficult constructions with read-timeouts etc and each client it's own thread.

This isn't needed with Asynchronous sockets. It doesn't use a single thread per client. It uses I/O Completion Ports http://msdn.microsoft.com/en-us/library/windows/desktop/aa365198(v=vs.85).aspx instead of blocking threads.

like image 94
Jeroen van Langen Avatar answered May 30 '26 20:05

Jeroen van Langen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!