Let's say Java has two kind of sockets:
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 : sends a connection request through a "Socket" to Y.
Y: Then the accept()
method returns a new client type "Socket",
when it occurs, the two Sockets get "interconnected",
So: the socket in client process, is connected with the socket in the server process.
Then: reading/writing through socket X is like reading/writing through socket Y.
Now, two Client Sockets get interconnected!!
But...
What if I create the two Client sockets in same process,
and I want to get them "interconnected" ?
... even possible?
Let's say how to have two client socket get interconnected 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...
(Those world-energy-consuming threads are not necessary with the client-server approach)
Any help or advice would be appreciated!! Thanks
Edit:
Example of application: "An existent server application could be converted to a client one", For example VNC server, one client socket connects to the VNC server, and other client socket is created (to connect to a middle server), then the application interconnects the two client resulting the VNC server is a client application! And then, no public IP is needed.
VNCServer---MyApp---> |middle server| <---User
First of all, don't call an accepted client (server-side) its socket a Client Socket
. That is very confusing.
Let's say how to have two client socket get interconnected without using an intermediate ServerSocket?
That is impossible. You always have to make a server-side, which can accept clients. Now the question is: which side of the connection should be the server-side?
Things you have to think about by this decision:
Middle server
Why don't make immediately a connection to the VNCServer?
But if you really want it, you can make a situation like this:
/ VNCServer (Server Running) <---. | | LAN -| Connects to VNCServer | | \ MyApp (Server Running --> Accepts from Middle Server) <------. | (Through a router) | Middle server (Server Running --> Accepts client) ---> Connects to Your App ^ | (Through a router) | Client --> Connects to Middle Server --°
And this is how it looks without the third server (What I recommend you):
/ VNCServer (Server Running) <---. | | LAN -| Connects to VNCServer | | \ MyApp (Server Running --> Accepts Clients) <------. | (Through a router) | Client --> Connects to MyApp --------------------------°
EDIT:
I think I got it now:
We have to visualize your situation like this:
Your Main Server (What you called middle server) (1) | | (2) /⁻⁻⁻⁻⁻⁻⁻⁻⁻⁻⁻⁻⁻⁻⁻⁻⁻⁻/ \⁻⁻⁻⁻⁻⁻⁻⁻⁻⁻⁻⁻⁻⁻⁻⁻⁻\ | | Your VNCServer <----------------------------> The client (5) (3)
(1)
The VNCServer connects to the main server. So, then the main server got the VNCServer its IP.
(2)
The client connects to the main server.
(3)
Now the main server knows where server and client are. Then he sends to the client where the server is. Then the client will connect to the IP he received from the main server. That is of course the IP from the VNCServer.
(5)
The VNCServer is running is server to accept the client.
Now desktop sharing can start.
I think this is the most recommend situation you can have.
Of course writing it in Java is to you.