开发者

Sockets terminology - what does "blocking" mean?

开发者 https://www.devze.com 2023-02-28 08:03 出处:网络
When talking sockets programming in C# what does the term blocking mean? I need to build a server component (possibly a Windows service) that will receive data, do some processing and return data bac

When talking sockets programming in C# what does the term blocking mean?

I need to build a server component (possibly a Windows service) that will receive data, do some processing and return data back to the caller. The caller can wait for the reply but I need to ensure that multiple clients can call in at the same time.

If client 1 connects and I take say 10 seconds to process their request, will the socket be blocked for client 2 calling in 2 seconds later? Or will the service start processing a second request on a different thread?

In summary, my clients can wait for a response but I must be able to handle 开发者_JAVA技巧multiple requests simultaneously.


Blocking means that the call you make (send/ receive) does not return ('blocks') until the underlying socket operation has completed.

For read that means until some data has been received or the socket has been closed. For write it means that all data in the buffer has been sent out.

For dealing with multiple clients start a new thread for each client/ give the work to a thread in a threadpool.

Connected TCP sockets can not be shared, so it must be one socket per client anyway.


This means you can't use the socket for anything else on the current executing thread.

It has nothing to do with szerver side. It means the thread pauses whilst it waits for a response from the socket.

If you don't want it to pause, use the async methods.

Read more: http://www.developerfusion.com/article/28/introduction-to-tcpip/8/


A blocking call will hold the currently executing thread until the call completes.

For example, if you wish to read 10 bytes from a network stream call the Read method as follows

byte[] buf = new byte[10];
int bytesRead = stream.Read(buf, 0, buf.Length);

The currently executing thread will block on the Read call until 10 bytes has been read (or the ReadTimeout has expired).

There are Async variants of Read and Write to prevent blocking the current thread. These follow the standard APM pattern in .NET. The Async variants prevent you having to deal out a Thread (which will be blocked) to each client which increases you scalability.

Blocking operations are usually those that send or receive data and those that establish connections (i.e. listen for new clients or connect to other listeners).


To answer your question, blocking basically means that the control stays within a function or block of code (such as readfile() in c++) until it returns and does not move to the code following this code block. This can be either in a Single threaded or a Multi-threaded context. Though having blocking calls in a single threaded code is basically recipe for disaster.

Solution:

To solve this in C#, you can simply use asynchronous methods for example BeginInvoke(), and EndInvoke() in the sockets context, that will not block your calls. This is called asynchronous programming method. You can call BeginInvoke() and EndInvoke() either on a delegate or a control depending on which ASYNCHRONOUS method you follow to achieve this.


You can use the function Socket.Select()

Select(IList checkRead, IList checkWrite, IList checkError, int microSeconds)

to check multiple Sockets for both readability or writability. The advantage is that this is simple. It can be done from a single thread and you can specify how long you want to wait, either forever (-1 microseconds) or a specific duration. And you don't have to make your sockets asynchronous (i.e.: keep them blocking).

It also works for listening sockets. It will report writability. when there is a connection to accept. From experimenting, i can say that it also reports readability for graceful disconnects.

It's probably not as fast as asyncrhonous sockets. It's also not ideal for detecting errors. I haven't had much use for the third parameter, because it doesn't detect an ungraceful disconnect.


You should use one socket per thread. Blocking sockets (synchronous) wait for a response before returning. Non-blocking (asynchronous) can peek to see if any data received and return if no data there yet.

0

精彩评论

暂无评论...
验证码 换一张
取 消