Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 213
  • Last Modified:

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...
0
TinasheC
Asked:
TinasheC
  • 4
  • 2
  • 2
  • +3
1 Solution
 
OviCommented:
A very nice example is provided in the O'Reilly book "Java Examples in a Nutshell, 1st Edition", called Server.java. You bcan adapt this server to your needs very easy. Download the examples from http://www.oreilly.com/catalog/jenut/examples/
0
 
OviCommented:
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).
0
 
TinasheCAuthor Commented:
Good start you can post the code at the following email address : chipomho@yahoo.co.uk
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
OviCommented:
Shiped ...
0
 
gadioCommented:
I sent you a sample as well.
0
 
objectsCommented:
Exactly what is the question?
If it's why your code does npt work, could you post it so we can tell you why :)
0
 
saurav17Commented:
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.
0
 
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();
    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();

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

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.
0
 
girionisCommented:
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
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
  • 2
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now