Relaying data with Java Sockets

Hello nice people.
I am a question with attractive points:
I have this scenario, I want to have a bridging program, the sole purpose of this program is to take to get two streams from socket connections, say from CompA and CompB
data will initially move from CompA to the bridge using InputStream then get the OutputStream to CompB write data to it, wait for CompB to reply fetch data from CompB using the appropriate stream relay it to CompA and then close the Socket connection to CompA and leave connection to CompB open. I have assigned this work to a thread and I got messed up in Piped(Input/Output)Streams/Readers
and Writers.

Since the design should be that CompA initiate the socket connection and there can be many at one time, this program should get the Sock,connection from CompA by listening to a port.

What I have done is assign all the work(reading and writing to streams) to a thread and kept the main thread back to listening for other connections, once connected then create a new thread do the bridging staff, this is somewhat similar to the proxy, I got messed up in these threads and apprently data in not getting to the other point,
Please help...
Who is Participating?

Improve company productivity with a Business Account.Sign Up

OviConnect With a Mentor Commented:
A very nice example is provided in the O'Reilly book "Java Examples in a Nutshell, 1st Edition", called You bcan adapt this server to your needs very easy. Download the examples from
I can provide you a example of mine if you post here a email address (you can create a temporary one on a mail server from web).
TinasheCAuthor Commented:
Good start you can post the code at the following email address :
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Shiped ...
I sent you a sample as well.
Exactly what is the question?
If it's why your code does npt work, could you post it so we can tell you why :)
Can u post code or atleast the pseudocode here, so that it will help us to understand ur problem very well. I have experience in sockets, but still i dont uinderstand what was ur requirement is.

Anyway let me give u some tips.

Write a socket program which ascts as a server socket and listens at a port.

When one connected (say CompA) this socket he needs to supply 3 datas.

1) whom does he want to speak to(say CompB)
2) wants to speak or receive.
3) time to wait for te reomote client(compB)
put this in socket in an arraylist.

wait for time mentioned in timetowait., if the remote person(compB) doesnot connect, close this person's socket connection.

In case if he connects with in the handshaking process, with the 3 dats supplied. if ll te dats match, send a message to the supplier socket(say compA), saying to receive.

read the stream and write it into the compB socket. Once the compB receives the EOF it will send some reply message. send that to compA close his socket and remove him from arrayList.

this is just a tips given to U. I dont have time to develop sample files for You.
so., post ur code to this address, i will help u out.

TinasheCAuthor Commented:
Ok here is my pseudocode:
//create a server socket 1
    ss1 = new ServerSocket(number1);
// only proceed if there is a connected and this connection should be kept open all the time
    Socket s1 = ss.accept();
//create a server socket number 2
    ss2 = new ServerSocket(number2);

 while (true)
  Socket s2 = ss2.accept();
 // once connected take input from s2 and write it to s1
 // wait for reply from s1 and write it to s2
 // doing these tasks with threads.
Bridge s1_to_s2=new Bridge(s1.getInputStream
()) ,s2.getOutputStream())

Bridge s2_to_s1 = new Bridge(s2.getInputStream()) ,s1.getOutputStream())
Bridge Thread as follows:
the run method
incomingStream = new BufferedInputStream(socket1.getInputStream());
outgoingStream = new BufferedOutputStream(socket2.getOutputStream());

int x = -1;
while (-1 != (
// i have tried incomingStream.close() here as well this wasn't a good idea as it seems to close the socket as well and these was having an impact on the other thread.

what about sample application ? I believe is very close to what you need.
Sounds about right. If you can turn your pseudocode into compiled code then we can help you get it working.

ur pseudocod e sounds good,
but still where does the code breaks down.
whether it writes reply to the client socket and client nt receives it or not writing to the socket itself.

where was ur problem actually evolve?

Pls., act fast.

No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this
question is:

- points to Ovi

Please leave any comments here within the
next seven days.


Cleanup Volunteer
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.