服务器套接字始终向客户端Java返回null

时间:2012-02-09 20:21:09

标签: java sockets networking tcp

我,做一些计算机网络功课,我必须开发某种分布式DBMS,它们通过对等网络相互连接,所以我有一个TCP客户端和一个TCP服务器在一个.java文件中运行下一个通过线程相互对话。该类的TCP服务器总是从其他人那里听取其他TCP客户端并给他们服务,问题是当我System.out我必须在服务器端发送回客户端的字符串时,它的方式是它应该是,但发送后,客户端什么也得不到,并打印出null。我根据我在网上找到的教程编写了我的代码,当我测试它们时,它们运行良好,但它不能在我自己的代码中工作。你能看出我的问题在哪里吗?谢谢

class CommandComp
{
    int PORT = 1210;
    int PORT2 = 1211;
    String IPLocal = "";
    String IPdest = "";
    InetAddress IPAD;
    InetAddress IPAD2;
    int numOfNodes;
    int numOfNodesnonchanged;
    String[] Nodes;
    Random rand = new Random();
    int max = 2000;
    int min = 1000;
    String command;

    Socket clientSocket;

    CommandComp(String[] IPdest, String IPLocal, int numOfNodes, String command)
    {
        try
        {
            this.numOfNodes = numOfNodes;
            numOfNodesnonchanged = numOfNodes;
            this.IPLocal = IPLocal;
            this.Nodes = IPdest;
            this.command = command;
            // this.IPAD = InetAddress.getByName(this.IPdest);
            this.IPAD2 = InetAddress.getByName(this.IPLocal);
            // clientSocket = new Socket(this.IPAD , PORT ,this.IPAD2 , PORT2 );
        }
        catch (Exception e)
        {
            // //e.printStackTrace();
        }
    }

    public String call()
    {

        int i = 0;
        while (numOfNodes > 0)
        {
            String response = "";
            try
            {
                Thread.sleep(rand.nextInt(max - min + 1) + min);
                i = numOfNodes - 1;
                int max2 = 50;
                int min2 = 10;
                this.IPAD = InetAddress.getByName(Nodes[i]);
                clientSocket = new Socket(this.IPAD, PORT, this.IPAD2, PORT2 + rand.nextInt(max2 - min2 + 1) + min2);
                PrintWriter outToServer = new PrintWriter(clientSocket.getOutputStream(), true);
                BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                outToServer.println(command);
                System.out.println(inFromServer.readLine());
                response = inFromServer.readLine();
                outToServer.close();
                inFromServer.close();
                clientSocket.close();
                numOfNodes--;
                System.out.println(Nodes[i] + " Remote DBMS");
                System.out.println(response);

            }
            catch (Exception e)
            {
                e.printStackTrace();
                try
                {
                    clientSocket.close();
                }
                catch (Exception e2)
                {
                    // TODO: handle exception
                }
            }
        }
        return command;
    }
}

class TCPListnerService
    implements Callable<Object>
{    
    String from;

    String to;
    ServerSocket Server;
    String IP = "";
    int numOfNodes;
    int numofNodesUnchanged;
    static clientThread t[];

    TCPListnerService(String IP, int numOfNodes)
    {
        try
        {
            this.IP = IP;
            this.numOfNodes = numOfNodes;
            numofNodesUnchanged = numOfNodes * 2;
            this.t = new clientThread[numofNodesUnchanged];
            InetAddress IPAD = InetAddress.getByName(IP);
            Server = new ServerSocket(1210, 20, IPAD);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    public String call()
        throws Exception
    {

        String gotten = "";
        while (numOfNodes > 0)
        {
            Socket connected = Server.accept();

            for (int i = 0; i < numofNodesUnchanged; i++)
            {
                if (t[i] == null)
                {
                    (t[i] = new clientThread(connected)).start();
                    break;
                }
            }
        }
        return gotten;
    }

}

class clientThread
    extends Thread
{

    Socket clientSocket = null;
    sqlite DB = new sqlite();
    String response = "";
    String fromclient;
    String delims = "[ =)(',\n\t\r]+";
    String[] tokens;

    public clientThread(Socket clientSocket)
    {
        this.clientSocket = clientSocket;
    }

    public void run()
    {

        try
        {
            BufferedReader inFromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            PrintWriter outToClient = new PrintWriter(clientSocket.getOutputStream(), true);
            fromclient = inFromClient.readLine();

            tokens = fromclient.split(delims);
            if (tokens[0].equalsIgnoreCase("create")
                || tokens[0].equalsIgnoreCase("drop")
                || tokens[0].equalsIgnoreCase("delete")
                || tokens[0].equalsIgnoreCase("insert")
                || tokens[0].equalsIgnoreCase("update")
                || tokens[0].equalsIgnoreCase("select"))
            {
                response = DB.RunQuery(fromclient);
                System.out.println(response);
                outToClient.print(response);
                clientS.close();

            }
            else if (tokens[0].equalsIgnoreCase("shut"))
            {
                System.exit(0);
            }

            inFromClient.close();
            outToClient.close();
            clientSocket.close();
        }
        catch (Exception e)
        {
        }
        ;
    }
}

1 个答案:

答案 0 :(得分:2)

问题在于:

inFromClient.close();
outToClient.close();
clientSocket.close();

您正在关闭(1)输入流,它关闭套接字,(2)PrintWriter,刷新它并关闭套接字,以及(3)套接字。如果套接字已经关闭,显然(2)不能成功。您发送给客户端的数据仍然是缓冲的,永远不会被刷新,因此它永远不会被发送。

关闭PrintWriter,并关闭finally块中的套接字以防万一(2)以某种方式失败。根本不需要关闭输入。