gpt4 book ai didi

java - 处理更多客户

转载 作者:行者123 更新时间:2023-12-01 12:06:15 24 4
gpt4 key购买 nike

现在我的服务器一次只能处理一个客户端。我正在尝试使用线程以便服务器可以处理多个客户端,但我做错了。我在 try/catch 子句中添加了 serverSocket 接受客户端的线程,但这没有什么区别。我没有收到错误或任何错误,但它就是不起作用。所以我想做的是让服务器不会卡住在一个客户端上,但仍然接受多个客户端。

这是服务器代码:

import java.io.*;
import java.net.*;

public class Server {

private BufferedReader reader;
private PrintWriter writer;
private int port;

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

private String getSeverAddress() {
String host = null;
try {
InetAddress adr = InetAddress.getLocalHost();
host = adr.getHostAddress();

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

return host;
}

public void startServer() {

print("Contact this sever on address: " + getSeverAddress() + " port: " + port);

ServerSocket ss = null;
Socket socket = null;
Thread clientThread = null;


try {
ss = new ServerSocket(port);
socket = ss.accept();

clientThread = new Thread(new Client(socket));
clientThread.start();

reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
writer = new PrintWriter(socket.getOutputStream(), true);

String msg = null;

while (( msg = reader.readLine()) != null) {
print("System out: " + msg);

if(msg.equals("Bye")) {
print("Client left");
break;
}
}

ss.close();
socket.close();
reader.close();
writer.close();

} catch(SocketException e) {
e.printStackTrace();
} catch (IOException i ) {
i.printStackTrace();
return;
}

}

private void print(String msg) {
System.out.println(msg);
}

public static void main(String[] args) {

Server server = new Server(1111);
server.startServer();

}
}

这是客户端代码:

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

public class Client implements Runnable{

private Socket client;
private BufferedReader reader;
private PrintWriter writer;

public Client(Socket socket)
{
client = socket;

try{
reader = new BufferedReader(new InputStreamReader(client.getInputStream()));
writer = new PrintWriter(client.getOutputStream(), true);
} catch (Exception e) {
e.printStackTrace();
return;
}
}

@Override
public void run() {

String msg = null;
BufferedReader r = null;

try {
r = new BufferedReader(new InputStreamReader(System.in));
} catch (Exception e1) {
e1.printStackTrace();
}

System.out.println("Write message to server");

while(true) {
try {
msg = r.readLine();

if(msg.equals("Quit") || msg == null) {
print("Disconnect");
break;
}
} catch (IOException e) {
e.printStackTrace();
}
writeToServer(msg);
}

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

public void writeToServer(String msg) {
writer.println(msg);
}

private void print(String msg) {
System.out.println(msg);
}

public static void main(String[] args) {
Socket socket = null;

try {
socket = new Socket("localhost", 1111);

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

Client client = new Client(socket);
client.run();
}

}

最佳答案

您仍在尝试在主线程中处理客户端。主线程应该只接受新连接并启动新线程。您还必须在循环中执行accept,以便可以接受多个连接:

ss = new ServerSocket(port);
while(true) {
Socket socket = ss.accept();
Thread clientThread = new Thread(new Runnable() {
public void run() {
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter writer = new PrintWriter(socket.getOutputStream(), true);

String msg = null;

while (( msg = reader.readLine()) != null) {
print("System out: " + msg);

if(msg.equals("Bye")) {
print("Client left");
break;
}
}

socket.close();
reader.close();
writer.close();
}});
clientThread.start();
}

关于java - 处理更多客户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27574643/

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