如何修复“服务器与客户端之间没有通信”

时间:2019-07-22 05:21:00

标签: java tcp

这是为我提供的代码。我正在尝试解决客户端和服务器之间的连接问题。即使两者都启动,它们也不会连接。

这是一款基于Java的Battleship游戏,它将允许两个用户在不同的设备上相互玩耍。我不确定为什么两者无法连接,甚至调试器也无法帮助我解决问题。

public class GameClient
{
    private Socket clientSocket;
    private PrintWriter out;
    private BufferedReader in;

    public void openConnection(String ip, int port)
    {
        try
        {
            clientSocket = new Socket(ip, port);
            out = new PrintWriter(clientSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

        }
        catch (Exception e)
        {
            System.out.println("Error opening client socket");
        }
    }

    public String sendMessage(String msg)
    {
        String resp = "";
        try
        {
            out.println(msg);
            resp = in.readLine();
        }
        catch (Exception e)
        {
            System.out.println("Error sending message from Client");
        }

        return resp;
    }

    public void stop()
    {
        try
        {
            in.close();
            out.close();
            clientSocket.close();
        }
        catch (Exception e)
        {
            System.out.println("Error stopping client");
        }
    }

    public static void main(String[] args)
    {
        GameClient client = new GameClient();

        client.openConnection("10.7.232.200", 3333);
        String response = client.sendMessage("1,2");
        System.out.println(response);
        client.stop();
    }
}

public class GameServer
{
    private ServerSocket serverSocket;
    private Socket clientSocket;
    private PrintWriter out;
    private BufferedReader in;

    public void start(int port)
    {
        try
        {
            serverSocket = new ServerSocket(port);
            clientSocket = serverSocket.accept();
            out = new PrintWriter(clientSocket.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

            String move = in.readLine();

            System.out.println(move);

            out.println("6,1");

        }
        catch (Exception e)
        {
            System.out.println("Socket opening error");
        }

    }

    public void stop()
    {
        try
        {
            in.close();
            out.close();
            clientSocket.close();
            serverSocket.close();
        }
        catch (Exception e)
        {
            System.out.println("Error closing sockets");
        }
    }


    public static void main(String [] args)
    {
        GameServer server = new GameServer();
        server.start(3333);
        server.stop();
    }
}

public class PlayBattleship
{
    public static void main(String[] args)
    {
        GameClient client = new GameClient();

        client.openConnection("10.7.232.200", 3333);

        //System.out.println(response);


        BattleshipGame game = new BattleshipGame();

        while (!game.checkEndgame())
        {
            game.getGuess(client);
        }

        client.stop();
    }
}

客户端和服务器应保持连接并保持连接状态,直到游戏完成 编辑:我已经彻底阅读了API文档,但仍然无法理解问题。

1 个答案:

答案 0 :(得分:1)

您代码中的服务器不等待传入请求,它仅处理一个传入请求,然后由于启动它的主要方法的性质而自行终止。

您需要让服务器等待请求并且不死。查看下面的代码片段以了解其逻辑。

此外,如果您无法在捕获的方法中对其做任何有意义的事情,请始终尝试抛出异常。在代码中,即使在服务器中捕获了异常,服务器的main方法仍将执行。启动方法

public class GameServer {
    private ServerSocket serverSocket;
    private Socket clientSocket;
    private PrintWriter out;
    private BufferedReader in;

    public ServerSocket start(int port) throws IOException {
        serverSocket = new ServerSocket(port);
        return serverSocket;
    }

    public void stop() throws IOException {
        in.close();
        out.close();
        clientSocket.close();
        serverSocket.close();
    }

    // This method accepts and serves the incoming requests
    public void acceptConnection(ServerSocket serverSocket) throws IOException {
        clientSocket = serverSocket.accept();
        out = new PrintWriter(clientSocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));

        String move = in.readLine();

        System.out.println(move);

        out.println("6,1");
    }

    public static void main(String[] args) throws IOException {
        GameServer server = new GameServer();
        ServerSocket serverSocket = server.start(3333);
        System.out.println("Server Started");

        // The effective change you need to make
        // Loop through the incoming requests
        while(true) {
            server.acceptConnection(serverSocket);
        }
    }
}


public class GameClient {
    private Socket clientSocket;
    private PrintWriter out;
    private BufferedReader in;

    public void openConnection(String ip, int port) throws IOException {
        clientSocket = new Socket(ip, port);
        out = new PrintWriter(clientSocket.getOutputStream(), true);
        in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
    }

    public String sendMessage(String msg) throws IOException {
        String resp = "";
        out.println(msg);
        resp = in.readLine();

        return resp;
    }

    public void stop() throws IOException {
        in.close();
        out.close();
        clientSocket.close();
    }

    public static void main(String[] args) throws IOException {
        GameClient client = new GameClient();

        client.openConnection("10.7.232.200", 3333);
        String response = client.sendMessage("1,2");
        System.out.println(response);
        client.stop();
    }
}