2011-03-22 176 views
0

我試圖用一個InputStreamReader讀通過插口發送的字節 我InputStreamReader中拋出IOException異常

InputStreamReader in = new InputStreamReader(socket.getInputStream()); 
in.read(); 

此拋出IOException :(。我知道的插座應送的東西,但它不斷拋出錯誤。

+4

你能提供的錯誤信息和堆棧跟蹤? – Greg 2011-03-22 20:53:56

回答

0

實際的錯誤信息和堆棧跟蹤確實是有用的。

你可以嘗試使用一個BufferedReader和的readLine使用,以確保你得到的東西。enter link description here

0
import java.io.*; 
import java.net.*; 

public class EchoClient { 
    public static void main(String[] args) throws IOException { 

     Socket echoSocket = null; 
     PrintWriter out = null; 
     BufferedReader in = null; 

     try { 
      echoSocket = new Socket("taranis", 7); 
      out = new PrintWriter(echoSocket.getOutputStream(), true); 
      in = new BufferedReader(new InputStreamReader(
             echoSocket.getInputStream())); 
     } catch (UnknownHostException e) { 
      System.err.println("Don't know about host: taranis."); 
      System.exit(1); 
     } catch (IOException e) { 
      System.err.println("Couldn't get I/O for " 
           + "the connection to: taranis."); 
      System.exit(1); 
     } 

    BufferedReader stdIn = new BufferedReader(
            new InputStreamReader(System.in)); 
    String userInput; 

    while ((userInput = stdIn.readLine()) != null) { 
     out.println(userInput); 
     System.out.println("echo: " + in.readLine()); 
    } 

    out.close(); 
    in.close(); 
    stdIn.close(); 
    echoSocket.close(); 
    } 
} 

發現here