-
Socket I-O Stream Multi-Threading
I am writing an application which will use a socket and this question has
occured. The application will be multi threaded- one thread will create
the server socket and accept a connection. Haveing accepted the connection,
it will fall into a loop reading messages from the input stream. For each
message a new thread is created to respond to the incomming message. The
message processing threads are synchronized so that only one will ever have
control over the output stream. Is there a problem with one of these message
processing threads trying to write to the output stream while the listener
thread is reading from the input stream. Or is this controled from within
the socket api?
-
Re: Socket I-O Stream Multi-Threading
Hey Bowena, BradO here...comments below:
"bowena" <java.@127.0.0.1> wrote in message news:3d88b991$1@10.1.10.29...
>The
> message processing threads are synchronized so that only one will ever
have
> control over the output stream.
I am curious as to how you are accomplishing this...since these threads are
different objects, these threads have to be bottlenecked through a common
"gate" if you will -- providing synchronization on methods only (since they
are different objects) isn't going to solve the problem.
>Is there a problem with one of these message
> processing threads trying to write to the output stream while the listener
> thread is reading from the input stream. Or is this controled from
within
> the socket api?
Depending on your answer to my question above, I think yes, there is a
potential problem. I have an idea for you. Why not have on thread perform
all reads from the socket and another thread perform all writes from the
socket. When a read thread gets a message, it will dump it into a read
queue. Likewise, the write thread will get outbound messages from a
separate write queue and send them. Now, create a small pool of threads to
pick up messages from the read queue, process, and create outbound messages
and put them in the write queue. If your queues are synchronized, you are
safe.
Just a thought, hope that helps.
Cheers,
BradO
--
Brad O'Hearne
DevX Section Leader
-
Re: Socket I-O Stream Multi-Threading
hi ..
I think ur trying to write chatserever like application with server socket
and sponing a thread each time new client comes.
if u keep every client thread seperate with its own soket created by serever
socket thread ,and allow it to handle its input and output streams in same
thread then there need not be any conflict.
but if u need to pass the messages across the clients then u will have some
central method to exchange the data.
-sanjay
"bowena" <java.@127.0.0.1> wrote:
>
>I am writing an application which will use a socket and this question has
>occured. The application will be multi threaded- one thread will create
>the server socket and accept a connection. Haveing accepted the connection,
>it will fall into a loop reading messages from the input stream. For each
>message a new thread is created to respond to the incomming message. The
>message processing threads are synchronized so that only one will ever have
>control over the output stream. Is there a problem with one of these message
>processing threads trying to write to the output stream while the listener
>thread is reading from the input stream. Or is this controled from within
>the socket api?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks