如何使用java Socket编程在同一个文件中实现Peer代码,即服务器和客户端代码

发布于 2024-11-03 17:25:19 字数 540 浏览 0 评论 0原文

是否可以编写一个既充当服务器又充当客户端的程序?假设我有程序 P 的三个实例:P1、P2 和 P3。

  1. 在一个用例中,P2 向 P1 请求文件。
  2. 当 P1 向 P2 提供文件时,P1 向 P3 请求文件。
  3. P3 将文件提供给 P1。 (此时,P3可以从P1或P2上传/下载。)

从概念上讲,我应该能够做到 这是通过在 a 中运行服务器部分来实现的 每次请求到来时都会线程。 有不同的方法吗?

编辑:


我有一些初始代码,允许我作为客户端从服务器请求文件(提供服务器端口号和文件名)。相同的代码还启动一个服务器线程来侦听传入的请求。因此,实际上,该代码库充当对等点(客户端和服务器)。

但是,我现在的问题是在发出第一个文件请求后,代码在服务器部分永远循环,实际上不允许我作为客户端发出任何额外的请求。我的问题是:当服务器侦听端口时,我是否可以保持套接字打开,而无需服务器无限循环?这样,我可以在“后台”运行服务器,并能够根据需要向客户端发送命令。

Is it possible to write a program that serves both as a Server and a Client? Suppose I have three instances of the program P: P1, P2, and P3.

  1. In one use case, P2 requests file from P1.
  2. While P1 is serving the file to P2, P1 requests file form P3.
  3. P3 serves the file to P1. (At this point, P3 is able to upload/download from P1 or P2.)

Conceptually, I should be able to do
this by running the server part in a
thread every time a request comes in.
Is there a different approach to this?

EDIT:


I have some initial code that allows me, as a Client, to request a file from a Server (provided the server port number and file name). The same code also starts up a Server thread that listens for incoming requests alongside. So, in effect, this code base serves as a Peer (both client and server).

But, my issue now is after making the first request for a file, the code loops forever in the Server part, effectively not allowing me to make any additional request as a Client. My question then is: Can I keep a socket open while the server listens to the port without needing for the Server to loop infinitely? This way, I can run the Server in the "background" and be able to send commands to the Client as needed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

单调的奢华 2024-11-10 17:25:19

是的,这是完全可能的,而且事实上很常见。大多数企业应用程序都利用了这一点。

例如,用户从网络服务器请求在浏览器中显示网页。更准确地说,用户请求显示文章 23。

然后,Web 服务器请求有关文章 23 的数据库信息。根据数据库服务器的响应,Web 服务器构建格式良好的 HTML 页面以显示给用户。

从概念上讲,使用网络只是一种通信方式,即发送或接收信息。这与从文件中写入/读取没有太大区别。而在linux中,这也是一样的。

所以是的,每个服务器都可以侦听一个端口,同时作为另一个服务器的客户端。 JAVA 语言中服务器的经典实现是在服务器端为每个客户端使用一个线程。

这意味着当 P2 连接到 P1 时,P1 和 P2 之间建立了一个新的连接,并且 P2 使用线程将响应写入 P1...在大多数简单情况下,同一个线程实际上会从其他服务器请求丢失的信息,因此发送请求并等待响应。

响应您的编辑:

服务器的经典线程实现是通过无限循环监听端口。每次检测到来自客户端的连接请求时,您都会建立一个连接,协商客户端和服务器上用于此通信的端口,并将此连接委托给另一个线程(通常使用线程池,以免重复使用线程)通信完成后下一个客户端)。

这样,您始终可以使用线程来响应客户端的连接请求。

Yes this is perfectly possible, and in fact very common. Most enterprise application take adventage of this.

For exemple the user request a web page to show in the browser from the web server. More precisely, the user request to show article 23.

The web server then request the database information concerning article 23. With the response from the database server, the web server construct a well formated HTML page to display to the user.

Conceptually, using network is just a way to communicate, that's it sending or receiving information. This isn't much different from writing/reading from a file. And in linux, this is the same.

So yes each server can listen on a port and at the same time being client of another server. Classical implementation for a server in JAVA word is to use one thread per client on the server side.

This mean that when P2 connect to P1, a new connexion is established between P1 and P2 and that P2 use a thread to write the response to P1... In most simple cases, the same thread will in fact request missing information from other server, so sending request and waiting for the response.

Responding to your edit :

Classical threaded implementation of a server is to listen on a port with some infinite loop. Each time you detect a connection request from a client, you establish a connection, negotiate the port to be used on client and server for this communication and you delegate this connection to another thread (typically using a thread pool, as to not reuse thread for the next client when communication is finished).

This way you always have always your thread available to respond to connect request from clients.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文