Problems with .NET C # Socket Concurrency

System.Net.Sockets.Socket Instance

can be shared by two threads, so use the send () method and the other receive () method?

Is it safe?

Well, I need it to be not only thread safe, but also because send / receive methods are not synchronized so that each thread calls them at the same time.

Do I have another way to do this?

Thanks for the help, I am experienced in java, but trying to do it with difficulty.

+4
source share
3 answers

It should be safe, yes. The Socket class is quoted by MSDN to be completely thread safe .

I do not know if this is a good idea. It may be difficult for you to do this using two threads. You will probably want to look at BeginSend and BeginReceive for asynchronous versions, in which case you will not need multiple threads.

+6
source

Yes, it is absolutely safe to access the sending and receive from two different streams at the same time.

If you want your application to scale to 100 active sockets, you need to use the BeginReceiveve / BeginSend methods, and not create threads manually. This will do the magic behind the scenes, so you won’t create 100 threads to handle sockets. What exactly this does depends on the platform. In windows, you will use the "high performance" completion ports. Under linux (mono) you will use epoll, I believe. In any case, you end up using a lot less threads than active sockets, which is always good :)

+2
source

A small topic, but using synchronization methods is only useful if you have limited customers. I realized that asynchronous sockets are slower with the answer. Asynchronous sockets are much better served by many clients.

So: synchronization is faster. async is more scalable

+2
source

All Articles