Socket 应用中DataInputStream 的readInt()方法

不知道为什么,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,怎么无法呈现了?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值