Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Socket Buffered*putStream Threads and Full-duplex communication

Reply
Thread Tools

Socket Buffered*putStream Threads and Full-duplex communication

 
 
Richard Maher
Guest
Posts: n/a
 
      03-16-2010
Hi,

I'm sure I asked a similar question here before but couldn't find it -
sorry.

Can someone please confirm that for a given Socket with a
BufferedInputStream "in" and a BufferedOutputStream "out", I can have one
thread T1 executing a series of out.write()s followed by an out.flush()
without explicit locking or synchronizing that will in noway interfere with
another Thread T2 that is happily performing one (or more until we get "n"
bytes) in.reads?

IOW, one T1 is sending messages as it likes to a remote server and T2 is
processing the response messages. I want this all to happen in parallel and
without explicit thread-synching or lock/mutexing. Is that architecturally
sound given the classes that I am using?

Cheers Richard Maher

PS. For the sake of argument please assume that T1 is sending random numbers
and T2 is just doing System.out.println() i.e pretend the reader and writer
are completely independent.


 
Reply With Quote
 
 
 
 
Richard Maher
Guest
Posts: n/a
 
      03-16-2010
Hi Thomas,

"Thomas Pornin" <(E-Mail Removed)> wrote in message
news:4b9f9a05$0$22008$(E-Mail Removed)...
> According to Richard Maher <(E-Mail Removed)>:
> > Can someone please confirm that for a given Socket with a
> > BufferedInputStream "in" and a BufferedOutputStream "out", I can have
> > one thread T1 executing a series of out.write()s followed by an
> > out.flush() without explicit locking or synchronizing that will in
> > noway interfere with another Thread T2 that is happily performing one
> > (or more until we get "n" bytes) in.reads?

>
> I can confirm. It is true without the buffered streams as well. The two
> streams associated with a socket are independent from each other, save
> for what happens upon closing either (or the socket itself). But
> explicit synchronization is not needed either for that anyway.
>
>
> > IOW, one T1 is sending messages as it likes to a remote server and T2
> > is processing the response messages. I want this all to happen in
> > parallel and without explicit thread-synching or lock/mutexing. Is
> > that architecturally sound given the classes that I am using?

>
> It is actually sounder than many other architectures, if data may
> be transmitted in both directions simultaneously.
>
>
> > PS. For the sake of argument please assume that T1 is sending random
> > numbers and T2 is just doing System.out.println() i.e pretend the
> > reader and writer are completely independent.

>
> If reader and writer are _not_ completely independent, then the problem
> becomes more complex. Depending on how they interact. One possible
> interaction being on the remote side of the socket, of course. An
> example is that of a HTTP client. Theoretically, the HTTP client may
> send several requests in a row, asynchronously reading the responses,
> but each response corresponds to one request, in due order. The client
> must match requests and responses and there lies potential trouble.
> Fortunately, the JVM comes with a HTTP client which already does all
> that hard work.


Thanks for confirming that for me.

>
> There is a subliminal message here: maybe you should try to use HTTP
> as a transport mechanism ? Sun's JVM already comes with an HTTP client
> _and_ an HTTP server. The HTTP client uses java.net.URL. For the HTTP
> server, see:
>

http://java.sun.com/javase/6/docs/jr...e-summary.html
>
>


No a *signle*, secure, authenticated, connection-oriented, context-rich
protocol will be just fine thanks.

> --Thomas Pornin


Cheers Richard Maher

PS. For those with a love-affair for HTTP and the school of thought that
says "Hey, I reakon BSD(ish) TCP/IP Sockets would be much better implemented
over HTTP" then you'll probably love the *******s the HTML5 people have come
up with

See http://cometdaily.com/2010/03/02/is-...t-chat-simple/ for just some
issues.

You just can't make a silk middleware-backbone out of a pig's http!


 
Reply With Quote
 
 
 
 
EJP
Guest
Posts: n/a
 
      03-17-2010
On 17/03/2010 1:47 AM, Thomas Pornin wrote:
> I can confirm. It is true without the buffered streams as well. The two
> streams associated with a socket are independent from each other, save
> for what happens upon closing either (or the socket itself). But
> explicit synchronization is not needed either for that anyway.


Qualification: if you started with a SocketChannel and got the streams
via Channels.newInput/OutputStream, the input and output streams are
synchronized internally for some reason, so problems can occur if you
try to use them in full-duplex mode. However if you started with a
Socket you are OK.
 
Reply With Quote
 
Richard Maher
Guest
Posts: n/a
 
      03-17-2010
Hi EJP,

"EJP" <(E-Mail Removed)> wrote in message
news:65Vnn.13412$(E-Mail Removed)...
> On 17/03/2010 1:47 AM, Thomas Pornin wrote:
> > I can confirm. It is true without the buffered streams as well. The two
> > streams associated with a socket are independent from each other, save
> > for what happens upon closing either (or the socket itself). But
> > explicit synchronization is not needed either for that anyway.

>
> Qualification: if you started with a SocketChannel and got the streams
> via Channels.newInput/OutputStream, the input and output streams are
> synchronized internally for some reason, so problems can occur if you
> try to use them in full-duplex mode. However if you started with a
> Socket you are OK.


Thanks for the heads-up about SocketChannels but we're using plain Sockets
so looks like we should be ok.

Thanks again

Cheers Richard Maher


 
Reply With Quote
 
hiwa
Guest
Posts: n/a
 
      03-17-2010
On Mar 17, 7:50*pm, "Richard Maher" <(E-Mail Removed)>
wrote:
> Hi EJP,
>
> "EJP" <(E-Mail Removed)> wrote in message
>
> news:65Vnn.13412$(E-Mail Removed)...
>
> > On 17/03/2010 1:47 AM, Thomas Pornin wrote:
> > > I can confirm. It is true without the buffered streams as well. The two
> > > streams associated with a socket are independent from each other, save
> > > for what happens upon closing either (or the socket itself). But
> > > explicit synchronization is not needed either for that anyway.

>
> > Qualification: if you started with a SocketChannel and got the streams
> > via Channels.newInput/OutputStream, the input and output streams are
> > synchronized internally for some reason, so problems can occur if you
> > try to use them in full-duplex mode. However if you started with a
> > Socket you are OK.

>
> Thanks for the heads-up about SocketChannels but we're using plain Sockets
> so looks like we should be ok.
>
> Thanks again
>
> Cheers Richard Maher


Hi EJP
We'd like you reviving your telekinesis.au site.
We need some files there to see regarding your FNiJ book.
hiwa

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 1 02-03-2009 06:20 AM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Steve Holden Python 0 02-01-2009 12:45 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 0 02-01-2009 07:37 AM
socket.unbind or socket.unlisten? - socket.error: (48, 'Addressalready in use') Laszlo Nagy Python 1 01-27-2009 05:05 PM
Re: socket.unbind or socket.unlisten? - socket.error: (48,'Address already in use') Jean-Paul Calderone Python 0 01-27-2009 01:41 PM



Advertisments