views:

126

answers:

1

Code for server:

http://stikked.com/view/64826511

Network Code for client:

http://stikked.com/view/38974838

Basically, the client connects to the server, but beyond that, it does not work. When a message is sent, the client's System.out.println indicates that the GUI is calling the correct function. But there is no sign of the server ever recieving the in put. If I telnet into the server, it functions correctly.

This is my first unaided attempt at both threaded code, and java networking. Up till now, most of my programming has been web apps or very simple desktop apps (e.g. Calculator).

(If your answer is "Your doing it all wrong", please point to a correct tutorial for a client-server program where both the client and server can send messages at any time. All the tutorials I've seen have the client execute a few hardcoded commands, then quit)

+3  A: 

Two immediate problems - you're using a PrintWriter, which means it won't throw any exceptions if it can't actually talk to the server. You're also not calling flush(), so it may well just be buffering the data.

I would suggest:

  • Use OutputStreamWriter instead of PrintWriter, and handle exceptions appropriately. This will remove buffering as well. You may want to wrap it in a BufferedWriter and then call flush() after you're "done" with a message.
  • Specify the appropriate charset, e.g. UTF-8.
Jon Skeet
Changing to OutputStreamWriter and calling flush() fixed it. How do I specify a charset?
Macha
Use the overload for the OutputStreamWriter constructor which takes a charset name, e.g. "utf8".
Jon Skeet