简体   繁体   English

Java计算机到计算机的连接?

[英]Java computer to computer connection?

Is it possible to have two computers socket-connect to each other without any server involved in Java? 是否可以使两台计算机通过套接字相互连接而无需Java参与任何服务器? If so, how? 如果是这样,怎么办? I've only seen examples of server-client connections where the server is permanent. 我仅看到服务器永久的服务器-客户端连接的示例。

For example: 例如:

1. 2 people, person A and person B, want to connect using this application
2. Person A runs application, clicks "host"
3. Application hands person A a code (IP+port?) and begins listening.
4. Person B clicks "join" and types in code.
5.  Person B's computer sees Person A's computer and they connect. Person A accepts connection.

I've never really understood this well. 我从来没有真正理解得很好。 Please pardon me if this is a bad question. 如果这是一个不好的问题,请原谅我。

In your scenario, person A's PC would be acting as the server and person B's would be acting as the client. 在您的方案中,人员A的PC将充当服务器,而人员B的将充当客户端。 Even if it is only temporary, the fact that A is listening for a connection means that it is acting as a server. 即使只是临时的,A 也在监听连接这一事实意味着A充当服务器。

By definition, one side has to initiate the connection (ie issue the connect request). 根据定义,一方必须发起连接(即发出连接请求)。 That means the other side has to have a server socket open to receive the connection. 这意味着另一端必须打开服务器套接字才能接收连接。 You could use the same code on both sides by making both implement client and server functionality, and assume the appropriate role (server or client) depending on which user initiates the connection. 通过实现客户端和服务器功能,可以在双方上使用相同的代码,并根据哪个用户发起连接来承担适当的角色(服务器或客户端)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM