2011-10-22 7 views
0

나는 C# 및 해당 클라이언트의 java에서 TCP 서버를 작성하고 있습니다. localhost에서 연결을 테스트 중이며 클라이언트가 서버에 연결할 수 있습니다. 그러나 메시지를 보낼 때 클라이언트는 메시지를받지 못합니다. 디버거를 사용하여 stream.Write (...)가 실행되었는지 확인했습니다. 어떤 문제가 어떤 문제 일 수 있습니까?Java TCP 클라이언트가 C# 서버에서 보낸 메시지를받지 못합니다.

 TcpClient client = (TcpClient)cl; 
     NetworkStream stream = client.GetStream(); 

     byte[] msg = new byte[512]; 
     int bytesRead; 

     while (running) 
     { 
      while (messages.getCount() > 0) 
      { 
       String msg = messages.Take(); 

       if (cmd != null) 
       { 
        byte[] bytes = Encoding.UTF8.GetBytes(msg.ToCharArray()); 

        try 
        { 
         stream.Write(bytes, 0, bytes.Length); 
         stream.Flush(); 
        } 
        catch (Exception e) 
        { 

        } 
       } 
      } 

      Thread.Sleep(1000); 
     } 

그리고 자바 클라이언트 :

public void run() 
{ 
    try 
    { 
     socket = new Socket(address, port); 
     in = new BufferedReader(new InputStreamReader(socket.getInputStream())); 
     out = new PrintWriter(socket.getOutputStream()); 
     running = true; 
    } 
    catch (Exception e){ 
     e.printStackTrace(); 
     running = false; 
    } 

    String data; 
    while(running) 
    { 
     try 
     { 
      data = in.readLine(); 

      if(data != null) 
      { 
       processData(data); 
      } 
     } 
     catch (IOException e) 
     { 
      e.printStackTrace(); 

      running = false; 
      break; 
     } 
    } 

    try 
    { 
     socket.close(); 
     socket = null; 
    } 
    catch (IOException e) 
    { 
     e.printStackTrace(); 
    } 

    running = false; 
} 

답변

5

당신은 BufferedReader.readLine()을 사용하고

는 C# 서버입니다. 메시지 문자열이 CR, LF 또는 CR/LF로 종료 되었습니까?

readLine 줄 끝 문자가 읽힐 때까지 차단.

관련 문제