简体   繁体   English

多个tcpip客户端连接到同一服务器?

[英]multiple tcpip clients connected to same server?

I'm just starting out learning about TCP/IP. 我刚刚开始学习有关TCP / IP的知识。

I have written a simple TCP/IP server which listens on port 3000 and spawns a thread when a connection is received. 我编写了一个简单的TCP / IP服务器,该服务器侦听端口3000,并在接收到连接时产生一个线程。 That new thread sits and reads data. 该新线程将坐下并读取数据。 My client application creates 100 connections to this server, and, using the tread pool, each connection gets a chance to send a unique message to the server. 我的客户端应用程序创建了到该服务器的100个连接,并且使用活动池,每个连接都有机会向服务器发送唯一的消息。

The server doesn't respond to the message - it only prints it to the screen. 服务器不响应该消息-它仅将其打印到屏幕上。 So, the thing I am seeing is the listening thread in the server will sometimes read messages from several different clients in the same read. 因此,我所看到的是服务器中的侦听线程有时会在同一读取中读取来自多个不同客户端的消息。

I was under the impression that TCP would separate each message to allow each thread in the server to read the message sent to it, not to all others as well. 我给人的印象是,TCP将分隔每个消息,以允许服务器中的每个线程读取发送给它的消息,而不是其他所有消息。 Am I mistaken in my understanding, or do I have something else going on in my code? 我是否理解错误,或者代码中还有其他问题?

You have something else in your code. 您的代码中还有其他内容。 Neither TCP or UDP will receive from multiple clients in the same read. TCP或UDP都不会在同一读取中从多个客户端接收到。

I would take a look at my buffer handling. 我来看看我的缓冲区处理。 Do post some server code regarding the reads. 不要发布有关读取的一些服务器代码。

Thanks. 谢谢。 I figured it out. 我想到了。 My client threads were executing much more often than I the read threads on the server. 我的客户端线程执行的频率比服务器上的读取线程执行的频率高得多。 As a result, each read often contained the data from multiple writes. 结果,每次读取通常包含来自多次写入的数据。

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

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