套接字术语 - “阻塞"是什么?意思? [英] Sockets terminology - what does "blocking" mean?

查看:35
本文介绍了套接字术语 - “阻塞"是什么?意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 C# 中谈论套接字编程时,术语阻塞是什么意思?

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

我需要构建一个服务器组件(可能是一个 Windows 服务)来接收数据、进行一些处理并将数据返回给调用者.调用者可以等待回复,但我需要确保多个客户端可以同时调用.

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.

如果客户端 1 连接并且我说 10 秒来处理他们的请求,套接字会在 2 秒后被客户端 2 调用阻塞吗?或者服务会开始处理不同线程上的第二个请求?

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 multiple requests simultaneously.

推荐答案

阻塞意味​​着你发起的调用(发送/接收)在底层套接字操作完成之前不会返回('阻塞').

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

对于读取,这意味着直到接收到一些数据或套接字已关闭.对于 write 表示缓冲区中的所有数据都已发送出去.

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.

无法共享已连接的 TCP 套接字,因此无论如何每个客户端必须有一个套接字.

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

这篇关于套接字术语 - “阻塞"是什么?意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆