1

我的程序运行良好,许多用户可以连接服务器并向服务器发送命令。但是,当用户使用命令向服务器发送垃圾邮件时,服务器会阻止所有其他客户端,并且服务器不会收到来自发送垃圾邮件的客户端以外的客户端的消息。为什么是这样?

TCPAccept 连接


    package game.server;

import java.io.IOException;
import java.net.Socket;

public class TCPAcceptConnections implements Runnable
{
    public static Socket clientSocket = null;;
    int clientID = -1;

    public void run()
    {
        while(Main.TCP)
        {
            try
            {
                clientSocket = TCPServer.serverSocket.accept();
                System.out.println("Client Connected.");
                clientID++;

                new TCPClientManager(clientSocket, clientID).run();
            } 
            catch (IOException e)
            {
                System.out.println("Couldn't create client socket.");
                System.exit(-1);
            }
        }
    }
}

TCPClientManager:


    package game.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class TCPClientManager implements Runnable
{
    Socket client;

    int clientID;

    static PrintWriter out;
    static BufferedReader in;
    String inputLine, outputLine;

    boolean destroy = false;

    public TCPClientManager(Socket cs, int id)
    {
        try
        {
            client = cs;
            clientID = id;
            out = new PrintWriter(client.getOutputStream(), true);
            in = new BufferedReader(new InputStreamReader(client.getInputStream()));
        } catch(IOException e)
        {
            e.printStackTrace();
        }
    }

    public void run()
    {
        System.out.println("Created TCPManager for client.");
        String command;

        while(!destroy)
        {
            try
            {
                if((command = in.readLine()) != null) //If received something
                {
                    System.out.println("Commad received: " + command);
                        System.out.println(" " + Commands.proccessCommand(command));
                    System.out.println("Command proccessed");
                }
                else
                {
                    client.close();
                    destroy = true;
                }
            } catch (IOException e)
            {
                try
                {
                    client.close();
                } catch (IOException e1)
                {
                    e1.printStackTrace();
                    destroy = true;
                }
                System.out.println("Client lost connection.");
                destroy = true;
            }
        }
        System.out.println("TCPManager for client destroyed.");
    }
}

命令:


package game.server;

public class Commands
{
    public static String proccessCommand(String command)
    {
        if(command.equalsIgnoreCase("cp"))
        {
            System.out.println("Creating player...");
                System.out.println("    Retrieved client");
            return "Player Created";
        }
        else
        {
            return "Unkown command: " + command;
        }
    }
}
4

1 回答 1

1

如果你得到一个未知的命令,你应该记录它并关闭连接。

但是你有一个更严重的问题。当它读取 null 时,您不会停止客户端处理程序。因此,一旦客户端断开连接,读取将永远徒劳无功。如果 readLine() 返回 null 您必须关闭套接字并退出循环。如果您收到任何 IOException,您还必须关闭套接字。

于 2012-10-28T01:06:02.293 回答