Socket Buffered*putStream Threads and Full-duplex communication

R

Richard Maher

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.
 
R

Richard Maher

Hi Thomas,

Thomas Pornin said:
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.



It is actually sounder than many other architectures, if data may
be transmitted in both directions simultaneously.



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/j...c/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!
 
E

EJP

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.
 
R

Richard Maher

Hi EJP,

EJP said:
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
 
H

hiwa

Hi EJP,





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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top