Solved

Java Socket Programming

Posted on 2009-05-04
11
583 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
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
LVL 5

Assisted Solution

by:avya2k
avya2k earned 200 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 300 total points
ID: 24307707
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24308943
:-)
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
GUI builder for Eclipse? 8 37
servlet requestdispatcher include and forward differences 1 32
replace a word with other 1 44
spring jars download 1 29
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

831 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