Java套接字编程:服务器循环无限执行
虽然我明白Java套接字编程:服务器循环无限执行,java,sockets,Java,Sockets,虽然我明白 while(true){ } 生成一个无限循环,我的理解是 while(true){ blockingCall() } 由于阻塞调用的性质,允许执行此循环x次(x可能介于0和达到给定计算机资源限制的数字之间),即如果对blockingCall()方法进行了3次调用,而第3次调用从未返回,则意味着程序应在那里等待。这是一个实现的主题,它没有按照我期望的方式工作。 我正在使用Java套接字实现一个客户机/服务器程序。是一个参考链接,用于了解我的客户机正在做什么(它只是请求连接到运行在
while(true){
}
生成一个无限循环,我的理解是
while(true){
blockingCall()
}
由于阻塞调用的性质,允许执行此循环x
次(x可能介于0和达到给定计算机资源限制的数字之间),即如果对blockingCall()方法进行了3次调用,而第3次调用从未返回,则意味着程序应在那里等待。这是一个实现的主题,它没有按照我期望的方式工作。
我正在使用Java套接字实现一个客户机/服务器程序。是一个参考链接,用于了解我的客户机正在做什么(它只是请求连接到运行在特定端口上的服务器并发送消息。服务器反转该消息并发送回客户机)。我正在尝试以这样一种方式实现该服务器,即该服务器允许的连接数是有限制的。如果请求连接的客户端数量超过此限制,则其他请求将排队,直至达到最大限制。一旦超过此最大限制,服务器只需将一条消息写入日志,说明“不再接受任何连接”。下面是我的服务器程序:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.concurrent.*;
public class MultithreadedServer {
private static BlockingQueue<Socket> queuedSockets = new ArrayBlockingQueue<>(1); //max queued connections.
private static Semaphore semaphoreForMaxConnectionsAllowed = new Semaphore(2); //max active connections being served.
private static void handleClientConnectionRequest(final Socket newSocketForClientConnection, final Semaphore maxConnectionSemaphore) {
new Thread(new Runnable() {
@Override
public void run() {
try (
BufferedReader socketReader = new BufferedReader(new InputStreamReader(newSocketForClientConnection.getInputStream()));
PrintWriter socketWriter = new PrintWriter(newSocketForClientConnection.getOutputStream(), true)
) {
maxConnectionSemaphore.acquire();
String serverMsg;
String clientMsg;
SocketAddress clientSocket = (InetSocketAddress) newSocketForClientConnection.getRemoteSocketAddress();
while ((clientMsg = socketReader.readLine()) != null) {
if (clientMsg.equalsIgnoreCase("quit")) {
maxConnectionSemaphore.release();
break;
}
System.out.println("client with socket " + clientSocket + " sent MSG : " + clientMsg);
serverMsg = reverseString(clientMsg);
socketWriter.println(serverMsg);
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("Closing client upon client's request.");
}
}
}).start();
}
private static String reverseString(String clientMsg) {
synchronized (clientMsg) {
StringBuffer stringBuffer = new StringBuffer();
for (int i = clientMsg.length() - 1; i >= 0; i--) {
stringBuffer.append(clientMsg.charAt(i));
}
return stringBuffer.toString();
}
}
public static void main(String[] args) throws IOException {
boolean shouldContinue = true;
if (args.length != 1) {
System.out.println("Incorrect number of arguments at command line");
System.exit(1);
}
ServerSocket serverSocket = null;
try {
Integer portNumber = Integer.parseInt(args[0]);
serverSocket = new ServerSocket(portNumber);
int connectionNumber = 0;
System.out.println("Server listening on port# : " + args[0]);
//main thread...
while (shouldContinue) {
Socket newServerSocketForClientConnection = null;
newServerSocketForClientConnection = queuedSockets.poll();
if (newServerSocketForClientConnection == null) {
newServerSocketForClientConnection = serverSocket.accept();
connectionNumber++;
System.out.println("Created new socket upon client request. ConnectionCOunt = " + connectionNumber);
processConnection(newServerSocketForClientConnection);
} else {
//i.e. queue has a socket request pending.
System.out.println("Picking queued socket..");
processConnection(newServerSocketForClientConnection);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (serverSocket != null) {
serverSocket.close();
}
}
}
private static void processConnection(Socket newServerSocketForClientConnection) {
if (semaphoreForMaxConnectionsAllowed.availablePermits() > 0) {
handleClientConnectionRequest(newServerSocketForClientConnection, semaphoreForMaxConnectionsAllowed);
} else {
//System.out.println("Since exceeded max connection limit, adding in queue.");
if (queuedSockets.offer(newServerSocketForClientConnection)) {
System.out.println("connectionRequest queued because no more space on server. QueuedSocketList size : " + queuedSockets.size());
}else{
System.out.println("No space available for client connections. Can not be queued too.");
}
}
}
}
导入java.io.BufferedReader;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.io.PrintWriter;
导入java.net.InetSocketAddress;
导入java.net.ServerSocket;
导入java.net.Socket;
导入java.net.SocketAddress;
导入java.util.concurrent.*;
公共类多线程服务器{
private static BlockingQueue queuedSockets=new ArrayBlockingQueue(1);//最大排队连接数。
私有静态信号量semaphoreForMaxConnectionsAllowed=新信号量(2);//正在服务的最大活动连接数。
私有静态void handleClientConnectionRequest(最终套接字newSocketForClientConnection,最终信号量maxConnectionSemaphore){
新线程(newrunnable()){
@凌驾
公开募捐{
试一试(
BufferedReader socketReader=新的BufferedReader(新的InputStreamReader(newSocketForClientConnection.getInputStream());
PrintWriter socketWriter=新的PrintWriter(newSocketForClientConnection.getOutputStream(),true)
) {
maxConnectionSemaphore.acquire();
字符串serverMsg;
字符串clientMsg;
SocketAddress clientSocket=(InetSocketAddress)newSocketForClientConnection.getRemoteSocketAddress();
while((clientMsg=socketReader.readLine())!=null){
if(clientMsg.equalsIgnoreCase(“退出”)){
maxConnectionsMaphore.release();
打破
}
System.out.println(“带套接字的客户端”+clientSocket+”发送消息:“%clientMsg”);
serverMsg=反向限制(clientMsg);
socketWriter.println(serverMsg);
}
}捕获(IOE异常){
e、 printStackTrace();
}捕捉(中断异常e){
e、 printStackTrace();
}最后{
System.out.println(“根据客户请求关闭客户”);
}
}
}).start();
}
私有静态字符串反向限制(字符串clientMsg){
已同步(clientMsg){
StringBuffer StringBuffer=新的StringBuffer();
对于(int i=clientMsg.length()-1;i>=0;i--){
stringBuffer.append(clientMsg.charAt(i));
}
返回stringBuffer.toString();
}
}
公共静态void main(字符串[]args)引发IOException{
布尔值shouldContinue=true;
如果(args.length!=1){
System.out.println(“命令行中的参数数量不正确”);
系统出口(1);
}
ServerSocket ServerSocket=null;
试一试{
整数端口号=Integer.parseInt(args[0]);
serverSocket=新的serverSocket(端口号);
int connectionNumber=0;
System.out.println(“服务器侦听端口:”+args[0]);
//主线程。。。
while(应该继续){
Socket newServerSocketForClientConnection=null;
newServerSocketForClientConnection=queuedSockets.poll();
if(newServerSocketForClientConnection==null){
newServerSocketForClientConnection=serverSocket.accept();
connectionNumber++;
System.out.println(“根据客户端请求创建新套接字。ConnectionCOunt=“+connectionNumber”);
processConnection(newServerSocketForClientConnection);
}否则{
//i、 队列有一个套接字请求挂起。
System.out.println(“拾取排队套接字…”);
processConnection(newServerSocketForClientConnection);
}
}
}捕获(IOE异常){
e、 printStackTrace();
}最后{
if(serverSocket!=null){
serverSocket.close();
}
}
}
私有静态void processConnection(套接字newServerSocketForClientConnection){
if(信号量FormaxConnectionsAllowed.availablePermits()>0){
handleClientConnectionRequest(newServerSocketForClientConnection,信号量FormaxConnectionsAllowed);
}否则{
//System.out.println(“因为超过了最大连接限制,正在添加队列”);
if(queuedSockets.offer(newServerSocketForClientConnection)){
System.out.println(“connectionRequest已排队,因为服务器上没有更多空间。QueuedSocketList大小:“+queuedSockets.size()”);
}否则{
//main thread...
while (shouldContinue) {
Socket newServerSocketForClientConnection = null;
// poll for a pending connection in the queue
newServerSocketForClientConnection = queuedSockets.poll();
// if a pending connection exists, go to else...
if (newServerSocketForClientConnection == null) {
...
} else {
// queue has a socket request pending, so we process the request...
System.out.println("Picking queued socket..");
processConnection(newServerSocketForClientConnection);
}
}
// if there are no permits available, go to else...
if (semaphoreForMaxConnectionsAllowed.availablePermits() > 0) {
handleClientConnectionRequest(newServerSocketForClientConnection, semaphoreForMaxConnectionsAllowed);
} else {
// BlockingQueue.offer() puts this connection immediately back into the queue,
// then the method exits
if (queuedSockets.offer(newServerSocketForClientConnection)) {
System.out.println("connectionRequest queued because no more space on server. QueuedSocketList size : " + queuedSockets.size());
}else{
System.out.println("No space available for client connections. Can not be queued too.");
}
}
//main thread...
while (shouldContinue) {
Socket newServerSocketForClientConnection = null;
// poll immediately gets the same request that was
// removed in the previous iteration
newServerSocketForClientConnection = queuedSockets.poll();
// Once something is in the queue, this condition will
// never be met, so no new incoming connections
// can be accepted
if (newServerSocketForClientConnection == null) {
...
} else {
// process the same request again, forever, or until
// a connection is freed up. Meanwhile, all other
// incoming requests are being ignored.
System.out.println("Picking queued socket..");
processConnection(newServerSocketForClientConnection);
}
}