• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Creating a Pipe that connects 2 sockets

 
Michael Herrmann
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Herrmann
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Michael Herrmann
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

"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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic