'Send client side data Queue to server side through Tcp/IP

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



Solution 1:[1]

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

Solution 2:[2]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Community