2012-02-09 8 views
1

저는 컴퓨터 네트워크 숙제를하고 있고 피어 투 피어 네트워크를 통해 서로 연결되어있는 일종의 분산 DBMS를 개발해야하므로 TCP 클라이언트와 스레드에 의해 서로 옆에서 실행되는 하나의 .java 파일에있는 TCP 서버. 클래스의 TCP 서버는 항상 다른 사람들로부터 다른 TCP 클라이언트를 듣고 그들에게 서비스를 제공합니다. 문제는 내가 System.out 문자열인데 서버 측에서 클라이언트로 다시 보내야합니다. 하지만 전송 후 클라이언트는 아무것도 얻지 않고 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를 닫고, (2)의 경우에 대비하여 finally 블록의 소켓을 닫습니다. 입력을 전혀 닫을 필요가 없습니다.

관련 문제