我正在尝试通过套接字发送字符串数组的内容,然后在另一端打印出数组项。
我一直在尝试使用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
。
非常感谢任何帮助或指示。
答案 0 :(得分:0)
我想给你一个代码片段。发件人和收件人使用套接字进行通信。将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);
}
}