I have a couple of classes. Student, Teacher and Book.
I want to send instances of those 3 objects from server to the connecting client. I know how to send/receive instances of single type, i.e. Student from server to client using the following code
Client Side
Socket socket = new Socket(ip, port);
try {
ObjectInputStream objectInput = new ObjectInputStream(socket.getInputStream());
try {
Object object =(Student) objectInput.readObject();
Student std = (Student) object;
//do something with std
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Server Side
Student a = new Student();
ServerSocket myServerSocket = new ServerSocket(port);
Socket skt = myServerSocket.accept();
try
{
ObjectOutputStream objectOutput = new ObjectOutputStream(skt.getOutputStream());
objectOutput.writeObject(a);
}
catch (IOException e)
{
e.printStackTrace();
}
How to extend this code to be able to send different types of objects from server and receive them correctly on the client side
Do I need to wrap them all in another object and give each one a type? Thanks!