本文介绍了Java 套接字 - 自定义对象发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created working sockets, and when I try to send text or numbers, its ok, but when im trying to send my custom class object, i got NullPointerException...Here is some code:

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;
}

实际上,我为列表编写的代码,但我想要一些类似的函数来发送其他对象.感谢您的回复!

Actually, that code I made for Lists, but I want to have some simmilar functions to send other objects.Thanks for any replies!

推荐答案

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

Code snippets below works with all kind serializable objects for me. Please check.

*IRequest 是要发送的对象.*IResponse 是要获取的对象.

*IRequest is object to send.*IResponse is object to get.

    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());
    }

这篇关于Java 套接字 - 自定义对象发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 14:46