Receive an object over TCP/IP

后端 未结 3 1540
渐次进展
渐次进展 2021-01-19 23:35

I am going to write a program over TCP/IP and I should send objects by client or by server, It is going right when I want to send or receive strings but when I am trying to

相关标签:
3条回答
  • If you'd like to achieve good performance and send object then you definitely should use Google Protobuf

    It allows you to define messages in simple .proto files. Then you use bundled compiler to generate Java classes which will be serialized and sent.

    Also better idea is to use Netty over plain Java sockets. This prevent you from writing a lot of boilerplate code and define simple serialization/deserialization pipelines. Take a look at user-guide.

    0 讨论(0)
  • 2021-01-20 00:19

    Just get rid of sending and receiving the Boolean. It's redundant. If there was some problem creating the connection, the socket wouldn't get created: an exception would be thrown instead. You're confusing everything with multiple streams on the same socket. Don't do that.

    In your read-object loop, you need to catch EOFException separately, and when you get it, close the socket and exit the loop. If you get any other IOException, log it, close the socket, and exit the loop.

    0 讨论(0)
  • 2021-01-20 00:31

    If you want to send object over network you must serialize your objects.

    Check this question: How To send an object over TCP in Java

    Java Serialization: Serialization

    You could do it like this:

    import java.net.*;  
    import java.io.*;  
    
    class testobject implements Serializable {  
      int value;  
      String id;  
    
      public  testobject(int v, String s ){  
           this.value=v;  
           this.id=s;  
        }  
    
    }  
    
    public class SimpleServer  {  
      public static void main(String args[]) {  
      int port = 2002;  
      try {  
        ServerSocket ss = new ServerSocket(port);  
        Socket s = ss.accept();  
        InputStream is = s.getInputStream();  
        ObjectInputStream ois = new ObjectInputStream(is);  
        testobject to = (testobject)ois.readObject();  
        if (to!=null){System.out.println(to.id);}  
        System.out.println((String)ois.readObject());  
        is.close();  
        s.close();  
        ss.close();  
    }catch(Exception e){System.out.println(e);}  
    }  
    }  
    
    import java.net.*;  
    import java.io.*;  
       public class SimpleClient {  
       public static void main(String args[]){  
         try{  
         Socket s = new Socket("localhost",2002);  
         OutputStream os = s.getOutputStream();  
         ObjectOutputStream oos = new ObjectOutputStream(os);  
         testobject to = new testobject(1,"object from client");  
         oos.writeObject(to);  
         oos.writeObject(new String("another object from the client"));  
         oos.close();  
         os.close();  
         s.close();  
       }catch(Exception e){System.out.println(e);}  
      }  
    }  
    
    0 讨论(0)
提交回复
热议问题