我想从客户端向服务器发送数据。有两个队列。在客户端和服务器端。我希望我的客户端连接到服务器并将客户端队列中的所有数据发送到服务器。在服务器端,我想接受所有客户端并获取所有对象并添加到服务器端队列
客户代码:
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);
}
我知道上面的代码不起作用。我只想勾勒出我的设计。有人可以给我发送代码示例。如何将客户端队列发送到服务器队列..
谢谢..
答案 0 :(得分:4)
对于服务器和客户端的队列,如果使用BlockingCollection
,则应使用.net 4.0
,因为早期版本使用Queue
和{{AutoResetEvent
的组合1}}。检查this。
在服务器端,您应该使用异步方法或只是实例化一个新线程来处理每个新客户端,然后读取该线程上的数据。像:
TcpClient client = tcpListener.AcceptTcpClient();
ThreadPool.QueueUserWorkItem(new WaitHandle(HandleCleint), client);
private void HandleClient(object clientObject)
{
TcpClient client = (TcpClient)clientObject;
NetworkStream clientStream = client.GetStream();
//handle the data here
}
编辑:您在评论中说明了这一点:
我不知道如何更改程序以将整个队列发送到服务器端
数组或队列本身就是一个对象:
//at your client side:
bf.Serialize(clientStream, persons);//assume persons is Queue<Person>
//at your server side:
Queue<Person> persons = (Queue<Person>)bf.Deserialize(clientStream);
答案 1 :(得分:2)
确定。最后,我找到了我的问题的答案,做了一些调查/测试。我会在这里张贴给其他人.. :)
首先,在我的客户端,我必须发送我要发送给服务器的字节数。然后发送那些数据..就像这样..
Queue<Person> clientQueue; // This is the client side queue
IPEndPoint serverEndPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 15884);
var client = new TcpClient();
NetworkStream clientStream = client.GetStream()
while (disconnectClient)
{
if (clientQueue.Count > 0)
{
Person person = clientQueue.Dequeue();
using (MemoryStream memoryStrem = new MemoryStream())
{
var bf = new BinaryFormatter();
bf.Serialize(memoryStrem, person);
byte[] writeData = memoryStrem.ToArray();
byte[] writeDataLen = BitConverter.GetBytes((Int32)writeData.Length);
clientStream.Write(writeDataLen, 0, 4);
clientStream.Write(writeData, 0, writeData.Length);
}
}
}
clientStream.Dispose();
client.Dispose();
在服务器端:
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);
tcpListener.Start();
while(true)
{
TcpClient tcpClient = tcpListener.AcceptTcpClient();
NetworkStream clientStream = tcpClient.GetStream();
while (client.Connected)
{
if (client.Available >= 4)
{
try
{
byte[] readDataLen = new byte[4];
clientStream.Read(readDataLen, 0, 4);
Int32 dataLen = BitConverter.ToInt32(readDataLen, 0);
byte[] readData = new byte[dataLen];
clientStream.Read(readData, 0, dataLen);
using (var memoryStream = new MemoryStream())
{
memoryStream.Write(readData, 0, readData.Length);
memoryStream.Position = 0; /// This is very important. It took 4 hrs to identify an error because of this :)
BinaryFormatter bf = new BinaryFormatter();
Person person = (Person)bf.Deserialize(memoryStream);
serverQueue.Enqueue(person);
}
}
catch { }
}
}
tcpClient.Close();
}