2017-12-20 2 views
-1

이 문제가 있습니다. 로컬 네트워크에 연결된 저울에서 무게를 읽어야합니다. 하이퍼 터미널에서 나는 포트, 열려있는 연결을 가진 IP에 연결하고, 나는 "XN"이라는 문자열을 입력하고, 스케일은 문자열로 가중치가있는 하이퍼 터미널에 새로운 라인을 작성한다. 소켓을 사용하여 코드에서 동일한 작업을 시도했지만 반환 결과를 얻을 수 있습니다. 나를 돕기 위해 나를 도와 줄 수 있습니까? 이것은 내가 성공하지 못한 코드입니다.Java 소켓에서 응답을받을 수 없습니다.

Socket echoSocket = null; 
DataOutputStream os = null; 
DataInputStream is = null; 
try 
{ 
    echoSocket = new Socket(IpBilancia, Porta); 
    os = new DataOutputStream(echoSocket.getOutputStream()); 
    is = new DataInputStream(echoSocket.getInputStream()); 
} 
catch (UnknownHostException e) { 
    System.err.println("Don't know about host: " + IpBilancia); 
} 
catch (IOException e) 
{ 
    System.err.println("Couldn't get I/O for the connection to" + IpBilancia); 
} 
    try 
    { 
     os.writeBytes("XN"); 
     os.writeByte('\n'); 
     os.flush(); 
     BufferedReader input = new BufferedReader(new InputStreamReader(is)); 
     String Peso; 
     while ((Peso = input.readLine()) != null) 
     { 

      System.out.println("echo: " + is.readLine()); 
     } 
     os.close(); 
     is.close(); 
     echoSocket.close(); 
    } catch (IOException e) { 
     System.err.println("I/O failed on the connection to" + IpBilancia); 
    } 

EDIT1 : while 루프에 붙어

Socket echoSocket = null; 
DataOutputStream os = null; 
DataInputStream is = null; 
try 
{ 
    echoSocket = new Socket(IpBilancia, Porta); 
    os = new DataOutputStream(echoSocket.getOutputStream()); 
    is = new DataInputStream(echoSocket.getInputStream()); 
} 
catch (UnknownHostException e) { 
    System.err.println("Don't know about host: " + IpBilancia); 
} 
catch (IOException e) 
{ 
    System.err.println("Couldn't get I/O for the connection to" + IpBilancia); 
} 
try 
{ 
    os.writeBytes("XN"); 
    os.writeByte('\n'); 
    os.flush(); 
    BufferedReader input = new BufferedReader(new InputStreamReader(is)); 
    String Peso; 
    while ((Peso = input.readLine()) != null) 
    { 

     System.out.println("echo: " + is.readLine()); 
    } 
    os.close(); 
    is.close(); 
    echoSocket.close(); 
} catch (IOException e) { 
    System.err.println("I/O failed on the connection to" + IpBilancia); 
} 

디버그 숙박. 그것은 루프에 들어 가지 않습니다. 여기에서 중지 while ((Peso = input.readLine())! = null) 아무 것도하지 않습니다.

+3

'while' 루프 안에서'is.readLine()'을하고 있으므로 ** ** 두 번 읽으므로 한 줄만'os'에 쓰여집니다. 당신은 뜻 :'System.out.println ("echo :"+ Peso);'대신에? – Jesper

+0

예. 그러나 문제는 input.readLine()이 비어 있다는 것입니다. 나는 왜 이것을 이해하지 못한다. 하이퍼 터미널을 사용하면 결과가 나타납니다. 도와 주셔서 감사합니다 – mmauro

+1

@ Jesper 코멘트를 무시 했으므로 다시 말씀 드리겠습니다. 루프에서'is.readLine()'을 두 번 호출하고 ** 처음 is.readLine() **의 결과를 버립니다. –

답변

1

다음 코드는 netcat을 더미 서버 (예스퍼의 메모 코드)와 함께 잘 작동 :

try 
{ 
    os.writeBytes("XN"); 
    os.writeByte('\n'); 
    os.flush(); 
    BufferedReader input = new BufferedReader(new InputStreamReader(is)); 
    String peso; 
    while ((peso = input.readLine()) != null) 
    { 
     System.out.println("echo: " + peso); 
    } 
    os.close(); 
    is.close(); 
    echoSocket.close(); 
} catch (IOException e) { 
    System.err.println("I/O failed on the connection to" + IpBilancia); 
} 

당신은 수동으로 서버를 시도하는 하이퍼 터미널을 사용하여, 따라서 나는 당신의 윈도우에 있는지 supose.

아마도 하이퍼 터미널의 구성에서 CRLF를 줄 끝 (End Of Line)으로 사용합니까?

이 경우, 사용하려고 :

os.writeBytes("XN"); 
os.writeByte('\r'); 
os.writeByte('\n'); 

는 자바 클라이언트와 같은 EOL을 가지고.

+0

감사! 그게 내 문제를 일으켰다! – mmauro

관련 문제