Windows 기반 채팅 응용 프로그램을 개발 중입니다. 클라이언트가 먼저 Command 클래스를 보내면 서버는이를 처리하고 다른 Command 클래스를 보내 클라이언트를 확인합니다. 개체를 deserialize 할 때 프로그램이 응답하지 않는다
는다 잘 단절 될 때까지 승인을 다시 보내 간다 (I 프로그램의 흐름을 발견 할 수있는 코드 세그먼트 번호가있다). 코드가 클라이언트 (5)에서 실행되어 deserialize하고 승인 사본을 가져 오면 클라이언트 프로그램이 응답하지 않습니다. 그러나 서버 (6.)의 코드가 작동하는 것처럼 보입니다. 명령을 성공적으로 직렬화합니다.
누구나 여기서 잘못된 점을 지적 할 수 있습니까?
미리 감사드립니다.
서버 코드 :
//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();
}
클라이언트 코드 :
//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.
안녕하세요. 문제가있는 것은 확실하지 않지만 데이터를 보내고받을 때 NetworkStream을 닫아야합니다. TcpClient를 닫으면 NetworkStream이 해제되지 않습니다. – Cybermaxs
@Cybermaxs는 귀하의 의견을 보내 주셔서 감사합니다. 그러나 "보내는 것을 통할 때"란 의미는 무엇입니까? 명령을 보낸 후에 매번 NetworkStream을 닫아야합니까? – manas
질문을 명확하게 말할 수 있습니까? –