通过Tcp/IP将客户端数据队列发送到服务器端

发布于 2024-11-25 12:02:04 字数 1224 浏览 4 评论 0原文

我想将数据从客户端发送到服务器。有两个队列。在客户端和服务器端。我希望我的客户端连接到服务器并将客户端队列中的所有数据发送到服务器。在服务器端,我想接受所有客户端并获取所有对象并添加到服务器端队列

客户端代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巴黎夜雨 2024-12-02 12:02:04
  1. 对于服务器端和客户端的队列,如果您使用 .net 4.0,则应使用 BlockingCollection,对于早期版本,请使用 的组合队列AutoResetEvent。检查这个

  2. 在服务器端,您应该使用异步方法或仅实例化一个新线程来处理每个新客户端,然后在该线程读取数据。像:

    TcpClient 客户端 = tcpListener.AcceptTcpClient();
    ThreadPool.QueueUserWorkItem(new WaitHandle(HandleCleint), client);
    
    私有无效HandleClient(对象clientObject)
    {
        TcpClient 客户端 = (TcpClient)clientObject;
        NetworkStream clientStream = client.GetStream();
        //这里处理数据
    }
    

编辑:您在评论中指出:

我不知道如何更改我的程序以将整个队列发送到服务器端

数组或队列本身是一个对象:

//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. 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 of Queue and AutoResetEvent. check this.

  2. 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:

    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
    }
    

Edit: You stated at a comment:

I don't have an idea about how to change my program to send an entire queue to server side

Array or Queue itself is an object:

//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);
蛮可爱 2024-12-02 12:02:04

好的。最后我通过一些调查/测试找到了我的问题的答案。我将在这里为其他人发布它..:)

首先在我的客户端,我必须发送我要发送到服务器的字节数。然后发送该数据..像这样..

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();
}

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..

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();

In 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);
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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文