服务器未接收数据/客户端未发送数据

时间:2014-07-09 05:38:35

标签: java networking socket.io client-server

我正在制作聊天程序。在连接阶段出现问题。从理论上讲,连接就是这样发生的:

  1. [服务器] accept()传入连接
  2. [客户端]尝试连接服务器
  3. [服务器&客户端]初始化IO
  4. [客户]发送带有后缀的用户名,表示
  5. 后是否有密码
  6. [服务器]接收用户名,检查是否有传入密码(如果有,也可以阅读)
  7. [服务器]检查数据库是否已注册客户
  8. [服务器] ...(对客户的帐户进行多次检查等)
  9. [服务器]如果被接受,则向客户发送答案
  10. [客户]收到答案并解释
  11. 请注意,尚未实施步骤6和7,服务器会跳过直接创建新客户帐户并在网络中接受该帐户。

    现在,在步骤4中发生了某些事情(如果错误来自客户端没有发送它的名称)或者在步骤5(如果错误来自服务器没有收到名称),则在任何一种情况下,服务器卡在等待客户端的名称,客户端卡住等待服务器的回答(步骤9)。我正在请求您帮助找出此错误的原因以及如何解决此问题。

    以下是服务器用于接受新连接的函数的代码:

    public void establishConnection() {
        Socket client = null;
        BufferedReader input = null;
        PrintWriter output = null;
        String name = null;
        String password = null;
    
        try {
            client = server.accept();
            System.out.println("A connection is being established");
            input = new BufferedReader(new InputStreamReader(client.getInputStream()));
            output = new PrintWriter(client.getOutputStream());
    
            //LOCATION OF THE BUG
            //DESCRIPTION: If the name has been sent, the server seems not to receive it.
            System.out.println("Waiting for the client's name...");
            name = input.readLine();
            if (name.charAt(name.length() - 1) == Keywords.PASSWORD) {
                System.out.println("Waiting for the client's password...");
                password = input.readLine();
            } else
                System.out.println("The client has no password");
            name = name.substring(0, name.length() - 1);
    
            System.out.println("Creation of the metaClient");
            MetaClient metaClient = new MetaClient(name);
            if (password != null)
                metaClient.setPassword(password);
            metaClient.setSocket(client);
            System.out.println("Activating listening thread");
            metaClient.activateCommunications(this, this);
    
            /* If there is no already registered clients,
             * the first to connect shall be the owner.
             */
            if (clientsList.size() == 0)
                metaClient.addRight(MetaClient.OWNER);
    
            clientsList.add(metaClient);
    
            output.write(Keywords.CONNECTION_ACCEPTED);
            System.out.println("The connection process of user " + metaClient.getName()
                    + " is complete.");
        } catch (IOException e) {
            System.out.println("Error when establishing a new connection");
            e.printStackTrace();
        } finally {
            try {
                if (input != null)
                    input.close();
                if (output != null)
                    output.close();
            } catch (IOException e) {
                System.out.println("Error when closing the IO of the new connection.");
                e.printStackTrace();
            }
        }
    }
    

    以下是尝试连接服务器的客户端构建器的代码:

    public ChatClient(String ip, int port, String name, String password) throws ConnectException {
        this.name = name;
        boolean connectionRefused = false;
        try {
            System.out.println("Establishing connection to the server...");
            server = new Socket(ip, port);
            BufferedReader input = new BufferedReader(new InputStreamReader(server.getInputStream()));
            PrintWriter output = new PrintWriter(server.getOutputStream());
    
            //LOCATION OF THE BUG:
            //BUG DESCRIPTION: The name seems not to be sent to the server
            System.out.println("Sending name to the server...");
            if (password == null || password == "") {
                name += Keywords.NO_PASSWORD;
                output.println(name);
            } else {
                name += Keywords.PASSWORD;
                output.println(name);
                System.out.println("Sending password to the server...");
                output.println(password);
            }
    
            System.out.println("Waiting for the server's response...");
            //Wait for the server's response
            String response = input.readLine();
            if (response.equals(Keywords.CONNECTION_ACCEPTED))
                System.out.println("The connection has been accepted");
            else
                connectionRefused = true;
        } catch (IOException e) {
            System.out.println("Error when connecting to the server");
            e.printStackTrace();
            System.exit(1);
        }
    
        if (connectionRefused)
            throw new ConnectException("The connection was refused by the server");
        else {
            communication = new CommunicationProtocol(server, this, this);
            communication.start();
        }
    }
    

    任何帮助都会非常感激,现在我花了很长时间才试图解决这个问题无济于事。

    编辑以回答Scary Wombat的评论: 是的,一台机器运行服务器而另一台机器运行客户机。但即使两台计算机在一台计算机上作为两个独立的程序运行,也会发生错误。

0 个答案:

没有答案