Only 44 hours left in the trailboss' kickstarter!

New rewards and stretch goals. CLICK HERE!



Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ObjectOutputStream and Sockets.  RSS feed

 
Nina Foley
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm new to Java and I'm trying to put together a client side program which will send a message (string) to a server side application, and depending on the content of the message, the application will perform an appropriate action. Using some example code from the net, I managed to put together a client and server program in java that worked.

However when I change the IP Address and socket to point to the real server side application, I get an invalid stream header error. I know the application receives the connection at least as it displays a brief message something like "Winsock1 Connection from <IP Address>" but then instantly goes to "Winsock1 <IP Address> disconnected". I tried using the 'HelloDevice' from UniversalComm (really useful program) to see if I could determine what was being passed, and it does show the message, but the message is preceded with ....t.. (byte code AC ED 00 05 74 00 0F).

I've read some articles that indicate that this may be to do with the encoding and the way the message is passed across, but I'm not really understanding it. Can someone please point me in the right direction with this? This is the code I'm using:-

 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I have added code tags and indentation to your code and new line characters so the lines are not too long. Always do that: doesn't it look better.
Don't know much about sockets myself. Are you using Java7/8? If so you should probably use try with resourcesYou can use try with resources with all sorts of readers writers etc. That way they are all automatically closed without your having to write explicit close calls. I went through the Java® Tutorials and can't see what you are doing wrong. They use PrintWriters and BufferedReaders there rather than object streams. Readers and writers are more appropriate for Strings and Streams for binary data, but since Strings are always Serializable, I can't see that being a real problem.
Why are you using Thread.sleep()? I cannot see that either helping or hindering.
I presume you have the server side working at the same time.
Try using only English letters and numbers in the text (e rather than é) which should obviate any encoding problems. Can you specify an encoding on the sockets? Probably not. I don't think the hex you showed will cause problems because you don't have 04 or 1a (=ctrl‑D/ctrl‑Z) which might be interpreted as end of transmission there.

That is about as much as I know, I am afraid.
 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you verified that port 1197 is available?
There is an error in the code I posted. Line 4 needs another ) at its end.
 
Nina Foley
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your prompt reply! Unfortunately I was drowning in work and this is the first chance I've had to get back to the issue.
Re Thread.sleep, there was additional code which has been removed since and it should have been removed also.
Re the inputs, all characters entered were ASCII - an example is BMSG0001001001X where the X is a terminator when read by the server app.
1197 was / is available as a port.

Since then... I looked at the code you mentioned in the tutorials and changed to PrintWriter to try it - Voila! no more leading characters and the message is processed accordingly.... but,
and there's always the but; I'm not getting the reply. The normal reply is OK, only 2 bytes, - and when I used the 'HelloDevice' to test, it works. It would appear my code is failing to execute to the end.

I'll go trawling for examples / forum posts on this issue and see if I can work out what's going on. If I can't, I may be posting again!

Thanks again

 
Campbell Ritchie
Sheriff
Posts: 53720
127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So far so good … well done
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!