gpt4 book ai didi

java - 如何运行两个监听两个不同端口的java套接字服务器实例?

转载 作者:行者123 更新时间:2023-12-01 09:36:22 25 4
gpt4 key购买 nike

场景:
a) 持久连接
b) 单独管理每个服务器-客户端通信
c) 保护系统免于传播异常/错误

我尝试使用以下代码创建服务器套接字监听器的两个实例:

SimpleSocketServers.java

public class SimpleSocketServers {

public static void main(String[] args) throws Exception {
int port1 = 9876;
SimpleSocketServer server1 = new SimpleSocketServer(port1);
server1.startAndRunServer();
System.out.println("Servers : server1 Listening on port: " + port1);

int port2 = 9875;
SimpleSocketServer server2 = new SimpleSocketServer(port2);
server2.startAndRunServer();
System.out.println("Servers : server2 Listening on port: " + port2);
}
}


SimpleSocketServer.java

    import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class SimpleSocketServer {

private ServerSocket serverSocket;
private int port;

public SimpleSocketServer(int port) {
this.port = port;
}

public void startAndRunServer() {
try {
System.out.println("Starting Server at port " + port + " ...");
serverSocket = new ServerSocket(port);
System.out.println("Listening for client connection ...");
Socket socket = serverSocket.accept();
RequestHandler requestHandler = new RequestHandler(socket);
requestHandler.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}

class RequestHandler extends Thread {

private Socket socket;

RequestHandler(Socket socket) {
this.socket = socket;
}

@Override
public void run() {
try {
System.out.println("Client Request Response being processed...");
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream());


} catch (Exception e) {
e.printStackTrace();
}
}
}

但是,它仅创建一个实例,因为控制未从第一个实例的构造函数返回。是否有可能重新获得控制权并同时运行服务器套接字监听器的两个实例? (ps:如果有错误或微不足道的地方,请原谅!)

最佳答案

使用 2 个不同的线程,监听 2 个不同的端口。

Thread ServerThread1 = new Thread(new Runnable() {

@Override
public void run() {
ServerSocket ServerSocketObject = null;
while(true)
{
try {
ServerSocketObject = new ServerSocket(Your_Port_Number1);

Socket SocketObject = ServerSocketObject.accept();
// Your Code Here
SocketObject.close();

} catch (IOException e) {
try {
ServerSocketObject.close();
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
}
}
});

Thread ServerThread2 = new Thread(new Runnable() {

@Override
public void run() {
ServerSocket ServerSocketObject = null;
while(true)
{
try {
ServerSocketObject = new ServerSocket(Your_Port_Number2);

Socket SocketObject = ServerSocketObject.accept();
// Your Code Here
SocketObject.close();

} catch (IOException e) {
try {
ServerSocketObject.close();
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
}
}
});

ServerThread1.start();
ServerThread2.start();

关于java - 如何运行两个监听两个不同端口的java套接字服务器实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38868774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com