Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Jelle Klap wrote:The problem you're facing is probably due to the fact that the client never sends a line seperator, causing the readLine() call on the InputStream of the server's socket to block.
Besides that, though, there are some additional problems you should look at.
Your RunnableThread extends the Thread class and invokes the start() method in its contructor - don't do that! Instead, have your RunnableThread class - for which you should probably come up with a better name - implement the Runnable interface, so you can pass an instance of it to the contructor of the Thread class, after which you can invoke the start() method of that Thread instance:
Your code uses a DataInputStream / DataOutputStream to wrap the Socket's InputStream / OutputStream. That's not the best option for what you're doing, and certainly not if you invoke deprecated methods like readLine(). You should probably have a look at BufferedReader and BufferedWriter instead. Also, while readLine() recognizes \r, \n, and \r\n as line seperators, you should use a well defined protocol for socket communication - pick one of the former and stick with it on all platforms. Given that, you should avoid methods that add line seperators in a platform specific way, such as BufferedWriter.newLine() etc.
Jayesh A Lalwani wrote:Why do you create a DataInputStream on your Socketstream before you create your thread? You don't even use it. It's likely that the finalizer of the SocketInputStream is closing the underlying socket .
Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Jelle Klap wrote:Well, your Server now seems to be listening to System.in, instead of the InputStream of the Socket connection to the client...
The client is still not sending line seperator characters, because the readLine() method you use to read the file content strips them off, so you have to re-add them before you write to the Socket's OutputStream.
Also, you're invoking the readLine() mehod too often when reading the file on the client. You probably should do something like:
Lastly, initializing a String reference by invoking the String constructor is not necessary and pointless, as Strings are immutable. You can simply delcare a String reference and assign a literal value to it or the return value of a method like readLine():