首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Exception >

socket通信中报java.io.EOFException异常

2014-01-26 
socket通信中报java.io.EOFException异常。最近在做一个SOCKET通信程序。如下: public static void main(Stri

socket通信中报java.io.EOFException异常。最近在做一个SOCKET通信程序。如下:

public static void main(String[] args) {
try
  Socket telSocket = new Socket(host, port);
  DataInputStream tel_in = new DataInputStream(telSocket.getInputStream());
  OutputStream tel_out = telSocket.getOutputStream();
//////////////一连接上后就发送一个请求指令,这样服务端就会回送信息
  String note = "ask readyok " + "\n ";
  tel_out.write(note.getBytes());
  tel_out.flush();
  System.out.println( "send note is ok! ");
////当连接保持的时候进行收发信息
  while (!telSocket.isClosed()) {
  tel_len = tel_in.readInt();
  System.out.println(tel_len);
  if (tel_len > 4092) {
  tel_len = 4092;
  }
  byte buf[] = new byte[tel_len];

  System.out.println( "read is ok! ");///接收信息OK
  tel_in.read(buf);
  String revice = new String(buf).trim();
  System.out.println(revice);///打印显示出接收的信息
 
  ///每收到信息后就要向服务端发送一条心跳确认
  String heard = "heard is jump "+ "\n ";
  tel_out.write(heard.getBytes());
  tel_out.flush();
  System.out.println( "send heard is ok! "+heard);
  }
}
catch (Exception e) {         

热点排行