From: Richard Maher on
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.


From: Thomas Pornin on
According to Richard Maher <maher_rj(a)hotspamnotmail.com>:
> 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.

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/jre/api/net/httpserver/spec/com/sun/net/httpserver/package-summary.html


--Thomas Pornin
From: Richard Maher on
Hi Thomas,

"Thomas Pornin" <pornin(a)bolet.org> wrote in message
news:4b9f9a05$0$22008$426a74cc(a)news.free.fr...
> According to Richard Maher <maher_rj(a)hotspamnotmail.com>:
> > 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/jre/api/net/httpserver/spec/com/sun/net/httpserver/package-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 bollocks the HTML5 people have come
up with :-(

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

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


From: EJP on
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.
From: Richard Maher on
Hi EJP,

"EJP" <esmond.not.pitt(a)not.bigpond.com> wrote in message
news:65Vnn.13412$pv.8699(a)news-server.bigpond.net.au...
> 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