不知道为什么,readInt就是不能用,同样的还有readChar(),相信其他的也不行,不过read却能用。其他都会抛出这个异常
Exception in thread "main" java.io.EOFException
at java.io.DataInputStream.readChar(Unknown Source)
at TestSocket.TestServer1.main(TestServer1.java:17)
试过sleep也不行,到底是为什么呢?
附代码:
ServerSocket:
public class TestServer1 {
public static void main(String[] args) throws IOException {
System.out.println("开机了");
ServerSocket ss = new ServerSocket(6666);
Socket s = ss.accept();
System.out.println("人来啦");
DataInputStream dis = new DataInputStream(s.getInputStream());
System.out.println(dis.readInt());
dis.close();
}
}
Client:
public class TestClient1 {
public static void main(String[] args) throws IOException {
Socket s = new Socket("127.0.0.1",6666);
OutputStream os = s.getOutputStream();
DataOutputStream dos = new DataOutputStream(os);
dos.write(21);
dos.flush();
dos.close();
s.close();
}
}
后记:终于知道了,write是将参数的低8位写出去,而接受是int(4个字节),若改成writeInt,就行了。
好像我之前不是报这个错误的,是SockedException,怎么无法呈现了?