本文介绍了如何使用Java Socket编程在同一个文件中实现对等代码,即服务器和客户端代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以编写同时用作服务器和客户端的程序?假设我有程序P的三个实例:P1,P2和P3.

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. 在一个用例中,P2向P1请求文件.
  2. 当P1将文件提供给P2时,P1请求文件形式为P3.
  3. P3将文件提供给P1. (此时,P3可以从P1或P2上传/下载.)

我有一些初始代码,可以让我作为客户端从服务器请求文件(提供服务器端口号和文件名).相同的代码还启动了一个Server线程,该线程同时侦听传入的请求.因此,实际上,此代码库用作Peer(客户端和服务器).

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.

推荐答案

是的,这是完全可能的,而且实际上很常见.大多数企业应用程序都采用这种方式.

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

例如,用户请求从Web服务器在浏览器中显示网页.更准确地说,用户要求显示第23条.

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.

然后,Web服务器请求有关第23条的数据库信息.在数据库服务器的响应下,Web服务器构建了格式正确的HTML页面,以显示给用户.

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.

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

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.

因此,是的,每个服务器都可以在端口上侦听,并且同时是另一台服务器的客户端.用Java语言对服务器的经典实现是在服务器端每个客户端使用一个线程.

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.

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

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.

响应您的

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

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.

这篇关于如何使用Java Socket编程在同一个文件中实现对等代码,即服务器和客户端代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 23:42