Solved

Relaying data with Java Sockets

Posted on 2002-06-25
13
187 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
  • 4
  • 2
  • 2
  • +3
13 Comments
 
LVL 9

Accepted Solution

by:
Ovi earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Good start you can post the code at the following email address : chipomho@yahoo.co.uk
0
 
LVL 9

Expert Comment

by:Ovi
Comment Utility
Shiped ...
0
 
LVL 6

Expert Comment

by:gadio
Comment Utility
I sent you a sample as well.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 1

Expert Comment

by:saurav17
Comment Utility
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
Comment Utility
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
Comment Utility
what about sample application ? I believe is very close to what you need.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Comment Utility

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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
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 one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now