通过Tcp/IP将客户端数据队列发送到服务器端
我想将数据从客户端发送到服务器。有两个队列。在客户端和服务器端。我希望我的客户端连接到服务器并将客户端队列中的所有数据发送到服务器。在服务器端,我想接受所有客户端并获取所有对象并添加到服务器端队列
客户端代码:
Queue<Person> clientQueue; // This is the client side queue
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 15884);
var client = new TcpClient();
while(!clientQueue.IsEmpty)
{
Person personObj = clientQueue.Dequeue();
client.Connect(serverEndPoint);
NetworkStream clientStream = client.GetStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(clientStream, personObj);
clientStream.Flush();
}
服务器端:
Queue<Person> serverQueue; // This is the server side queue
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 15884);
TcpListener tcpListener = new TcpListener(ipEndPoint);
while(true)
{
TcpClient client = tcpListener.AcceptTcpClient();
NetworkStream clientStream = tcpClient.GetStream();
BinaryFormatter bf = new BinaryFormatter();
Person person = (Person)bf.Deserialize(clientStream);
tcpClient.Close();
serverQueue.Enqueue(person);
}
我知道上面的代码不起作用。我只是想画出我的设计草图。有人可以给我发送代码示例吗?如何将客户端队列发送到服务器队列..
谢谢..
I wanna send data from client to server. There are two queues. in client side and in server side. I want to my client to be connected to the server and send all the data in client queue to the server. In server side I wanna accept all the clients and get all objects and add to the server side queue
Client code :
Queue<Person> clientQueue; // This is the client side queue
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 15884);
var client = new TcpClient();
while(!clientQueue.IsEmpty)
{
Person personObj = clientQueue.Dequeue();
client.Connect(serverEndPoint);
NetworkStream clientStream = client.GetStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(clientStream, personObj);
clientStream.Flush();
}
Server Side :
Queue<Person> serverQueue; // This is the server side queue
IPEndPoint ipEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 15884);
TcpListener tcpListener = new TcpListener(ipEndPoint);
while(true)
{
TcpClient client = tcpListener.AcceptTcpClient();
NetworkStream clientStream = tcpClient.GetStream();
BinaryFormatter bf = new BinaryFormatter();
Person person = (Person)bf.Deserialize(clientStream);
tcpClient.Close();
serverQueue.Enqueue(person);
}
I know above code is not working. I just wanna sketch my design. Can someone please send me the code example. How to send client queue to server queue..
Thanks..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于服务器端和客户端的队列,如果您使用
.net 4.0
,则应使用BlockingCollection
,对于早期版本,请使用的组合队列
和AutoResetEvent
。检查这个。在服务器端,您应该使用异步方法或仅实例化一个新线程来处理每个新客户端,然后在该线程读取数据。像:
编辑:您在评论中指出:
数组或队列本身是一个对象:
For the queue at both the server and the client side you should use
BlockingCollection
if you are using.net 4.0
, for earlier versions use a combination ofQueue
andAutoResetEvent
. check this.At server side you should use asynchronous methods or just instantiate a new thread to handle each new client and then read the data at that thread. like:
Edit: You stated at a comment:
Array or Queue itself is an object:
好的。最后我通过一些调查/测试找到了我的问题的答案。我将在这里为其他人发布它..:)
首先在我的客户端,我必须发送我要发送到服务器的字节数。然后发送该数据..像这样..
在服务器端:
Ok. Finally I found the answer for my question doing some investigations/Testings. I ll post it here for someone else.. :)
In my client side first I have to send how much bytes im gonna send to server. and then send that data.. Like this..
In Server Side :