good day
Task: send an object from the client bytes, get on the server and send a response.
Problem: The client sends, the server receives but does not process and does not send back the response. freezes while waiting ...
If you remove the response from the server for the client and simply convert the object back-to-byte, everything works
Maybe someone came across ... what's the problem?
Server
public void receiveData() throws IOException, ClassNotFoundException { ServerSocket serverSocket = null; try { serverSocket = new ServerSocket(settingsConnection.getPort()); } catch (IOException e) { e.printStackTrace(); } System.out.println("port " + serverSocket.toString()); System.out.println("Wait..."); System.out.println(""); Socket socket = null; try { socket = serverSocket.accept(); System.out.println("socket " + socket.toString()); InputStream inputStream = null; OutputStream outputStream = null; try { inputStream = socket.getInputStream(); outputStream = socket.getOutputStream(); } catch (IOException e) { e.printStackTrace(); } DataInputStream dataInputStream = new DataInputStream(inputStream); DataOutputStream dataOutputStream = new DataOutputStream(outputStream); Object o; while (true) { byte[] result = IOUtils.readFully(dataInputStream, -1, false); byte[] bytes = encryption.description(result); o = serializer.deserialize(bytes); Today today = (Today) o; System.out.println("input stream " + today.getVal()); Today today1 = new Today(); today1.setVal(1313); dataOutputStream.write(serializer.serialize(today1)); dataOutputStream.flush(); System.out.println("output stream sent"); outputStream.close(); inputStream.close(); socket.close(); serverSocket.close(); } } catch (IOException e) { e.printStackTrace(); }
Customer
public void sendTo(Object o) { InetAddress ipAddress = null; try { ipAddress = InetAddress.getByName(client.getAddressServer()); } catch (UnknownHostException e) { e.printStackTrace(); } Socket socket = null; try { socket = new Socket(ipAddress, client.getPortServer()); System.out.println("socket " + socket.toString()); } catch (IOException e) { e.printStackTrace(); } InputStream inputStream = null; OutputStream outputStream = null; try { if (socket != null) { inputStream = socket.getInputStream(); outputStream = socket.getOutputStream(); } } catch (IOException e) { e.printStackTrace(); } DataInputStream dataInputStream = new DataInputStream(inputStream); DataOutputStream dataOutputStream = new DataOutputStream(outputStream); int count = 0; while (true) { try { dataOutputStream.write(encryption.encription(serialize((Serializable) o))); outputStream.flush(); System.out.println("sent data to server"); byte[] result = IOUtils.readFully(dataInputStream, -1, false); byte[] bytes = encryption.decription(result); Object o1 = deserialize(bytes); Today today = (Today) o1; System.out.println("input stream " + today.getVal()); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } }
Conclusion
public static void main(String[] args) { SpringApplication.run(SocketTestTwoApplication.class, args); } @PostConstruct public void init() { Today today = new Today(); today.setVal(50011); clientService.sendTo(today); System.out.println("today " + today.getVal()); }
Threat ... Today's objects for test purposes
Thanks in advance