简体   繁体   English

使用TCP处理多个线程

[英]Handling Multiple threads with TCP

I am trying to implement a chat application, and have a design choice to make about using TCP or UDP for the messaging exchange between peers. 我正在尝试实现一个聊天应用程序,并有一个设计选择来考虑使用TCP或UDP在同级之间进行消息交换。 I want to use TCP but am having the following issue. 我想使用TCP,但是遇到以下问题。

Problem Scenario: Peer A is listening on a well known port, (say 5555). 问题场景:对等A正在一个众所周知的端口上侦听(例如5555)。 When peer B wants to send message to peer A, it connects to the port 5555 on A. Peer A accepts the connection and starts a new thread to deal the communication with peer B, so that other peers (say peer C) are able to connect to peer A's 5555 port. 当对等体B要向对等体A发送消息时,它连接到A上的端口5555。对等体A接受该连接并启动新线程来处理与对等体B的通信,以便其他对等体(例如对等体C)连接到对等体A的5555端口。 Now the problem is that its not a request/response protocol, so I am confused that if peer A doesn't reply to peer B for any reason, then the subsequent messages sent by B will be delivered to port 5555 on peer A ? 现在的问题是它不是请求/响应协议,所以我很困惑,如果对等体A由于某种原因没有答复对等体B,那么B发出的后续消息将传递到对等体A的端口5555? and peer A will create separate threads for each of the messages received ? 对等方A将为收到的每个消息创建单独的线程?

Using UDP will probably solve this problem and I wont have to create separate threads for communicating with each peer and everyone can send the messages to the same well known port. 使用UDP可能会解决此问题,并且我将不必创建用于与每个对等方通信的单独线程,并且每个人都可以将消息发送到相同的知名端口。 But I want to use TCP for guaranteeing that the messages will be delivered. 但是我想使用TCP来保证消息将被传递。 Any ideas what would be a good way to handle this problem and use only one thread for communication with one peer ? 有什么想法是处理此问题并仅使用一个线程与一个对等方进行通信的好方法?

The problem you describe won't happen because TCP is a 'connected' protocol, which basically means that the two peers have to negotiate the communication before anything else can happen. 您描述的问题不会发生,因为TCP是一种“连接”协议,这基本上意味着两个对等方必须先协商通信,然后才能进行其他任何事情。 After that, TCP controls the order of data packets to make sure they arrive at destination and in the right order. 之后,TCP控制数据包的顺序,以确保它们以正确的顺序到达目的地。 BTW, TCP stands for Transmission Control Protocol so it puts a lot of emphasis making sure what you described does not happen. 顺便说一句,TCP代表传输控制协议,因此它特别强调确保您所描述的内容不会发生。 It's not at all the case with UDP. UDP完全不是这样。

Once your ServerSocket has accepted the connection from the client Socket , the negotiation has been finalized and the TCP stream is dedicated to that communication. 一旦ServerSocket接受了来自客户端Socket的连接,就完成了协商,并且TCP流专用于该通信。

The only way a new one is created is if your client emits another connect through a new Socket. 创建新连接的唯一方法是,如果客户端通过新Socket发出另一个连接。

But the best way to convince yourself is to add logging to your app and try it yourself. 但是说服自己的最佳方法是将日志添加到您的应用中,然后自己尝试。

You are confusing listening , or server, sockets and connected sockets. 您在使Listen或Server,套接字和连接的套接字混乱。

Once TCP connection is accepted on the listening side, you have a brand-new full-duplex socket between two parties, so they can exchange data. 一旦在侦听端接受了TCP连接,您就会在两方之间拥有一个全新的全双工套接字,以便它们可以交换数据。 The only purpose of a listening socket is to accept connections, no application data flows through those. 监听套接字的唯一目的是接受连接,没有应用程序数据通过这些连接流动。

You can hand that new connected socket off to a thread, but you certainly don't have to - you can handle many non-blocking sockets in a single thread, I believe Java NIO package was created for exactly this. 您可以将新连接的套接字移交给一个线程,但是您不必这样做-您可以在一个线程中处理许多非阻塞套接字,我相信Java NIO包正是为此而创建的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM