Link to home
Start Free TrialLog in
Avatar of TinasheC
TinasheC

asked on

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...
ASKER CERTIFIED SOLUTION
Avatar of Ovi
Ovi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Ovi
Ovi

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).
Avatar of TinasheC

ASKER

Good start you can post the code at the following email address : chipomho@yahoo.co.uk
Shiped ...
I sent you a sample as well.
Avatar of Mick Barry
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 time.do 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.

saravanankj@business-functions.com

regards,
K.J.S.
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();
    System.out.println("CONNECTED!");
//create a server socket number 2
    ss2 = new ServerSocket(number2);

 while (true)
 {
  conNumber++;
  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())
s1_to_s2.start();

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

int x = -1;
while (-1 != (x=incomingStream.read()))
{
   outgoingStream.write(x);
}
incomingStream.flush();
outgoingStream.flush();
// 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.
this.stop();

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.

Regards,
K.J.S.
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.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER !

girionis
Cleanup Volunteer