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

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.

In one use case, P2 requests file from P1.

While P1 is serving the file to P2, P1 requests file form P3.

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.

解决方案

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值