简体   繁体   中英

Java sockets - Client doesn't read the strings from server's socket

I have two Java projects that communicate with sockets through strings. One is a client and the other is a server. The server accepts the connection trough the "ServerSocket" and creates a new "Session" (Thread) with a freshly created "Socket".

Meanwhile, the client only has a "Socket", once that socket is connected, the client creates a "ClientSession" (Thread, pretty similar to "Session).

What I want is the server to ask the client his username through the "USERNAME" string, and the client to answers with his username. But the answer never come back. I think it's maybe a synchronisation problem with the BufferedReader and the PrintWriter lines fired at the wrong place.

-----------Server Code---------------

Login Class:

public class Login implements Runnable {

private ArrayList<Session> sessions;
private int port;
private Thread thread;
private ServerSocket serverSocket;
private Game game;

public Login(int port, Game game) throws Exception {
    this.sessions = new ArrayList();
    this.port = port;
    this.serverSocket = new ServerSocket(port);
    this.game = game;
    this.thread = new Thread(this);
    this.thread.start();
}

@Override
public void run() {
    while(true) {
        Socket socket;
        try {
            System.out.println("[Server Network] Waiting for a new player to log in.");
            socket = serverSocket.accept();
            sessions.add(new Session(socket,this));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

public void addPlayer(Player p) {
    this.game.addPlayer(p);
}

}

Session Class:

public class Session implements Runnable {

private String username;
private Thread thread;
private Socket socket;
private BufferedReader br;
private OutputStreamWriter os;
private PrintWriter out;
private Login login;

private boolean ready;

public Session(Socket socket, Login login) throws IOException {
    this.login = login;
    this.socket = socket;
    this.ready = false;

    this.br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    this.os = new OutputStreamWriter(socket.getOutputStream());
    this.out = new PrintWriter(os);

    System.out.println("[Server network] Session created for client with ip: "+socket.getInetAddress().getHostAddress());

    this.thread = new Thread(this);
    this.thread.start();
}

public void send(String m) {
    System.out.println("[Server network] Sending message: "+m);
    out.write(m);
    out.flush();
    System.out.println("[Server network] Message sent: "+m);
}


@Override
public void run() {
    while (true) {
        if (!ready) {
            try {
                this.send("USERNAME");
                this.username = br.readLine();
                this.ready = true;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        try {
            String request = br.readLine();
            System.out.println(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

}

-----------Client Code-------------

Game Class:

public class Game {

private Window window;

private LocalPlayer localPlayer;
private ArrayList<Player> players;
private Map map;

private String username;

private String serverIpAddress;
private ClientSession clientSession;
private static int port = 23123;

public Game(Window window, Map map) throws UnknownHostException, IOException {
    System.out.println("Game Launched.");

    //Asks for the server ip address and creates a session.
    //this.serverIpAddress = JOptionPane.showInputDialog(null,"Please enter the server ip address.",JOptionPane.QUESTION_MESSAGE);
    this.serverIpAddress = "localhost";

    //this.username = JOptionPane.showInputDialog(null,"What is your username?",JOptionPane.QUESTION_MESSAGE);
    this.username = "GriffinBabe";

    this.clientSession = new ClientSession(new Socket(serverIpAddress,port),this);

    this.window = window;
    this.localPlayer = new LocalPlayer(new Warrior(),this.username,'R');
    this.map = map;

}

public LocalPlayer getLocalPlayer() {
    return this.localPlayer;
}

public Map getMap() {
    return map;
}

}

ClientSession Class:

public class ClientSession implements Runnable {

private Socket socket;
private Thread thread;
private BufferedReader br;
private OutputStreamWriter os;
private PrintWriter out;
private Game game;

public ClientSession(Socket socket, Game game) throws IOException {
    this.game = game;
    this.socket = socket;

    this.br = new BufferedReader(new InputStreamReader(this.socket.getInputStream()));
    this.os = new OutputStreamWriter(this.socket.getOutputStream());
    this.out = new PrintWriter(os);

    System.out.println("[Client network] Session created for server with ip: "+socket.getInetAddress().getHostAddress());

    this.thread = new Thread(this);
    this.thread.start();
}

public void send(String m) {
    System.out.println("[Client network] Sending message: "+m);
    out.write(m);
    out.flush();
    System.out.println("[Client network] Message sent: "+m);
}

@Override
public void run() {
    while(true) {
        try {
            String request = br.readLine();
            System.out.println(request);
            switch (request) {
            case "USERNAME":
                send(game.getLocalPlayer().getUsername());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

}

Here are the Console logs:

Server (launched first, of course):

服务器控制台日志

Client :

客户端控制台日志

Your server sends "USERNAME" and then waits. The client reads the next line, and blocks until it receives it. So you have a deadlock, since the server never sends an EOL character. If the client reads a line, the server should send a line.

In your send methods, try changing out.write(m) to out.println(m) .

The readLine() method will keep reading until it hits a line terminator or end of stream, whichever comes first. The println() method will automatically put a line terminatorat the end of the string you are trying to send.

https://docs.oracle.com/javase/7/docs/api/java/io/PrintWriter.html#println()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM