?
Solved

Java Socket Programming

Posted on 2009-05-04
11
Medium Priority
?
623 Views
Last Modified: 2013-12-02
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.
0
Comment
Question by:dolphin_g
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 17

Expert Comment

by:Thomas4019
ID: 24301102
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.
0
 
LVL 92

Expert Comment

by:objects
ID: 24301207
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.

0
 
LVL 5

Expert Comment

by:avya2k
ID: 24301902
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
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 5

Assisted Solution

by:avya2k
avya2k earned 800 total points
ID: 24301907
synchronization will slower the performance of ur threads
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24302156
>>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
0
 

Author Comment

by:dolphin_g
ID: 24306969
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.
0
 

Author Comment

by:dolphin_g
ID: 24306999
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.

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24307317
>>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
0
 

Author Comment

by:dolphin_g
ID: 24307526
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.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 1200 total points
ID: 24307707
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24308943
:-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This exercise is about for the following scenario: Dmgr and One node with 2 application server. Each application server contains it owns application. Application server name as follows server1 contains app1 server2 contains app1 Prereq…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month14 days, 17 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question