Pregunta

I am developing a windows based chat application. When the client first sends the Command class, server gets it processes it and acknowledges the client by sending another Command class.

(I have numbered the code segment to spot the flow of the program)

Everything goes well until the sever send back the acknowledgment. When the code runs in client (5.) to deserialize and get the copy of the acknowledgment, the client program goes unresponsive. But the code in the server (6.) seems working - it serializes the command succesfully.

Can anyone point out whats wrong here?

Thanks in advance.

Server Code:

//1. Server runs first
try
{
    BinaryFormatter binaryFormatter = new BinaryFormatter();

    //2. Server is blocked here waiting for an incoming stream
    Command newCommand = (Command)binaryFormatter.Deserialize(networkStream);
}
catch (Exception ex)
{
    MessageBox.Show("EXCEPTION: " + ex.Message);
    Console.WriteLine(ex.Message);
}

Client c = new Client(newCommand.ClientName, endPoint,
                                        clientServiceThread, client);

// ...processing the newCommand object

Command command = new Command(CommandType.LIST);

try
{
    TcpClient newTcpClient = new TcpClient(newClient.Sock.RemoteEndPoint
                                                           as IPEndPoint);
    newTcpClient.Connect(newClient.Sock.RemoteEndPoint as IPEndPoint);
    NetworkStream newNetworkStream = newTcpClient.GetStream();
    BinaryFormatter binaryFormatter = new BinaryFormatter();

    //6. Server serializes an instance of the Command class to be recieved by the client
    binaryFormatter.Serialize(newNetworkStream, command);
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message, "Error");
    Console.WriteLine(ex.Message);
    newClient.Sock.Close();
    newClient.CLThread.Abort();
}

Client Code:

//3. Client runs second
TcpClient tcpClient = new TcpClient('localhost', 7777);
NetworkStream networkStream = tcpClient.GetStream();

Command newCommand = new Command(CommandType.CONN);

try
{
    BinaryFormatter binaryFormatter = new BinaryFormatter();

    //4. Client serializes an instance of a Command class to the NetworkStream
    binaryFormatter.Serialize(networkStream, newCommand);
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}


BinaryFormatter binaryFormatter = new BinaryFormatter();

//5. Then client is blocked until recieve an instance of command class to deserialize
Command serverResponse = (Command)binaryFormatter.Deserialize(networkStream);

clientForm.updateChatMessages(serverResponse);

//7. Instead of recieving the instance of the Command class, the clients go unresponsive
//   and the client program hangs.
¿Fue útil?

Solución

I figured it out. Since the server was servicing multiple clients, I made a mistake of deserializing from the same NetworkStream instance. So I changed the code to create a new NetworkStream by providing the client's socket each time I want the server to send a message.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top