클라이언트와 서버간에 매우 간단한 TCP/IP 통신 프로그램을 만들고 있습니다. 목표는 그들 사이의 문자열을 교환하는 것입니다.클라이언트 측의 TCP/IP 통신 중에 루프가 영원히 정지합니까?
클라이언트 서버 쪽에서 서버에 간단한 문자열을 보내고 받아야합니다.
문제는 StreamWriter를 사용하여 Sever에 데이터를 보낼 때 완벽하게 작동한다는 것입니다. 그러나 StreamReader를 활성화하면 while 루프가 그냥 매달려 있습니다. "Console.Write ("> ");" 행은 처음에 한 번만 실행되지만 두 번째에는 다시 프롬프트되지 않습니다.
다음은 아래 코드입니다.
참고 "_client"는 코드의 다른 부분에 TcpClient _client로 선언 된 TcpClient의 소켓에 이미 연결되어 있습니다.
_sReader = new StreamReader(_client.GetStream(), Encoding.ASCII);
_sWriter = new StreamWriter(_client.GetStream(), Encoding.ASCII);
_isConnected = true;
String sData = null;
while (_isConnected)
{
Console.Write("> ");
sData = Console.ReadLine();
_sWriter.WriteLine(sData);
_sWriter.Flush();
//If I commnet out these two lines of code below, then there is no problem. Program works fine.
String sDataIncomming = _sReader.ReadLine();
Console.WriteLine(sDataIncomming);
//While loop just hanging after this line.
//It does not even break the while loop.
//Either while loop goes back to the top of the line.
//Just stuck here. Why ?
}
StreamReader에 대한 두 줄의 코드를 주석 처리하면 프로그램이 다시 완벽하게 작동합니다.
이 문제를 어떻게 해결할 수 있습니까? 당신의 지식을 공유하는 것은 정말로 감사 할 것입니다.
친절하게 제공합니다.