I want to transfer an object from a client to a server, I found a bunch of examples, I realized that I needed an object stream and what method to call.

SERVER

startB.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { thread = new Thread(new Runnable() { public void run() { try { int i = 0; // счётчик ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠΉ server = new ServerSocket(6666, 0, InetAddress.getByName("localhost")); System.out.println("server is started"); // ΡΠ»ΡƒΡˆΠ°Π΅ΠΌ ΠΏΠΎΡ€Ρ‚ while (true) { // ΠΆΠ΄Ρ‘ΠΌ Π½ΠΎΠ²ΠΎΠ³ΠΎ ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΡ, послС Ρ‡Π΅Π³ΠΎ запускаСм ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚ΠΊΡƒ ΠΊΠ»ΠΈΠ΅Π½Ρ‚Π° // Π² Π½ΠΎΠ²Ρ‹ΠΉ Π²Ρ‹Ρ‡ΠΈΡΠ»ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹ΠΉ ΠΏΠΎΡ‚ΠΎΠΊ ΠΈ ΡƒΠ²Π΅Π»ΠΈΡ‡ΠΈΠ²Π°Π΅ΠΌ счётчик Π½Π° Π΅Π΄ΠΈΠ½ΠΈΡ‡ΠΊΡƒ new NewConnection(i, server.accept()); i++; System.out.println(server.getInetAddress()); } } catch (Exception e) { } // Π²Ρ‹Π²ΠΎΠ΄ ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠΉ } }); thread.setDaemon(true); thread.start(); } }); public class NewConnection extends Thread { ClientsMessage msg = null; Socket s; int num; NewConnection(int num, Socket s) { this.num = num; this.s = s; setDaemon(true); setPriority(NORM_PRIORITY); start(); System.out.println("Got client"); try { ServerProgram.inStr = s.getInputStream(); ServerProgram.inObject = new ObjectInputStream(ServerProgram.inStr); } catch (Exception e) { e.printStackTrace(); } } public void run() { while(true) { try{ msg = (ClientsMessage) ServerProgram.inObject.readObject(); System.out.println(msg.name + " " + msg.surname); } catch (Exception e) { e.printStackTrace(); } } } 
  • Look retrofit, everything is very good there with serialization / deserialization square.imtqy.com/retrofit - rjhdby
  • one
    Hammer on standard Java serialization. Use third-party libraries: xstream, jackson, gson, avro, kryo, protobuf - Nofate ♦
  • Yes, and how does your client-server work? Naked sockets? - Nofate ♦
  • Here it will be necessary to read about new technologies, I just just started. Well, sockets are not bare and it is already possible to transfer the object but very crookedly. Now the ladies code is AlexanderBogomaz
  • Now I try to make the server notice my object in time, otherwise a bunch of ExecΓ©ns with a monitor and zero links crashes, because while I enter the data (I am logged in) there is no object, how can I wait? - AlexanderBogomaz

0