Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Sending object on a stream (ObjectInputStream, ObjectOutputStream)

843790Jul 8 2008 — edited Jul 12 2008
Hi.
I'm writing a client-server program and I need to pass objects between them. Since there are several different object to send, I have created a class named Message and decided that every object that is sent will be packed inside a Message.
For some reason it doesn't work...
Here is a part from my code, where the client tries to send a message to the server:

In the client side:
User user = new user(123, "David", "USA");
Sockect socket = new Socket(ServerName,ServerPort);

Message message = new Message(user ,"LOGIN");

ObjectOutputStream  objectoutputstream = new ObjectOutputStream(socket.getOutputStream());
objectoutputstream.writeObject(message);
ObjectInputStream objectinputstream = new ObjectInputStream(socket.getInputStream());

Thread thread = new Thread(this);
thread.start();
In the server side:
Message recievedMessage;
while(thread != null)
{
	try 
               {				
		recievedMessage= (Message)inputstream.readObject();										
		/*******RFC Checking**************/
		if(recievedMessage.getType().equals("LOGIN"))
		{					
			//do login...					
		}
               }
              catch(Exception ex) 
              { 

              }
...
...
}
But it doesn't work... It's my first time using ObjectInputStream/ObjectInputStreamand i guess I'm doing some basic mistakes...

What is the problem?
Thanks!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 9 2008
Added on Jul 8 2008
7 comments
170 views