Connect two client sockets

Posted by Hernán Eche on Stack Overflow See other posts from Stack Overflow or by Hernán Eche
Published on 2010-04-05T12:08:18Z Indexed on 2010/04/05 12:13 UTC
Read the original article Hit count: 409

Filed under:
|
|
|

Good morning, let's say Java has two kind of sockets...

  1. server sockets "ServerSocket"
  2. client sockets or just "Socket"

////so Simple !

Imagine the situation of two processes:

X Client <--> Y Server

The server process Y : has a "ServerSocket", that is listening to a TCP port

The client process X : send a connection request through a -client type- "Socket" X

////so Simple !

then the accept() method (in server Y) returns a new client type "Socket",

when it occurs, great the two Sockets get "interconected",

so the -client socket- in client process, is connected with the -client socket- in the server process

then (reading/writing in socket X is like reading/writing in socket Y, and viceversa. )

TWO CLIENT SOCKETS GET INTERCONECTED!!

////so Simple !

BUT... (there is always a But..)

What if I create the two CLIENT sockets in same process, and I want to get them "interconected" ?

////mmm Complex =(... even posible?

Let's say how to have TWO CLIENT SOCKETS GET INTERCONECTED WITHOUT using an intermediate ServerSocket ?

I 've solved it.. by creating two threads for continuously reading A and writing B, and other for reading B and writng A... but I think could be a better way..(or should!) (Those world-energy-consuming threads are not necessary with the client-server aproach)

Any help or advice would be appreciated!! Thanks

© Stack Overflow or respective owner

Related posts about java

Related posts about sockets