File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Sockets and Internet Protocols and the fly likes sockets and threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "sockets and threads" Watch "sockets and threads" New topic
Author

sockets and threads

Priya Vasudevan
Greenhorn

Joined: Mar 19, 2004
Posts: 17
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

Joined: May 17, 2003
Posts: 65
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

Joined: Jan 29, 2003
Posts: 8791
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.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Priya Vasudevan
Greenhorn

Joined: Mar 19, 2004
Posts: 17
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

Joined: Jan 29, 2003
Posts: 8791
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

Joined: Apr 13, 2004
Posts: 2
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

Joined: Mar 19, 2004
Posts: 17
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

Joined: Jan 29, 2003
Posts: 8791
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

Joined: Jul 03, 2001
Posts: 2120
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.


SCJP2. Please Indent your code using UBB Code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sockets and threads