Solved

Java Socket Programming

Posted on 2009-05-04
11
581 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Updating Java 9 87
network + 7 73
Java array passed to SQL where clause 23 40
JUnit 4 @Before and @BeforeClass differences 3 19
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

708 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now