Link to home
Start Free TrialLog in
Avatar of dolphin_g
dolphin_g

asked on

Java Socket Programming

I'm new to Java socket Programming. I'm trying to develop an application that constantly listens to and writes to
a socket. I'm trying to implement a singleton socket, and create Input and Output streams, to read and write to it.

Than have to different threads forked out, each get hold of the input and output streams of the socket for paralell
reads and writes. Is this a good approach. I've not implemented it. trying to figure out the odds first.

Can we create more than 2 streams in a socket?

Thanks.
Avatar of Thomas4019
Thomas4019
Flag of United States of America image

I usually make two socket connections, one for input and one for output. However, it is perfectly possible to do both with only one socket connection, i dont know about the performance consequences though. I know for a fact that you can only have 1 input and 1 ouput stream per socket connection.
Avatar of Mick Barry
If you have more than one thread using the same socket then you need to worry about synchronising the access so only one thread is using the socket at a time.  Depending on your application a separate socket for each thread may be a better solution to avoid synchronization issues.

Avatar of avya2k
avya2k

The socket provide only two streams as InputStream and OutputStream from repective get methods of Socket class
You can not create more than these

Also if your program is multithrreaded why not use Socket pool for individual thread if possible. For this i guess each communication should be independent of others
If second thread can only write some thing based on input recieved from first thread then it may not be possible
But pooling will be best to achieve good performance in multi-threaded environment
SOLUTION
Avatar of avya2k
avya2k

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>Is this a good approach. I've not implemented it. trying to figure out the odds first.

It's difficult to know without knowing the details of what you're doing, but it's quite possibly a good approach. You can make a synchronized facade to the streams and have that accessed by members of a thread pool as avya2k suggested
Avatar of dolphin_g

ASKER

I'm trying to read and write to a socket, that is established with a third party vendor software on a different machine. And they accept only socket connections. My application requires to be
listening to the input stream, and write to output stream(irrespective of the input stream data). I'm trying to have these both streams handled by two different threads, each with the restricted job of reading or writing messages to/from their respective JMS queues on the application.

I have two JMS Queues that which store data that requires pre processing(write), and post processing(after read).

Does synchronization come into play, when i have each stream of the socket assigned to threads on 1 on 1 basis(two threads, one for input stream and one for output stream).

I'm also looking at the Java NIO libraries.

Input stream:

If The input stream just read some data, and has made a call for post processing, and returns after post processing, will the socket buffer all the data in the meantime.

On socket read, will it give back the complete data buffered so far, or will it read one msg at a time.

The server on the other end of the socket is sending one msg at a time. During my intial tests with the socket, each socket read, reads one msg that was written onto it by the server.
To be more descriptive.
Thread 1: Input Stream of the Socket
            1. reads messages from the socket, processes it, and writes to a JMS queue that delivers msg to the application.

Thread 2: Output Stream of the socket.
                Receives messages from the JMS socket, processes it, and writes to the socket.

>>If The input stream just read some data, and has made a call for post processing, and returns after post processing, will the socket buffer all the data in the meantime.

Once you've read it, it will be removed from the buffer
Not about the data that was read, about the data the third party server would be sending to you while you are processing the read data.

I understand that the read data is removed from the socket buffer. As I am expecting large
amount of data from the input stream of the socket, I'm concerned that every time I read some data, during the processing time, will it on the road down lead to buffer overflow on the socket. Should I increase the size of the buffer on the socket. Can it be done? what is the size limit on this.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
:-)