• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading in different variables  RSS feed

 
Matthew Tilic
Ranch Hand
Posts: 41
Chrome Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I've setup an ObjectInputStream and its waiting to recieve a String or an object. Is their a way for it to distinguish between the two.

I tried to setup two threads one looking for a string the other for my object but they seem not to work together as they are listening on the same port.

Thanks
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you writing the String? Since a String is an Object, you could write it in the same way. Then at the receiving end you can just read an Object, and then use instanceof to find out if it's a String or not.
 
Matthew Tilic
Ranch Hand
Posts: 41
Chrome Java MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey that would be great!!! How do you use instance of? I have not ever used it.

Can you give me a link to a good tutorial or where is a good place for info.



On a side note. ever since I have found code ranch its been great. Such a good forum
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using instanceof is pretty straightforward. There's a bit on it here, but basically it tells you if an object can be cast to a particular type:
So you can do something like this:
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh, by the way, we've got a forum for IO and Streams. I'll move this there because I think it's a better fit - it's not really about the underlying protocols.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!