我正在尝试使用InputStreamReader来读取套接字发送的字节 我有
InputStreamReader in = new InputStreamReader(socket.getInputStream());
in.read();
这会抛出一个IOException :(。我知道套接字应该发送一些东西,但它一直在抛出错误。
答案 0 :(得分:0)
实际的错误消息和堆栈跟踪确实很有用。
您可以尝试使用BufferedReader并使用readLine来确保获得某些内容。enter link description here
答案 1 :(得分: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
答案 2 :(得分:0)
这是预期的行为,IOException表示没有要读取的内容,很可能是套接字未正确连接。这个问题应该关闭。