Java sockets - Custom Objects Sending

前端 未结 2 907
陌清茗
陌清茗 2021-01-24 23:49

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 cod

2条回答
  •  醉梦人生
    2021-01-25 00:26

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

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

    Client Code:

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

    Server Code:

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

提交回复
热议问题