Reputation: 3
I am getting snagged up on this server client program in java. My program gets to
System.out.println("Check1");
String name = in.nextLine();
in the BoardServer
and then it just sits there and nothing gets sent to the client.
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();
}
}
}
Upvotes: 0
Views: 109
Reputation: 3534
When you send the name to the server with :
out.print(name);
use
out.println(name);
to send it if you want to read it with in.nextLine()
on the server. This should solve your problem.
Upvotes: 1