Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Issue with simple multi-threaded Echo server and client program

 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey everyone,

I am just trying to create a simple echo server. For some reason I can't determine I will connect to the server using the client program and the server will acknowledge that it received a connection, but the server never echoes what the client sends to them.

Server:



Client:


Any help would be great.

Thanks,
Hunter
 
Paul Clapham
Sheriff
Posts: 21143
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, for one thing you're using PrintWriters to send data over the network. This is a problematic practice because PrintWriter is specifically written not to throw exceptions, and you do want to know about exceptions in an environment which is likely to have them.

The other thing is that you aren't echoing the data sent exactly. You are removing line-ending characters from it. This could be a problem when your client is expecting to receive data terminated by line-ending characters.
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't know that about PrintWriter. I was formatting my example off of code I saw on the Oracle tutorial page and PrintWriter was used there, next time I'll investigate more. Would PrintStream be a better option to send data across the network?

Thanks.
Hunter
 
Paul Clapham
Sheriff
Posts: 21143
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PrintStream? Well, I looked at its API documentation and it says
Unlike other output streams, a PrintStream never throws an IOException...


But you already have an OutputStream. Why do you need anything more than that?
 
Hunter McMillen
Ranch Hand
Posts: 492
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you that worked perfectly, still adjusting to Java vs C networking stuff.

Thanks.
Hunter
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic