为什么我的Socket客户端只收到1个数据包?

时间:2017-05-01 17:24:07

标签: java sockets

我有一个用Java制作的国际象棋服务器和客户端,可以相互传输数据包。当客户端连接时,服务器将数据包发送回客户端。我的程序总是检测到这个包。之后,当我尝试移动一块时,会将一个数据包发送到服务器,服务器会检测到它。它将数据包字符串打印到客户端用于移动该部分的客户端,但客户端从不读取它。我该如何解决这个问题?

连接超类:

public class ChessConnection {
protected Socket socket;
protected PrintWriter output;
protected BufferedReader input;

public void listen() {
    try {
        String line;
        while ((line = input.readLine()) != null) {
            this.inputReceived(line);
        }
    } catch (SocketException e) {
        System.out.println("SocketException: " + e.getMessage());
    } catch (IOException e) {
        System.out.println("IOException: " + e.getMessage());
        e.printStackTrace();
    }
}

public void sendPacket(Packet packet) {
    output.println(packet.serialize());
};

public void inputReceived(String input) {}
}

ClientConnection(客户端):

public class ClientConnection extends ChessConnection {
public ClientConnection() {
    try {
        socket = new Socket("localhost", 4141);
        output = new PrintWriter(socket.getOutputStream(), true);
        input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    } catch (HeadlessException e) {
        e.printStackTrace();
    } catch (UnknownHostException e) {
        System.out.println("Host is unknown");
    } catch (IOException e) {
        e.printStackTrace();
    }

    new Thread(this::listen).start();
}

@Override
public void listen() {
    try {
        String line;

        while ((line = input.readLine()) != null) {
            System.out.println("client received input: " + line); // This doesn't print the move piece packet, but it does print the connection packet
            this.inputReceived(line);
        }
    } catch (SocketException e) {
        System.out.println("SocketException: " + e.getMessage());
    } catch (IOException e) {
        System.out.println("IOException: " + e.getMessage());
        e.printStackTrace();
    }
}
}

ServerConnection(服务器端):

public class ServerConnection extends ChessConnection {
private static int NEXT_ID = 1;
private static ArrayList<ServerConnection> clients = new ArrayList<ServerConnection>();

public int connectionId;
public ServerHandler handler;

public ServerConnection(ServerHandler handler, Socket socket) {
    this.connectionId = NEXT_ID++;
    this.handler = handler;

    try {
        this.socket = socket;
        this.output = new PrintWriter(socket.getOutputStream(), true);
        this.input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    } catch (Exception e) {
        e.printStackTrace();
    }

    new Thread(this::listen).start(); // Starts listening to client

    System.out.println("Client " + this.connectionId + " connected");
    sendPacket(new ConnectionPacket());
}

@Override
public void listen() {
    try {
        String line;
        while ((line = input.readLine()) != null) {
            this.inputReceived(line);
        }
    } catch (SocketException e) {
        System.out.println("SocketException: " + e.getMessage());
        handler.broadcastPacket(new DisconnectionPacket());
        System.out.println("Client " + this.getId() + " disconnected");
    } catch (IOException e) {
        System.out.println("IOException: " + e.getMessage());
        e.printStackTrace();
    }
}

@Override
public void inputReceived(String input) {
    System.out.println("server received input: " + input);
    Packet packet = Packet.toPacket(input);

    switch (packet.getType()) {
    case Packet.DISCONNECTION:
        break;
    case Packet.CONNECTION:
        break;
    case Packet.PIECE_MOVE:
        System.out.println("sending move piece");
        handler.broadcastPacket(packet);
        break;
    case Packet.PAWN_REPLACEMENT:
        break;
    default:
        System.out.println("Received malformed packet: " + input);
        break;
    }
}

public int getId() {
    return this.connectionId;
}

public static ServerConnection getClientFromId(int clientId) {
    for (ServerConnection client : clients) {
        if (client.getId() == clientId) {
            return client;
        }
    }

    return null;
}
}

0 个答案:

没有答案