gpt4 book ai didi

java - 客户端未收到服务器输出

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

我在java中被这个服务器客户端程序所困扰。我的程序达到

System.out.println("Check1");
String name = in.nextLine();

BoardServer 中,然后它只是坐在那里,没有任何内容发送到客户端。

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Scanner;


public class BoardServer {
private static Scanner in;
private static PrintWriter out;
private static int num = 1;

public static void main(String[] args) throws IOException {

final int SBAP_PORT = 8888;
ServerSocket server = new ServerSocket(SBAP_PORT);
System.out.println("Waiting for players to connect...");

while (true) {
Socket s = server.accept();
in = new Scanner(s.getInputStream());
out = new PrintWriter(s.getOutputStream());
System.out.println("Check1");
String name = in.nextLine();
System.out.println("Check2");
String message = "Hello " + name + " you are player number " + num;
num++;
System.out.println("Check3");
out.print(message);
out.flush();
System.out.println("player connected.");
Player client = new Player(s);
Thread t = new Thread(client);
t.start();
}
}
}

最佳答案

当您使用以下命令将名称发送到服务器时:

out.print(name);

使用

out.println(name);

如果您想在服务器上使用 in.nextLine() 读取它,则发送它。这应该可以解决您的问题。

关于java - 客户端未收到服务器输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23188608/

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