Connect two client sockets

前端 未结 12 1460
执笔经年
执笔经年 2020-12-10 03:58

Let\'s say Java has two kind of sockets:

  • server sockets \"ServerSocket\"
  • client sockets or just \"Socket\"

Imagine the situation of two

12条回答
  •  时光说笑
    2020-12-10 04:32

    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:

    • A server should have a static public IP.
    • A server, which is after a router connected, has to do "port forwarding". (See UPnP)
    • A client has to know which host it has to connect to (public IP)

    Middle server

    I don't see what you want to do with that third server. Maybe holding the VNCServer's public IP? *Elister* wrote, you want to make a brigde between the client and the VNCServer. I don't see the advantage of it.

    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.

提交回复
热议问题