Solved

Relaying data with Java Sockets

Posted on 2002-06-25
13
198 Views
Last Modified: 2010-03-31
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
Comment
Question by:TinasheC
[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
  • 2
  • 2
  • +3
13 Comments
 
LVL 9

Accepted Solution

by:
Ovi earned 500 total points
ID: 7107111
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
 
LVL 9

Expert Comment

by:Ovi
ID: 7107115
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
 

Author Comment

by:TinasheC
ID: 7107289
Good start you can post the code at the following email address : chipomho@yahoo.co.uk
0
Technology Partners: 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!

 
LVL 9

Expert Comment

by:Ovi
ID: 7107489
Shiped ...
0
 
LVL 6

Expert Comment

by:gadio
ID: 7107642
I sent you a sample as well.
0
 
LVL 92

Expert Comment

by:objects
ID: 7109549
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
 
LVL 1

Expert Comment

by:saurav17
ID: 7109784
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
 

Author Comment

by:TinasheC
ID: 7110028
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
 
LVL 9

Expert Comment

by:Ovi
ID: 7110064
what about sample application ? I believe is very close to what you need.
0
 
LVL 92

Expert Comment

by:objects
ID: 7110081
Sounds about right. If you can turn your pseudocode into compiled code then we can help you get it working.
0
 
LVL 1

Expert Comment

by:saurav17
ID: 7110239

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
 
LVL 35

Expert Comment

by:girionis
ID: 8860703
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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Weekend adv creator 3 64
How do I remove an object from a 3 52
Delphi Firemonkey: if the Sms contain special characters it won't send it 3 63
java mysql insert application 14 41
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

762 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