开发者

How to implement Peer code i.e., Server and Client code in the same file using java Socket programming

开发者 https://www.devze.com 2023-03-01 07:34 出处:网络
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.

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 t开发者_开发技巧he "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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消