连接两个客户端套接字 [英] Connect two client sockets

查看:217
本文介绍了连接两个客户端套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设Java有两种套接字:




  • 服务器套接字ServerSocket

  • 客户端套接字或只是套接字



想象一下两个进程的情况:



X =客户端

Y =服务器



服务器进程Y:有一个ServerSocket,正在侦听TCP端口

客户端进程X:通过Socket向Y发送连接请求。



Y:然后 accept() 方法返回一个新的客户端类型Socket,

发生时,两个套接字得到互连,



所以:客户端进程中的套接字与服务器进程中的套接字连接。

然后:通过套接字X读/写就像通过套接字Y读/写一样。

现在,两个客户端套接字相互连接!



但是...

如果创建两个客户端套接字怎么办?在同一过程中,
我希望让它们互联?



...甚至可能吗?



假设如何在不使用中间件的情况下使两个客户端套接字互连ServerSocket?



我通过创建两个Thread来连续读取A并写入B,
和其他用于读取B和写入A来解决它... < br>
但我认为可能是更好的方式...
(客户端 - 服务器方法不需要那些世界耗能的线程)



任何帮助或建议将不胜感激!谢谢






修改:



应用程序示例:现有服务器应用程序可以转换为客户端,
例如VNC服务器,一个客户端套接字连接到VNC服务器,并创建其他客户端套接字(连接到中间服务器) ),然后应用程序互连两个客户端,导致VNC服务器是客户端应用程序!然后,不需要公共IP。



VNCServer --- MyApp ---> |中间服务器| < ---用户

解决方案

首先,不要将接受的客户端(服务器端)调用其套接字a 客户端套接字。这是非常令人困惑的。


假设如何在不使用中间ServerSocket的情况下使两个客户端套接字互连?


这是不可能的。你总是需要建立一个服务器端,它可以接受客户端。现在的问题是:连接的哪一侧应该是服务器端?

这个决定你需要考虑的事情:




  • 服务器应具有静态公共IP。

  • 服务器在连接路由器之后必须执行端口转发。 (见 UPnP

  • 客户必须知道哪个主机必须连接到(公共IP)




中间服务器





我看不出你想用第三台服务器做什么。也许持有VNCServer的公共IP?
* Elister *写道,你想在客户端和VNCServer之间做一个brigde。我没有看到它的优势。


为什么不立即连接到VNCServer?



<但是,如果你真的想要它,你可以做出这样的情况:

 

/ VNCServer(服务器运行)< ; ---。
| |
LAN - |连接到VNCServer
| |
\ MyApp(服务器运行 - >接受中间服务器)< ------。
|
(通过路由器)
|
中间服务器(服务器运行 - >接受客户端)--->连接到您的应用程序
^
|
(通过路由器)
|
客户端 - >连接到中间服务器 - °

这就是没有第三个的情况服务器(我推荐你):

 

/ VNCServer(服务器运行)< ---。
| |
LAN - |连接到VNCServer
| |
\ MyApp(服务器运行 - >接受客户端)< ------。
|
(通过路由器)
|
客户端 - >连接到MyApp --------------------------°






编辑:



I我想现在就明白了:



我们必须想象你的情况如下:

 
您的主服务器(您称之为中间服务器)
(1)| | $ E旧产品价格(记为¥25) $ b | |
您的VNCServer< ---------------------------->客户
(5)(3)

(1) VNCServer连接到主服务器。那么,主服务器获得了VNCServer的IP。

(2)客户端连接到主服务器。

(3)现在主服务器知道服务器和客户端在哪里。然后他发送到服务器所在的客户端。然后客户端将连接到他从主服务器收到的IP。这当然是来自VNCServer的IP。

(5) VNCServer正在运行的服务器是接受客户端。



现在可以开始桌面共享。



我认为这是你可以提供的最佳建议情况。

Of当然是用Java写的。


Let's say Java has two kind of sockets:

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

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:

  • 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.

这篇关于连接两个客户端套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆