Java套接字 - 自定义对象发送

时间:2012-01-12 12:12:00

标签: java sockets object arraylist

我创建了工作套接字,当我尝试发送文本或数字时,确定,但是当我尝试发送自定义类对象时,我得到了NullPointerException ... 这是一些代码:

public boolean SendLi(List<Entity> list)
{
    try {
         out.writeObject(list);
    } catch (IOException e) {
        e.printStackTrace();
        System.err.println("Send: Error on OutputStream.write(byte[])");
    }
    return true;
}

public List<Entity> RecvLi()
{
    List<Entity> data;
    data = new ArrayList<Entity>();
      try{
          data = (List<Entity>) in.readObject();
        } catch (IOException e) {
          System.err.println("Send: Error on OutputStream.read(byte[]) - IOException");
          return null;
        } catch (ClassNotFoundException e) {
          System.err.println("Send: Error on OutputStream.read(byte[]) - ClassNotFound");
            e.printStackTrace();
        }
    return data;
}

实际上,我为Lists制作的代码,但我希望有一些simmilar函数来发送其他对象。 感谢您的回复!

2 个答案:

答案 0 :(得分:1)

您需要在通过套接字发送自定义类之前对其进行序列化。您要发送的对象

out.writeObject(list);

应序列化,您的类应实现java.io.Serializable接口

答案 1 :(得分:1)

下面的代码片段适用于所有类型的可序列化对象。请检查。

* IRequest是发送对象。 * IResponse是得到的对象。

客户代码:

    Socket clientSocket = new Socket(hostName, portNo);
    clientSocket.setSoTimeout(connectionTimeout);
    try {
        OutputStream outputStream = clientSocket.getOutputStream();
        InputStream inputStream = clientSocket.getInputStream();
        ObjectOutputStream objOutputStream = new ObjectOutputStream(outputStream);
        ObjectInputStream objInputStream = null;

        try {
            objOutputStream.writeObject(request);
            objOutputStream.flush();
            objInputStream = new ObjectInputStream(inputStream);
            res = (IResponse) objInputStream.readObject();
        } finally {
            if (objOutputStream != null) {
                objOutputStream.close();
            }
            if (outputStream != null) {
                outputStream.close();
            }
            if (objInputStream != null) {
                objInputStream.close();
            }
            if (inputStream != null) {
                inputStream.close();
            }
        }
    } finally {
        clientSocket.close();
        clientSocket = null;
    }

服务器代码:

        private IMessageProcessor messageProcessor = null;
        private Socket clientSocket = null;
        private Logger logger = null;


        String clientIP = clientSocket.getInetAddress().getHostAddress();
        int clientPortNr = clientSocket.getPort();

        InputStream inputStream = clientSocket.getInputStream();
        OutputStream outputStream = clientSocket.getOutputStream();
        try {
            ObjectInputStream objInputStream = null;
            ObjectOutputStream objOutputStream = null;
            try {
                IRequest request = null;

                objInputStream = new ObjectInputStream(inputStream);
                objOutputStream = new ObjectOutputStream(outputStream);
                request = (IRequest) objInputStream.readObject();

                IResponse response = null;

                try {
                    response = messageProcessor.processMessage(clientIP,
                                                                clientPortNr,
                                                                request,
                                                                logger);
                    objOutputStream.writeObject(response);
                } catch (Exception ex) {
                    objOutputStream.writeObject(ex);
                }

                objOutputStream.flush();
                objOutputStream.reset();

            } finally {
                if (objInputStream != null)
                    objInputStream.close();
                if (objOutputStream != null)
                    objOutputStream.close();
            }
        } finally {
            inputStream.close();
            outputStream.close();
            clientSocket.close();
        }
    } catch (Exception ex) {
        logger.error(ex.getMessage());
    }