Java Socket send object to server

Description

Java Socket send object to server

import java.io.ObjectOutputStream;
import java.net.Socket;

class Message implements java.io.Serializable {
   String subject, text;// w  w  w . ja v  a2 s . c  o  m

   Message(String s, String t) {
      this.subject = s;
      this.text = t;
   }

   String getSubject() {
      return subject;
   }

   String getText() {
      return text;
   }
}

public class Main {
   public static void main(String argv[]) throws Exception {
      String fact;
      // create a socket to the server
      Socket socket = new Socket("172.16.5.81", 6789);
      System.out.println("Connected to localhost at port 6789");
      // Create a Message object to be sent
      Message msg = new Message("Remainder", "Return my book on Monday");
      // Create an ObjectOutputStream object
      ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
      // Serialize and send over TCP
      oos.writeObject(msg);
      System.out.println("Sent an object");
      socket.close();
   }
}



PreviousNext

Related