gpt4 book ai didi

java - 套接字服务器卡住

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

我正在尝试让服务器/客户端从客户端向服务器发送文本,然后向客户端发送回 ok 消息或类似的消息,但是对于一些我看不到的错误,要么服务器被卡住在将 ok 发送回客户端之前,或者客户端没有收到消息(但我认为这是第一个)。如有任何帮助,我们将不胜感激。

这是服务器代码:

class ActiveServer extends Thread {

InputStream inStream;
OutputStream outStream;

public ActiveServer(InputStream inStream, OutputStream outStream) {
this.inStream = inStream;
this.outStream = outStream;
}

@Override
public void run() {
boolean ret = false;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inStream));
PrintWriter writer = new PrintWriter(outStream);) {
String line = null;
while((line = reader.readLine()) != null) {
String[] str = line.split(";");
line = null;
switch (str[0]) {
case "insert" : //ret = SQLOptions.insert(str[1], str[2]);
System.out.println(str[1]);
break;
}
writer.print(ret);
writer.flush();
// As far as i can see it gets stuck at the end of this while, but I don't know why.
}

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

}

public class Server {

private static final int PORT = 39165;

public static void main(String[] args) {

try (ServerSocket server = new ServerSocket(PORT);) {
System.out.println("Servidor online");
ExecutorService service = Executors.newFixedThreadPool(10);
while (true) {
Socket client = server.accept();
InetAddress ip = client.getInetAddress();
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Date time = new Date();
System.out.print(sdf.format(time));
System.out.println(" " + ip + " connected");
InputStream inStream = client.getInputStream();
OutputStream outStream = client.getOutputStream();
service.execute(new ActiveServer(inStream,outStream));
}
} catch (IOException e) {
e.printStackTrace();
}

}

}

这是客户端代码:

public class Telnet {

static Console console = System.console();

public static void connect(String ip, String port) {
try(Socket socket = new Socket(ip, Integer.parseInt(port));
PrintWriter writer = new PrintWriter(socket.getOutputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));) {

String msg = null;
while(true) {
msg = console.readLine();
writer.println(msg);
writer.flush();

if (msg.equals(".quit")) {
System.out.println("Exiting...");
break;
}

String input = reader.readLine();
System.out.println(input);
}


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

public static void main(String[] args) {

if(args.length < 2) {
err.println("Telnet <ip> <port>");
return;
}

if (console == null) {
err.println("A console is not available");
return;
}

connect(args[0], args[1]);

}
}

最佳答案

在服务器端,您编写的响应没有终止换行符:

            writer.print(ret);

但在客户端,您会读到行尾:

            String input = reader.readLine();

BufferedReader#readLine 的文档说:

Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.

因此,客户端将永远等待服务器永远不会发送的换行序列。

关于java - 套接字服务器卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28095188/

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