Click here to Skip to main content
15,608,936 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!

I' m creating a Java Client/Server program, so I'm trying to send a object(User) from Client to Server, then, in the server-side, I want to know what kind of object it is and then I want to save it to use that in the future..


When I try to create userReceived...then nothing happens...like..
It's possible to read the obj only once, in this case in the conditional "if" but my purpose is to know before what kind of obj is and then work with it..but of course I need to know the type of data because in the future I want to send different types of data.

Thanks in advance for your help.

What I have tried:

Client-side

<pre lang="Java">InetAddress addr = InetAddress.getLocalHost();

ObjectOutputStream oos = null;

Socket socket = new Socket(addr.getHostName(), port);

oos = new ObjectOutputStream(socket.getOutputStream());

oos.writeObject(user);


Server-side

Java
server = new ServerSocket(port);

Socket mySocket = server.accept();
				
ObjectInputStream ois = new ObjectInputStream(mySocket.getInputStream());
				
				
if ( ois.readObject() instanceof User) {
					
		System.out.println("Client connected!");
					
		User userReceived = (User) ois.readObject();
Posted
Updated 27-Aug-22 6:55am

1 solution

You can get most of the required information via the methods of the ObjectInputStream (Java Platform SE 7 )[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900