使用 ObjectInputStream 通过套接字发送数组

Send array via a socket using ObjectInputStream

我正在尝试通过套接字发送字符串数组的内容,然后在另一端打印出数组项。

我一直在尝试使用 ObjectInputStream,但没有成功。老实说,我已经在网上搜索过了,但我仍然不知道自己在做什么。

客户端

正在将对象放入数组

String[] sendServer = new String [4];

{
    sendServer[0] = txtUsername.getText();
    sendServer[1] = dateFormat.format(date);
    sendServer[2] = txtMessageOut.getText();
    sendServer[3] = "I HOPE THIS WORKS!!!";
}

套接字已通过其他方法创建,我可以退出,只是不确定是否正确?

ObjectOutputStream objectStream = new ObjectOutputStream(clientSocket.getOutputStream());
objectStream.writeObject(sendServer);

System.out.println(sendServer[0]);
System.out.println(sendServer[1]);
System.out.println(sendServer[2]);

对于服务器我一无所知。我一直在尝试使用 ObjectInputStream 但没有成功。服务器上的套接字名为 socket.

任何帮助或指点将不胜感激。

我想给你一段代码。发送方和接收方使用套接字进行通信。将 Request 替换为您的数据结构并删除不需要的代码行。

这是发件人:

private Response doUnicastTCP(Server remoteServer, Request request) throws ServerException {
                Socket clientSocket = null;
                ObjectOutputStream outToServer = null;
                ObjectInputStream inFromServer = null;
                try {
                    if (isStarted()) {
                        request.setRequestType(RequestType.UNICAST_TCP);
                        // LOGGER.debug("Sending " + request.getRequestType() + " "
                        // + request);
                        clientSocket = new Socket(remoteServer.getHost(), remoteServer.getUnicastTCPPort());
                        clientSocket.setSoTimeout(request.getTimeout());
                        outToServer = new ObjectOutputStream(clientSocket.getOutputStream());
                        inFromServer = new ObjectInputStream(clientSocket.getInputStream());
                        outToServer.writeObject(request);
                        Response response = (Response) inFromServer.readObject();
                        // LOGGER.debug("Received " + request.getRequestType() + " "
                        // + response);
                        if (response.getReturnValue() instanceof Exception) {
                            throw new ServerException((Exception) response.getReturnValue());
                        }
                        return response;
                    } else {
                        throw new ServerNotStartedException();
                    }
                } catch (SocketTimeoutException e) {
                    throw new ServerException("cant execute request " + request + " on server " + remoteServer + " "
                            + e.toString());
                } catch (ClassNotFoundException | IOException e) {
                    throw new ServerException("cant execute request " + request + " on server " + remoteServer + " "
                            + e.toString());
                } finally {
                    try {
                        if (clientSocket != null) {
                            clientSocket.close();
                        }
                    } catch (IOException e) { //
                        LOGGER.trace("socket couldn't be closed");
                    }
                }
            }

我是收件人:

public void run() {
            Request r = null;
            try {
                ObjectInputStream inFromClient = new ObjectInputStream(s.getInputStream());
                ObjectOutputStream outToClient = new ObjectOutputStream(s.getOutputStream());
                while (isStarted()) {
                    final Object receivedObject = inFromClient.readObject();
                    // LOGGER.debug("Receiving "
                    // + ((Request) receivedObject).getRequestType() + " "
                    // + receivedObject);
                    r = (Request) receivedObject;
                    processId.set(r.getProcessId());
                    Response rs = new Response();
                    rs.setRequest(r);
                    rs.setServerFrom(GoldenNodeServer.this);
                    if (getOperationBase() != null) {
                        try {
                            Object s = ReflectionUtils.callMethod(getOperationBase(), r.getMethod(), r.getParams());
                            rs.setReturnValue(s);
                        } catch (Exception e) {
                            rs.setReturnValue(e);
                        }
                        outToClient.writeObject(rs);
                    } else {
                        rs.setReturnValue(new NoClientProxySetException());
                    }
                }
            } catch (EOFException e) {
                // LOGGER.trace("eof occured");
            } catch (SocketException e) {
                if (e.toString().contains("Socket closed") || e.toString().contains("Connection reset")
                        || e.toString().contains("Broken pipe")) {
                } else {
                    stop();
                    LOGGER.error("Error occured" + (r == null ? "" : " while processing " + r) + " ", e.toString());
                }
            } catch (IOException | ClassNotFoundException e) {
                stop();
                LOGGER.error("Error occured" + (r == null ? "" : " while processing " + r) + " ", e.toString());
            } finally {
                tcpProcessors.remove(this);
            }
        }