为什么 UDP 服务器中只有一个套接字? [英] why a single socket in UDP servers?

查看:36
本文介绍了为什么 UDP 服务器中只有一个套接字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在准备考试,发现了这个问题:

I am studying for my exams and found this question:

可以使用单个套接字来实现典型的 UDP 服务器.解释一下为什么对于 TCP 驱动的服务器,我发现创建了两个套接字 - 一个用于所有客户端都接近服务器,一个用于每个客户端的特定(套接字)用于服务器和客户端之间的进一步通信.

A typical UDP server can be implemented using a single socket. Explain why, for a TCP driven server, I find that two sockets are created - one where all clients approach the server, and one specific (socket) for each client for further communication between the server and client.

这(在我的理解中)是由并发问题驱动的(希望不与接触点地址上的单个客户进行太多沟通).我知道 UDP 是无连接的,但无法在我的脑海中说明它.我看到如果服务器是 UDP 驱动的,它可以执行单个操作(通过/到套接字/端口重复泵送内容),然后可以由多个客户端监听.如果服务器可以对两个任务做出反应 - 一个 get 和一个 put.客户端如何在不创建连接的情况下发出指令?客户端(在我看来)需要在已知端口上发送 get-request,并在同一端口上获得反馈.这将阻止服务器同时与多个客户端通信的能力.那么创建第二个套接字来在双方之间进行通信会更好,这样服务器和其他客户端之间的潜在通信就不会受到阻碍吗?(与 tcp 一样)

This is (in my understanding) driven by concurrency-issues (the wish not to communicate too much with a single client on the contact-point address). I know that UDP is connectionless, but can't illustrate it in my mind. I see that if a server is UDP-driven it can do a single action (pump a content repeatedly through/to a socket/port), which then could be listen to by multiple clients. If a server can react to two tasks - a get and a put. How can a client give an instruction without creating a connection? The client (in my mind) needs to send the get-request on a known port, and get the feedback on the same port. This would block the servers ability to communicate with multiple clients at the same time. Then would it be nicer to create a second socket to communicate on between the two parties so that potential communication between the server and other clients is not hindered? (as in the case with tcp)

推荐答案

对于TCP,没办法,socket API将一个TCP连接映射到一个socket,在两个端点之间.

For TCP, there's no choice, the socket API maps one TCP connection to one socket, which is between two endpoints.

对于 UDP,套接字 API 允许一个套接字从多个端点接收,并发送到多个端点 - 很多服务器只使用一个套接字,因为不需要更多.

For UDP, the socket API allows one socket to receive from many endpoints, and to send to many endpoints - so many servers use just one socket since there isn't any need for more.

在某些情况下,协议是一个简单的请求和回复.无需为此创建另一个套接字 - 只需记下源地址,然后将回复发送到那里 - 这就是某些服务器所做的.

In some cases, the protocol is a simple request and reply. There's no need to create another socket for that - just take note of the source address, and send the reply there - so that's what some servers do.

对于其他人,该协议可能需要更长时间的数据交换,以便更方便地创建新套接字,因此一些服务器会这样做.

For others, the protocol might require longer lived data exchange where it's more convenient to create a new socket, so some servers do that.

这会阻止服务器与多个服务器进行通信客户同时.

This would block the servers ability to communicate with multiple clients at the same time.

不一定.如果服务器 CPU 忙于执行指令,则无论它是否在同一个套接字上处理多个客户端,它都无法为其他任何人提供服务.如果服务器确实阻塞调用(例如数据库查询),或者您想利用多个内核,您可以在多个线程中处理它,或者即使只有 1 个套接字也可以使用线程池模式.服务器只需要跟踪每个数据包的源 IP 地址和端口,以便知道将回复发送到哪里.

Not necessarily. If the server CPU is busy executing instructions, it can't service anyone else regardless of whether it handles multiple clients on the same socket or not. If the server does blocking calls (e.g. a database query), or you want to exploit multiple cores, you can handle that in multiple threads, or use a threadpool pattern even with just 1 socket. The server just needs to keep track of the source IP address and port for each packet so it knows where to send the reply to.

但如果特定协议/应用程序使用多个套接字更有意义,例如每个客户一个 - 这样做有问题,在这种情况下通常的方法是:

But if it makes more sense for a particular protocol/application to use multiple sockets, e.g. one per client - there's noting wrong with doing so, the usual approach in that case is:

  • 客户端通过其众所周知的端口向服务器发送数据包
  • 服务器记录客户端数据包的源端口
  • 服务器创建一个新套接字,并在该套接字上发送回复
  • 客户端注明回复的源端口
  • 客户端使用该端口而不是其众所周知的端口与服务器进行进一步通信.

这篇关于为什么 UDP 服务器中只有一个套接字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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