Is it possible to create a single socket, single thread, TCP/IP packet reader for multiple clients?

GeirGrusom

A typical server application would create a socket for each incoming connection spawn a new thread.

However is it possible to do the demuxing yourself in a single thread? What I want is that the application keeps a state machine for each end point which deserializes message as data becomes available in a single thread.

Threading is the simplest, and I have a working implementation of that. However each thread adds overhead fairly quickly, and lingering connections will require resources. What I want is that instead for each connection a state machine builds up a message (which the threaded code already does anyway) and upon completion dispatches the deserialized message to a worker queue.

Is this possible in .NET? P/Invoke would also be an acceptable solution.

Peter Duniho

A typical server application would create a socket for each incoming connection spawn a new thread.

That is not a true statement. Very few servers — only those which need not scale to large numbers of connected clients, and not even all of those — will dedicate an entire thread to a single connection.

I would say only the most rudimentary servers would use a thread-per-connection design. I suppose that since there are a lot more experimental servers (i.e. hobbyists learning to write network code) than production servers, the sheer numbers might be on the side of thread-per-connection.

But IMHO only production servers are really relevant to the question, as they show what good design and implementation would be. And for those, thread-per-connection is definitely going to be in the minority.

However is it possible to do the demuxing yourself in a single thread?

Using Socket.Select() one can have a single thread dedicated to the handling of several sockets.

However, more typical would be to use one of the several asynchronous programming APIs available for use with sockets, which uses I/O completion ports to farm out handling of I/O operations to a pool of threads dedicated for the purpose. This allows concurrency but efficient use of the threads to minimize context-switching.

My preferred approach with the current .NET 4.5 and C# 5.0 features is to wrap the socket in an instance of NetworkStream, so that I can use the ReadAsync() and WriteAsync() methods, which in turn allow the use of await in the C# code. This makes the asynchronous code a lot easier to read and implement.

But you can use e.g. Socket.BeginReceive() or Socket.ReceiveAsync() (the latter being useful for servers that require extremely high scalability…even the former is still much more scalable than thread-per-connection though) just as well.

Regardless of API used, the typical server implementation will include a state object class for each connection. This can be done with thread-per-connection design as easily as with the async I/O designs. Your state machine would reside in that state object class, so that the processing of I/O operations on the socket can use the state machine.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Multiple Thread Writer with Single Thread Reader using PipedOutputStream and PipedInputStream

From Dev

Is it possible to interleave execution of multiple delegates on a single thread?

From Dev

tcp socket forwarding from multiple sequential clients to a single persistent socket connection to server

From Dev

multiple InputReader for a single Socket

From Dev

I am trying to develop a chat application using socket Programming in single server and multiple Clients

From Dev

C# Dictionary<T,K> thread-safety with single writer multiple reader

From Dev

PyMongo: Should I use single or multiple clients?

From Dev

Multiple clients for single Azure Service Bus queue

From Dev

Is it possible to create a single pdf from multiple Google spreadsheets?

From Dev

Is it possible to create and send multiple DocuSign envelopes with a single API call

From Dev

Create signal handler for a single thread

From Dev

Multiple transaction scopes in single thread?

From Dev

Synchronization of multiple tasks on single thread

From Dev

Is it possible to create a VPN on a single machine?

From Dev

Single TCP/IP server that handles multiple clients (in C++)?

From Dev

Possibility of multiple CAsyncSocket servers and clients linked with a single MFC modal Dialog

From Dev

Can a server handle multiple sockets in a single thread?

From Dev

Send Data from multiple threads to a single thread

From Dev

Single reader multiple writers with pthreads and locks and without boost

From Dev

Multiple writes in server socket to single read in client socket program?

From Dev

Is it Possible to Create Nuget Package for Single Project in a Solution?

From Dev

Is it possible to create one string from single characters?

From Dev

Is It Possible to Create Nuget Package for only a Single Namespace?

From Dev

Is it possible to have multiple controllers for a single entity type?

From Dev

Is it possible to `git squash` multiple branches into a single branch?

From Dev

A single exception handler for multiple possible exceptions

From Dev

Is it possible to use multiple macros on a single line?

From Dev

Is it possible to set multiple decorators for a single node?

From Dev

Multiple indices under a single name in kibana is possible?

Related Related

  1. 1

    Multiple Thread Writer with Single Thread Reader using PipedOutputStream and PipedInputStream

  2. 2

    Is it possible to interleave execution of multiple delegates on a single thread?

  3. 3

    tcp socket forwarding from multiple sequential clients to a single persistent socket connection to server

  4. 4

    multiple InputReader for a single Socket

  5. 5

    I am trying to develop a chat application using socket Programming in single server and multiple Clients

  6. 6

    C# Dictionary<T,K> thread-safety with single writer multiple reader

  7. 7

    PyMongo: Should I use single or multiple clients?

  8. 8

    Multiple clients for single Azure Service Bus queue

  9. 9

    Is it possible to create a single pdf from multiple Google spreadsheets?

  10. 10

    Is it possible to create and send multiple DocuSign envelopes with a single API call

  11. 11

    Create signal handler for a single thread

  12. 12

    Multiple transaction scopes in single thread?

  13. 13

    Synchronization of multiple tasks on single thread

  14. 14

    Is it possible to create a VPN on a single machine?

  15. 15

    Single TCP/IP server that handles multiple clients (in C++)?

  16. 16

    Possibility of multiple CAsyncSocket servers and clients linked with a single MFC modal Dialog

  17. 17

    Can a server handle multiple sockets in a single thread?

  18. 18

    Send Data from multiple threads to a single thread

  19. 19

    Single reader multiple writers with pthreads and locks and without boost

  20. 20

    Multiple writes in server socket to single read in client socket program?

  21. 21

    Is it Possible to Create Nuget Package for Single Project in a Solution?

  22. 22

    Is it possible to create one string from single characters?

  23. 23

    Is It Possible to Create Nuget Package for only a Single Namespace?

  24. 24

    Is it possible to have multiple controllers for a single entity type?

  25. 25

    Is it possible to `git squash` multiple branches into a single branch?

  26. 26

    A single exception handler for multiple possible exceptions

  27. 27

    Is it possible to use multiple macros on a single line?

  28. 28

    Is it possible to set multiple decorators for a single node?

  29. 29

    Multiple indices under a single name in kibana is possible?

HotTag

Archive