我在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();
}
}
}
答案 0 :(得分:1)
使用以下命令将名称发送到服务器时
out.print(name);
使用
out.println(name);
如果要在服务器上使用in.nextLine()
读取它,请发送它。这应该可以解决你的问题。