ObjectInputStream #readObject抛出java.io.EOFException

时间:2017-03-05 18:04:50

标签: java sockets

我在尝试从客户端返回服务器时发送数据(它可能被视为一种POST,虽然我没有使用http)时得到java.io.EOFException

我的客户端源代码:

Socket clientSocket = new Socket(serverIpAddr, IConstant.ServerPort);
OutputStream out = clientSocket.getOutputStream();
out.write(dataByteArray);
out.flush();
if(out!=null)
    out.close();
if(clientSocket!=null)
    clientSocket.close();

我的服务器源代码:

try{    
      ServerSocket serverSocket = new ServerSocket(IConstant.ServerPort);
      while(true)
      {
           Socket connectionSocket = serverSocket.accept();
           ObjectInputStream ois = new ObjectInputStream(connectionSocket.getInputStream());
           Object obj = ois.readObject();
           byte[] bytes = (byte[])obj;
           System.out.println("A notification has been received");
           //process bytes
           //...
       }
} catch(IOException ex){
     ex.printStackTrace();  
} catch (ClassNotFoundException e) {
     e.printStackTrace();
}

这就是我如何构建我的dataByteArray对象(其中PlayList是一个实现java.io.Serializable的POJO):

private byte[] serializePlayList(PlayList playList) throws IOException{
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] byteArray = null;
        ObjectOutput out = null;
        try {
          out = new ObjectOutputStream(bos);   
          out.writeObject(playList);
          out.flush();
          byteArray = bos.toByteArray();
        } finally {
          try {
            bos.close();
          } catch (IOException ex) {
          }
        }
        return byteArray;
    }

每当服务器尝试执行ois.readObject()时,都会抛出java.io.EOFException。有人可以解释一下原因吗?

3 个答案:

答案 0 :(得分:0)

您应该在playList中发送Object

客户端:

Socket clientSocket = new Socket(serverIpAddr, IConstant.ServerPort);
ObjectOutputStream out = new ObjectOutputStream(clientSocket.getOutputStream();
out.writeObject(playlist);

服务器:

ServerSocket serverSocket = new ServerSocket(IConstant.ServerPort);
Socket connectionSocket = serverSocket.accept();
ObjectInputStream ois = new ObjectInputStream(connectionSocket.getInputStream());
PlayList playList = (PlayList) ois.readObject();

答案 1 :(得分:0)

您可以使用线程并启动线程,即空间类中的线程

像这样:

public class receive  extends Thread{
public   Socket socket = null;
ObjectInputStream din = null;
data_socket respon = null ;
data_socket dtsk = new data_socket();

public receive(Socket skt) {
        this.socket=skt;

     }
@Override
public void run(){
    try {

        din = new ObjectInputStream(this.socket.getInputStream());
        while(true){

                respon=(data_socket)din.readObject();

            switch(respon.action){
                case "login"             : this.check_login(); break;
                case "afeef"             : this.afeef();break;

                default                  : System.out.println("unknow action");
            }
        }
    } catch (IOException ex) {
        System.out.println("::::::::::::::::::ERROR 1::::::::::::::::"+ex.toString());
        System.out.println("You r Lost connect to server ");
       System.exit(0);
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
        System.out.println("::::::::::::::::::ERROR 2::::::::::::::::"+e.getException());
    }
}

答案 2 :(得分:-1)

  

我在尝试发送数据时收到BLANK

不,你不是。您在尝试接收数据时获得了它。

您的发送代码没有意义。您应该将java.io.EOFException对象直接序列化到现有的PlayList,而不是所有这些带有ObjectOutputStream的恶作剧和每个发送的新对象流。

因此,您应该将ByteArrayOutputStreams的结果直接投射到readObject()

读取代码应循环直到PlayList被捕获,这意味着对等方关闭了连接。

NB EOFExceptionclientSocket在您测试它们的点上不可能为空。