套接字编程:服务器

发布于 2024-11-19 10:02:42 字数 637 浏览 3 评论 0原文

好吧,我一直在尝试自学一些套接字编程。我自己编写了一个带有异步服务器的 C# 小应用程序,我了解其中的大部分内容,但以下内容除外:

因此服务器有一个侦听连接的端口,然后当它收到连接时,它会创建一个不同的套接字来进行通信。这就是我不明白的...当理论上客户端不知道为这个新连接选择了什么端口时,客户端和服务器之间如何进行通信?

感谢您的所有回答

编辑:据我了解,侦听线程侦听默认端口,但所有消息都在每个客户端的不同套接字上处理?

再次编辑:你们是如何误解我的问题的。我了解正常的套接字通信。我的问题是异步服务器的监听套接字与连接套接字不同。 IE。

  1. 服务器在默认端口上侦听
  2. 客户端尝试连接。
  3. 服务器接收请求。
  4. 服务器然后在客户端之间创建一个通信套接字 和服务器并继续监听 在默认端口上。

我的问题出在最后一步。客户端现在如何知道如何在新套接字上进行通信? 这是一些示例代码 http://msdn.microsoft.com/en-us/library/5w7b7x5f.aspx

Ok so I've been trying to teach myself some socket programming. I wrote myself a little C# application with an async server and I understand most of it, except for the following:

So the server has a port it listens on for connections then when it receives a connection it creates a different socket to do the communication on. This it what I dont understand... How does the communication happen between the client and the server when in theory the client has no idea what port has been elected for this new connection?

Thanks for all your answers

Edit: As far as I understand the listening thread listens on the default port, but all messages are then handled on a different socket for each client?

Edit Again: Some how you guys are misunderstanding my question. I understand normal socket communication. My problem is with an async server where the listening socket is different from the connecting socket. Ie.

  1. Server listens on default port
  2. Client attrmpts to connect.
  3. Server receiver request.
  4. Server then creates a communication socket between client
    and server and continues listening
    on the default port.

My problem is at the last step. How does the client now know how to communicate on the new socket?
Here is some sample code
http://msdn.microsoft.com/en-us/library/5w7b7x5f.aspx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心头的小情儿 2024-11-26 10:02:42

尽管这个问题是一年多前发布的,但我相信值得尝试进一步澄清(或混淆?)它。

“客户端现在如何知道如何在新套接字上进行通信?” - 客户端不知道创建了一个新套接字。它只是继续向同一端口发送数据(数据包)。

然而,这又带来了另一个问题:服务器如何知道哪些数据来自哪个客户端? - 由于 TCP 和 IP 协议,服务器知道客户端的地址和发送数据包的源端口。有了这些信息,服务器就可以从多个客户端和多个(客户端)端口接收数据包,并将它们路由到正确的套接字。对于这个问题,将服务器套接字视为过滤器:当从客户端 X - 端口 Y 接收数据包时,然后将它们路由到套接字 Z。

“...它现在知道它需要在不同的套接字/端口上进行通信吗?” - 这是一个常见的混乱来源。当服务器上创建新的套接字来接收数据包时(建立连接后),它不会使用新端口,而是继续使用原始端口号。服务器端的整个socket创建过程对客户端来说是透明的。客户端永远不知道(也不需要知道)创建了一个新套接字来处理其数据包。

谷歌 TCP 标头了解更多信息。

希望这对某人有帮助。

Although this question was posted over a year ago, I belive it is worth trying to clarify (or confuse?) it a bit more.

"How does the client now know how to communicate on the new socket?" - The client is not aware that a new socket was created. It simply keeps on sending data (packets) to the same port.

However, this gives rise to another question: How does the server know which data comes from which client? - Thanks to the TCP and IP protocols the server knows both the address of the client and the source port from which the packets were sent. With this information the server can receive packets from multiple clients and multiple (client) ports and route them to the correct socket. For this question, think of server sockets as filters: when packets are received from client X - port Y then route them to socket Z.

"...does it now know it needs to communicate on a different socket/port?" - This is a frequent source of confusion. When a new socket is created on the server to receive packets (after the connection has been established) it does not use a new port, it keeps on using the original port number. The entire socket creation process on the server side is transparent to the client. The client never knows (nor does it need to know) that a new socket was created to handle its packets.

Google TCP header for more information.

Hope this helps someone.

万劫不复 2024-11-26 10:02:42

当客户端连接到服务器时,它会选择要连接的端口。客户端还包括一个将在其上接收响应的端口。这通常是随机选择的端口,但客户端可以覆盖它。

把它想象成一个电话。当你给某人打电话时,有你拨打的电话号码,你也有一个电话号码。即使你们互相通话,两个电话号码都已被使用。

这不是一个完美的类比,因为电话号码更像是 IP 地址,并且中继线不需要在所有情况下都有始发电话号码,但同样的概念适用。

简单地说,TCP协议需要源端口和目标端口以及源IP地址和目标IP。当数据包以任一方向发送时,都会使用适当的 IP/端口。

When the client connects to the server, it selects the port to connect to. The client also includes a port that it will receive responses on. This is typically a randomly selected port, but it's possible for the client to override that.

Think of it like a phone call. When you call someone, there is the phone number you call, and you also have a phone number. even though you both talk to each other, both phone numbers are in use.

That's not a perfect analogy, since phone numbers are more like IP addresses and trunk lines need not have an originating phone number in all cases, but the same concept applies.

Simply put, the TCP protocol requires an originating port and destination port as well as originating ip address and destination IP. When packets are sent in either direction, the apropriate IP/Port is used either way.

暮光沉寂 2024-11-26 10:02:42

实际上新连接使用相同的端口。服务器在特定端口上侦听传入连接,每当收到来自客户端的连接请求时,服务器都会接受它并创建一个新线程来处理该请求。然后继续监听该端口。

Actually the new connection use the same port. A server listen on a specific port for incoming connection, anytime it receives connection request from client, the server accept it and create a new thread to process the request. And then continue to listen on that port.

淑女气质 2024-11-26 10:02:42

定义

  • 客户端:连接到服务器的远程计算机上的套接字
  • 服务器:正在服务器上等待连接的套接字 服务器
  • 客户端:正在与客户端通信的套接字

应答

我找不到有关 ServerClient 端口在 Server 接受后如何传输到 Client 的任何详细信息。但它很可能是在握手中转移的。如果您想了解更多信息,请随时阅读 RFC793。

我不会详细介绍,但您可以阅读被动连接以获取有关侦听器套接字如何在较低级别工作的更多信息。但基本上侦听器套接字 (Server) 的目的只是接受套接字 (ServerClient)。

ServerClient 使用的端口是由操作系统中的套接字实现分配的,您无法控制。您需要知道的是,每个连接的 ServerClient 将获得其自己的端口,并在(三向)握手期间将其传输到 Client我认为;)

Definitions

  • Client: The socket at the remote machine that is connecting to the server
  • Server: The socket that is waiting for connections on the server
  • ServerClient: The socket which is communicating with the Client

Answer

I couldn't find any details about how the ServerClient port is transfered to the Client after the Server has accepted it. But it's most likely transfered in the handshake. Feel free to read RFC793 if you want to know more.

I wont go through the details, but you can read about passive connects to get more information about how listener sockets work at lower levels. But basically the purpose of listener sockets (Server) is only to accept sockets (ServerClient).

The port that the ServerClient uses is assigned by the socket implementation in the operating system and is nothing that you can control. All you need to know is that the each connected ServerClient will get it's own port and it's transfered to the Client during the (threeway) handskake (I think ;))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文