Solved

Java Socket Programming

Posted on 2009-05-04
11
587 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Flexible connectivity for any environment

The KE6900 series can extend and deploy computers with high definition displays across multiple stations in a variety of applications that suit any environment. Expand computer use to stations across multiple rooms with dynamic access.

 
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

Turn Insights Into Action

You’ve already invested in ITSM tools, chat applications, automation utilities, and more. Fortify these solutions with intelligent communications so you can drive business processes forward.

With xMatters, you'll never miss a beat.

Question has a verified solution.

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

This article is about some of the basic and important steps to be used to improve the performance in web-sphere commerce application development. 1) Always leverage the Dyna-caching facility provided by the product 2) Remove the unwanted code …
Upgrading Tomcat – There are a couple of methods to upgrade Tomcat is to use The Apache Installer is to download and unzip and run the services.bat remove|install Tomcat6 Because of the App that we are working with, we can only use Tomcat 6.…
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 will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

690 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