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

查看:31
本文介绍了为什么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,别无选择,套接字 API 将一个 TCP 连接映射到一个套接字,该套接字位于两个端点之间.

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天全站免登陆