Tcp Listener和客户端监听specfic ip

时间:2022-09-09 00:03:27

What i am trying to do is listen to a socket of 5000, which works perfectly with the code

我想要做的是听一个5000的套接字,它与代码完美配合

TcpListener listener = new TcpListener(IPAddress.Any, 5000);
NetworkStream Network;
TcpClient client;
client = listener.AcceptTcpClient();

but when the server has two clients that connect to the server they both listen to the same message coming through as they are multi threaded, i dont want this to happen because with them reading each other they are removing bytes from the network stream.

但是当服务器有两个连接到服务器的客户端时,它们都会听到相同的消息,因为它们是多线程的,我不希望这发生,因为他们互相读取它们正在从网络流中删除字节。

so my question is, is there a way for the listener to listen to any ip until it finally receives a connection, then once the connection has been made the thread only listens to that ip address??

所以我的问题是,是否有一种方法让监听器监听任何IP,直到它最终收到一个连接,然后一旦建立连接,线程只监听该IP地址?

Thank you

1 个解决方案

#1


TCP does not work that way. When you create a TCP socket, bind to port A, and listen what you have is :

TCP无法正常工作。创建TCP套接字时,绑定到端口A,然后监听您拥有的内容:

  • A listening socket on port A
  • 端口A上的侦听套接字

A client connects to the server, what happens is that the listening socket is cloned, the clone is part of a socket pair, the other socket is the connecting socket. The 'cloned' socket is called the servicing socket. End result :

客户端连接到服务器,会发生的事情是克隆侦听套接字,克隆是套接字对的一部分,另一个套接字是连接套接字。 '克隆'套接字称为服务套接字。最终结果 :

  • A listening socket on port A (still lives, you did not close it)
  • 端口A上的监听套接字(仍然存在,您没有关闭它)

  • A servicing socket on port A, but part of a socket pair. (IPA(A), IPB(B))
  • 端口A上的服务套接字,但是是套接字对的一部分。 (IPA(A),IPB(B))

The socket pair identifies the connection! When something is received at TCP level, the source ip address and port are also checked and as such the right servicing socket is identified where upon the reception will take place. You never ever receive data on a listening socket.

套接字对识别连接!当在TCP级别接收到某些内容时,还会检查源IP地址和端口,因此将识别正确的服务套接字,以便在接收时进行。您永远不会在侦听套接字上收到数据。

If another client connects you get this, assuming a different ip address and port:

如果另一个客户端连接你得到这个,假设一个不同的IP地址和端口:

  • A listening socket on port A (still lives, you did not close it)
  • 端口A上的监听套接字(仍然存在,您没有关闭它)

  • A servicing socket on port A, but part of a socket pair. (IPA(A), IPB(B))
  • 端口A上的服务套接字,但是是套接字对的一部分。 (IPA(A),IPB(B))

  • A servicing socket on port A, but part of a socket pair. (IPA(A), IPC(C))
  • 端口A上的服务套接字,但是是套接字对的一部分。 (IPA(A),IPC(C))

and this is assuming that no connections are closed. You see now you have 3 sockets on your server system, all using the same port. 1 listening socket, and a servicing socket per connection that is being establised. ie per client. Each connection is distinct, the right socket will receive data that belongs to the connection. There are only 2 ends in a connection, and connections are bidirectional.

这假设没有关闭连接。您现在看到服务器系统上有3个插槽,所有插槽都使用相同的端口。 1个监听套接字,以及正在建立的每个连接的服务套接字。即每个客户。每个连接都是不同的,右侧套接字将接收属于该连接的数据。连接中只有两个端点,连接是双向的。

TCP is a bit complex, you may find my explanation daunting in which case you should try to read more about TCP in books, or on the internet. Also socket programming is an interesting read because a mere explanation of TCP does not explain what happens at socket level.

TCP有点复杂,您可能会发现我的解释令人生畏,在这种情况下,您应该尝试在书籍或互联网上阅读有关TCP的更多信息。套接字编程也是一个有趣的阅读,因为仅仅解释TCP并不能解释套接字级别的情况。

#1


TCP does not work that way. When you create a TCP socket, bind to port A, and listen what you have is :

TCP无法正常工作。创建TCP套接字时,绑定到端口A,然后监听您拥有的内容:

  • A listening socket on port A
  • 端口A上的侦听套接字

A client connects to the server, what happens is that the listening socket is cloned, the clone is part of a socket pair, the other socket is the connecting socket. The 'cloned' socket is called the servicing socket. End result :

客户端连接到服务器,会发生的事情是克隆侦听套接字,克隆是套接字对的一部分,另一个套接字是连接套接字。 '克隆'套接字称为服务套接字。最终结果 :

  • A listening socket on port A (still lives, you did not close it)
  • 端口A上的监听套接字(仍然存在,您没有关闭它)

  • A servicing socket on port A, but part of a socket pair. (IPA(A), IPB(B))
  • 端口A上的服务套接字,但是是套接字对的一部分。 (IPA(A),IPB(B))

The socket pair identifies the connection! When something is received at TCP level, the source ip address and port are also checked and as such the right servicing socket is identified where upon the reception will take place. You never ever receive data on a listening socket.

套接字对识别连接!当在TCP级别接收到某些内容时,还会检查源IP地址和端口,因此将识别正确的服务套接字,以便在接收时进行。您永远不会在侦听套接字上收到数据。

If another client connects you get this, assuming a different ip address and port:

如果另一个客户端连接你得到这个,假设一个不同的IP地址和端口:

  • A listening socket on port A (still lives, you did not close it)
  • 端口A上的监听套接字(仍然存在,您没有关闭它)

  • A servicing socket on port A, but part of a socket pair. (IPA(A), IPB(B))
  • 端口A上的服务套接字,但是是套接字对的一部分。 (IPA(A),IPB(B))

  • A servicing socket on port A, but part of a socket pair. (IPA(A), IPC(C))
  • 端口A上的服务套接字,但是是套接字对的一部分。 (IPA(A),IPC(C))

and this is assuming that no connections are closed. You see now you have 3 sockets on your server system, all using the same port. 1 listening socket, and a servicing socket per connection that is being establised. ie per client. Each connection is distinct, the right socket will receive data that belongs to the connection. There are only 2 ends in a connection, and connections are bidirectional.

这假设没有关闭连接。您现在看到服务器系统上有3个插槽,所有插槽都使用相同的端口。 1个监听套接字,以及正在建立的每个连接的服务套接字。即每个客户。每个连接都是不同的,右侧套接字将接收属于该连接的数据。连接中只有两个端点,连接是双向的。

TCP is a bit complex, you may find my explanation daunting in which case you should try to read more about TCP in books, or on the internet. Also socket programming is an interesting read because a mere explanation of TCP does not explain what happens at socket level.

TCP有点复杂,您可能会发现我的解释令人生畏,在这种情况下,您应该尝试在书籍或互联网上阅读有关TCP的更多信息。套接字编程也是一个有趣的阅读,因为仅仅解释TCP并不能解释套接字级别的情况。