• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sockets and threads

 
Priya Vasudevan
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello,
I have a Javaserver and my client send X messages to the server.Each messsage is terminated by a # and my javaserver uses this to identify end-of-message.Now,i want each thread to read a message till it encounters #.Since all threads would read the messages,I thought the read logic needs to be synchronized.My javaserver just starts X threads from a Threadpool.I call the synchronized read method from the run() of the Runnable worker thread.But it doesn't seem to work right.The read logice works well for the 1st message only.
I can't figure out where i went wrong??..could u help me with it...
Thanks!!
 
Gopal Shah
Ranch Hand
Posts: 65
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Priya,
Could u specify the code for read(). It is difficult to figure out the problem without the code.
It looks like u r updating a shared variable (something like counter) for the thread to read the next message. In that case synchronization is necessary.
Gopals
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, show us where you're going in a little more detail. You can't have multiple threads reading from the same socket at the same time. But you could have a driver that reads a message and hands it off to a thread for handling. The driver could read at the speed of the network and not be slowed down by the server code that runs in response to each message.

The logic would be something like:

Hmmm, there must be a cleane way to say that without three reads. Lemme know if that's going the right direction.
If you get tons of messages faster than you can handle them you'll wind up with tons of threads which could be very bad. If the number of threads or the overhead of creating them becomes a problem, look into thread pools. The Jakarta Commons has a nice one.
 
Priya Vasudevan
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The code in the Java server is as follows:

This is where the socket accepts messages from the client,and the socket's instance is passed as parameter to a Runnable thread from the thread pool
The code in New Thread is


Here,r is a read class,which has a synchronized read() method.This method is called in the read() of the Thread.
The code for read() is:

Can u help me with it now??
Thanks!!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your last code snippet, look at what happens when you hit a # character. Does it display one message and keep reading for the next message? My little psuedo code separated the check for # from the check for -1 end of stream. See if that would help.
 
Karthik Meyyappan
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the read method's while, you can just change the order in which the characters are checked for. i.e., -1 before #.
 
Priya Vasudevan
Greenhorn
Posts: 17
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
Yup, show us where you're going in a little more detail. You can't have multiple threads reading from the same socket at the same time. But you could have a driver that reads a message and hands it off to a thread for handling. The driver could read at the speed of the network and not be slowed down by the server code that runs in response to each message.

The logic would be something like:

Hmmm, there must be a cleane way to say that without three reads. Lemme know if that's going the right direction.
If you get tons of messages faster than you can handle them you'll wind up with tons of threads which could be very bad. If the number of threads or the overhead of creating them becomes a problem, look into thread pools. The Jakarta Commons has a nice one.

Hi Stan,

Instead of reading the message and passing each message to a Thread,I want the Thread to read the message till it encounters a #.Have already sent the code.I would alsolike to know,if there would be a better preformance this way!!!
I tried using your code as well,but it gives me NullPointerException,when it tries to read the message the 2nd time...though it works well the first time.
Thanks!!
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, I dropped the idea of the thread doing the reading. I'm not sure how you'd make that work. The main thread would have to wait until the reader thread has found the # before giving the socket to another reader thread to read. I thought it would be far simpler to keep the reading in a single thread and dispatch message handling.
This is all because I assume we're dealing with a single client connecting and sending a series of messages separated by #. When you have many clients each sending one message - like a web server - you actually get a new socket for each client so you can spin them off to read on their own threads.
 
Jose Botella
Ranch Hand
Posts: 2120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Priya.
You don't need to synchronize the reads on the sockets returned by accept. They are different ones. Even though their getLocalPort and getLocalAddress yield the same result, surely getPort and getInetAddress won't.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic