2012-02-09 6 views
1

私はいくつかのコンピュータネットワークの宿題をしていますが、ピアツーピアネットワークで互いに接続された分散型DBMSを開発しなければならないので、TCPクライアントと1つの.javaファイル内のTCPサーバー。スレッドごとに相互に実行されます。クラスのTCPサーバは、他のクライアントから他のTCPクライアントを常に聴いてサービスを提供します。問題は、私がサーバ側のクライアントに返す必要がある文字列です。送信後、クライアントは何も取得せず、nullを出力します。私はネット上で見つけたチュートリアルに基づいて自分のコードを書いた。テストしたところ、うまく働いたが自分のコードでは動かない。私の問題がどこにあるのか分かりますか?おかげサーバソケットは常にクライアントにnullを返します。Java

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) 
     { 
     } 
     ; 
    } 
} 

答えて

2

問題はここにある:あなたがソケットを閉じ(1)入力ストリームを、閉じている

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

、(2)それをフラッシュし、ソケットを閉じ、PrintWriterを、 (3)ソケット。明らかに(2)ソケットが既に閉じている場合、成功することはできません。クライアントに送信したデータはまだバッファリングされています。決してフラッシュされないので、送信されることはありません。

PrintWriterを閉じ、何とか失敗した場合はfinallyブロックでソケットを閉じるだけです。入力をまったく閉じる必要はありません。

関連する問題