This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Sockets and Internet Protocols and the fly likes Creating a Pipe that connects 2 sockets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Sockets and Internet Protocols
Bookmark "Creating a Pipe that connects 2 sockets" Watch "Creating a Pipe that connects 2 sockets" New topic
Author

Creating a Pipe that connects 2 sockets

Michael Herrmann
Ranch Hand

Joined: Dec 06, 2003
Posts: 60
Hi,
I'm trying to create a class that is able to link two Sockets. Whatever comes in at Socket A should be forwarded to Socket B.
Here's what the class, I called it "SocketConnector", looks like:


It extends Thread, because I want to be able to create a two sided communication, by connecting Socket A to Socket B and vice versa.

The problem is, that I get the following error:

java.net.SocketException: socket closed
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(Unknown Source)
at java.net.SocketInputStream.read(Unknown Source)
at SocketConnector.run(SocketConnector.java:37)

when linking two existing and open Sockets through
new SocketConnector(s1, s2).start();

What bothers me is, that everything works fine, if the class SocketConnector doesn't extend Thread.

Thanks in advance,
Michael Herrmann
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Hmmm, just guessing ... any chance the main thread that spawns a connecter, closes the sockets and exits? Like

I've done exactly that to myself before and wondered why it didn't work. I made the main read from keyboard before closing and exiting so I could watch the test run a while and hit Enter to end it.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Michael Herrmann
Ranch Hand

Joined: Dec 06, 2003
Posts: 60
Thanks for your answer.

It's slightly more complicated than a single main method. I extend the following class:



The testcode itself looks like this:



Thanks again,
Michael Herrmann
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
One of the problems is that the connected socket is closed immediately after connecting:

Place s.close(); out of the loop.

Also:

No socket is listening at port 4444. There is a connection of a socket to localhost:4444, where no process is listening!

I modified the program to do the following:
A server socket listens at 4445.
Socket test.in connects to it.
Socket test.out connects to it.
A SocketConnector thread is started, its run method reads from the first connected socket: the one to which test.in was connected (SocketConnector.in). And it writes to the second connected socket: the one to which test.out was connected (SocketConnector.out).
A thread is started. It reads from the keyboard and writes to test.in. Via the magic of sockets this will appear at SocketConnector.in. From where SocketConnector.run is reading.
A thread is started. It reads from test.out and prints to System.out. SocketConnector.run reads from SocketConnector.in and writes to SocketConnector.out, to wich test.out was connected.

The output is a bit of mess because all is printed to System.out, thus it's better to write single caracters and press Enter.




By the way you will need to provide your own ip in the code.


SCJP2. Please Indent your code using UBB Code
Michael Herrmann
Ranch Hand

Joined: Dec 06, 2003
Posts: 60
I must be blind! The problem was, as you correctly said, the s.close(); call in VirtualServer's onConnect method.
But there actually is a process listening on port 4444. It's a tool, not written by me, to which clients can connect. Everything a client sends is printed on the screen (You can d/l the tool here: click).

"By the way you will need to provide your own ip in the code."
Isn't "localhost" possible too?

Thanks again,
Michael Herrmann
[ June 11, 2004: Message edited by: Michael Herrmann ]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120

"By the way you will need to provide your own ip in the code."
Isn't "localhost" possible too?

Yes it's possible. I meant to run run my example, because I hardcoded the IP.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating a Pipe that connects 2 sockets
 
Similar Threads
Singleton/Static in Multi threading scenario
Not Sure
Multithread-call center support
Socket is not receiving the first request after long Ideal time
Reader -> InputStream conversion