Tcp Server为几个客户端数据冲突? [英] Tcp Server for several clients data conflict ?

查看:91
本文介绍了Tcp Server为几个客户端数据冲突?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个接收来自多个客户端的数据的TCP服务器。


代码:


Socket clientSocket = server。 AcceptSocket(); // server是一个TcpListener

对象。


现在我启动一个处理来自

的clientSocket接收数据的线程客户端,我回去等服务器.AcceptSocket()上面的

新客户端,等等。


麻烦就是那个线程处理一个客户端接收从另一个客户端发送的数据。这对我不好!

对于每个接受的套接字,RemoteEndPoint是不同的但是

LocalEndPoint是一样的,我觉得这是麻烦,但我不知道我

如何解决它。这是服务器必须在单个端口上侦听,但是应该同时处理多个套接字连接。我猜每个新的插座

应该移到另一个端口,但是我发现没办法这样做。

有人可以帮我们吗?


---------

谢谢

Sharon

I''m writing a TCP server that receive data from several clients.

The code:

Socket clientSocket = server.AcceptSocket(); // server is a TcpListener
object.

And now I start a thread that handle the clientSocket received data from the
client, and I going back to wait on the server.AcceptSocket() above for the
new client, and so on.

The trouble is that thread that handle one client receive data sent from
another client. And this is bad for me !
For each accepted socket the RemoteEndPoint is different but the
LocalEndPoint is the same, I think this is the trouble, but I do not know I
how to fix it. Here is the server must listen on a single port but should
handle several sockets connections at the same time. I guess every new socket
should be moved to a different port, but I found no way to do this.
Can anyone help we with that?

---------
Thanks
Sharon

推荐答案

不,那是对的。您必须在代码中做错事,服务器

意味着能够在单个插槽上接受多个连接

我没有做任何dotnet套接字的东西但是没有帮助。

你能发布一些代码吗。
Nope, that''s right. You must be doing something wrong in your code, servers
are meant to be able to accept multiple connections on a single socket
I haven''t done any dotnet socket stuff yet so can''t help.
Can you post some of your code.


好的,这是总结的代码我是使用:


//////////////////////////////////// ///////////////////////////////////////////

IPAddress DEFAULT_SERVER = IPAddress.Parse(" 127.0.0.1");

IPEndPoint ipNport = new IPEndPoint(DEFAULT_SERVER,31001);

TcpListener m_server = new TcpListener( ipNport);


while(true)//永远这个例子

{

Socket clientSocket = m_server.AcceptSocket( );

线程m_clientListenerThread =新线程(新

ThreadStart(SocketListenerThreadStart));

m_clientListenerThread.Start();

}


private void SocketListenerThreadSt art()

{

int size = 0;

byte [] byteBuffer = new byte [131072]; // 128 Kilobyte。


while(true)//永远这个例子

{

size = m_clientSocket.Receive(byteBuffer);

//错误:在这里我得到混合数据,我的意思是我从多个客户那里收到数据

!!!

} < br $>
}

////////////////////////////////// /////////////////////////////////////////////


任何想法???


------------

谢谢

Sharon
Ok, here is the summarized code I''m using:

///////////////////////////////////////////////////////////////////////////////
IPAddress DEFAULT_SERVER = IPAddress.Parse("127.0.0.1");
IPEndPoint ipNport = new IPEndPoint(DEFAULT_SERVER, 31001);
TcpListener m_server = new TcpListener(ipNport);

while( true )// For ever for this example
{
Socket clientSocket = m_server.AcceptSocket();
Thread m_clientListenerThread = new Thread(new
ThreadStart(SocketListenerThreadStart));
m_clientListenerThread.Start();
}

private void SocketListenerThreadStart()
{
int size = 0;
byte [] byteBuffer = new byte[131072];// 128 Kilobyte.

while( true )// For ever for this example
{
size = m_clientSocket.Receive(byteBuffer);
// ERROR: In here I get mixed data, I mean that I get data received
from more then one client !!!
}
}
///////////////////////////////////////////////////////////////////////////////

Any idea???

------------
Thanks
Sharon


其中是m_clientSocket组?我可以看到clientSocket在第一位但是

我无法在线程方法中看到m_clientSocket的来源。

如果有多个,请检查你选择的是否正确一个。
Where is "m_clientSocket" set? I can see "clientSocket" in the first bit but
I can''t see where the m_clientSocket comes from in the thread method.
If there are several, check you''re selecting the correct one.


这篇关于Tcp Server为几个客户端数据冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆