• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java Server/Client String comunication over socket

 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello. I'm experiencing a big problem.

I have created an application (client/server mode) in which i have a concurrent server and a many clients. all I want is to send some messages from the client to the server and then back (skipping back response for now). well, for now i have constructed the server which i think is well constructed, and the server which i think is well constructed to.

So, i have put the client reading from a file (a dictionary file with many words), and as it reads a word it sends it over the socket. As far as i know its ok from here. Then when the client sends something, the server should read from the socket. The big problem is that the server reads nothing and seems to me that s = brIn.readLine(); is not a passive wait method.


Please the following code is just for document (i MUST NOT read it all)

THE SERVER CODE

Class KnockKnockServer ---> the main class that "waits" for clients and accepts their connections






The RunnableThread Classv ----> handles the client/server comunication (extends thread)





THE CLIENT CODE

the Client class ----> Just to get started




The Graphics class ---> it implements some swing stuff (that is supposed to not bother you and are here just for document all and for better understanding) and on SendToServer() sends the content file to over the socket to the server. The method SendToServer() creates a new thread because i have lo free UI thread to update the progress bar.





Please i say it again: you probably DONT HAVE to see all the code to understand my problem.


Thanks in advance.
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Pedro Neves
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.





You were right about i'm using a file without /n and the DataInputStream / DataOutputStream didn't really work. Well i changed to BuffeRedrear/BufferedWrited changed the file, and changed the "extend thread" to "Implement runnable" and it still no works..

On the SERVER (on the run() ) i changed to this




on the client to this (graphics class, sendToServer method):




the console output is:

- ON SERVER:

Thread Constructor
Thread Constructor
0-----------
1-----------
0-----------
1-----------


- ON CLIENT:

1
2
1
2
....

it does not read all file because (at some point) it stops on the WRITE i dont know why. However nothing is getting to the server side yet.. :-/

Thanks in advance
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
Pedro Neves
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 .



You're right.. i have already chaged it.. I had put the new change before you comment..
 
Jelle Klap
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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():


 
Pedro Neves
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
.
 
Pedro Neves
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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():





Well Another problem Now.... I cant send the dictionary back to the client..

On the server i have this:




on the client i have this:




It sends all words over the socket but has a problem. The problem is that, when the client connects, these lines are ran:



and then, after it reads some words of the file (in this stage the server will send the same dictionary to the client) it gives me the following exception:

java.net.SocketException: Software caused connection abort: socket write error
nullOUT
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109)
at java.net.SocketOutputStream.write(SocketOutputStream.java:153)
at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221)
at sun.nio.cs.StreamEncoder.implWrite(StreamEncoder.java:282)
at sun.nio.cs.StreamEncoder.write(StreamEncoder.java:125)
at java.io.OutputStreamWriter.write(OutputStreamWriter.java:207)
at java.io.BufferedWriter.flushBuffer(BufferedWriter.java:129)
at java.io.BufferedWriter.write(BufferedWriter.java:230)
at java.io.Writer.write(Writer.java:157)
at RunnableThread.run(RunnableThread.java:53)
at java.lang.Thread.run(Thread.java:722)


this should be because on the client side i "write" to the server and the i read from the server. On the server side i Read from the client and then a write to the client. The problem is that, when the server starts this code lines:


it skips the first ones (might be because s is null), starts to read the file and then tryies to write to the client which is not listening to a reading and is ready to write.


thanks in advance
 
reply
    Bookmark Topic Watch Topic
  • New Topic