2011-04-26 183 views
0

我正在使用Java上的客戶端 - 服務器程序的示例。我遇到過這樣的問題: 我用8080端口和本地主機啓動服務器,比我啓動客戶端併發出請求。只要請求完成了兩個程序就關閉了插座,所以我不能重複我的操作。我如何使用相同的客戶端和同一臺服務器來提出多個請求?服務器 - 客戶端通信問題

public class Network extends Thread 
{ 
    MasterEdit ME = new MasterEdit(); 
     private Socket _socket; 
     InputStream is; //Data streams 
     OutputStream os; 
     /** 
     * Network class constructor 
     */ 
     public Network(int port, int backlog, InetAddress address) 
     { 
       //We create an object of SocketFactory 
       SocketFactory sf = new SocketFactory(); 
       //Save server socket 
       ServerSocket ss = null; 

       try 
       { 
         if(address == null) //If there is no host 
         { 
           if(backlog <= 0) //If backlog is not given we create it with port 
           { ss = sf.createServerSocket(port); 
            System.out.println("Success"); 
           } 
           else 
             ss = sf.createServerSocket(port, backlog); //If backlog is given we just create it 
         } 
         else 
           ss = sf.createServerSocket(port, backlog, address); //If everything is given we create it using data 
       } 
       catch(Exception e) 
       { 
         //Exception with creation of socket 
         System.err.println("Failed open server socket"); 
         System.exit(1); //Stop program and send 1 as a exception-code 
       } 

       while(true) //Listening to the socket 
       { 
         try 
         { 
           StartThread(ss.accept()); //If client has connected we send him to the daemon 
         } 
         catch (IOException e) 
         { 
           e.printStackTrace(); 
         } 
       } 

     } 

     /** 
     * Start daemon-tool when client has connected 
     */ 
     private void StartThread(Socket ss) 
     { 
       _socket = ss; //initializing of global variable 

       setDaemon(true); //anounce that new potok is daemon 
       setPriority(NORM_PRIORITY); //set the priority 
       start(); //Start it 
     } 

     @Override 
     public void run() 
     { 
       byte buffer[] = new byte[64*1024]; //buffer in 64 kb 
       try 
       { 
         is = _socket.getInputStream(); 
         os = _socket.getOutputStream(); //Initializing the output stream to a client 
         String toClient = SearchRequest(new String(buffer, 0, is.read(buffer))); 
         os.write(toClient.getBytes()); //Sending an answer 
       } 
       catch(Exception e) 
       { 
         e.printStackTrace(); 
       } 

     } 

     private String SearchRequest(String request) 
     { 
       String info = ""; //Initializing of a variable 

       if(request.equalsIgnoreCase("info")) //Check the request 
       { 
         //Adding data 
         info += "Virtual Machine Information (JVM)n"; 
         info += "JVM Name: " + System.getProperty("java.vm.name")+"n"; 
         info += "JVM installation directory: " + System.getProperty("java.home")+"n"; 
         info += "JVM version: " + System.getProperty("java.vm.version")+"n"; 
         info += "JVM Vendor: " + System.getProperty("java.vm.vendor")+"n"; 
         info += "JVM Info: " + System.getProperty("java.vm.info")+"n"; 
         return info; //Give the answer 
       } 
       if(request.charAt(0)=='0') { 
        StringTokenizer rm = new StringTokenizer(request, " \t\n\r,:"); 
        rm.nextToken(); 
        ME.MasterDell(Double.parseDouble(rm.nextToken()), Double.parseDouble(rm.nextToken()), Double.parseDouble(rm.nextToken()), Double.parseDouble(rm.nextToken())); 
        return "Successfully deleted"; 
       } 
       if(request.charAt(0)=='1'){ 
        StringTokenizer temp = new StringTokenizer(request, " \t\n\r,:"); 
        temp.nextToken(); 
        ME.MasterAdd(Double.parseDouble(temp.nextToken()), Double.parseDouble(temp.nextToken()), Double.parseDouble(temp.nextToken()), Double.parseDouble(temp.nextToken()), Double.parseDouble(temp.nextToken()), Double.parseDouble(temp.nextToken()), temp.nextToken()); 
        return "Successfully added"; 
       } 
       this.ClostIt(); 
       return "Bad request"; //bad request 
     } 
     public void ClostIt() { 
       try { 
        is.close(); 
         os.close(); 
         _socket.close(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

     } 
} 

它是服務器的一部分。它使用SocketFactory類,但主要是它只是在開始時創建一個套接字。在主程序中,我調用新的網絡(PORT,BACKLOG,InetAddress.getByName(host));

+2

不要關閉連接? – 2011-04-26 15:28:46

回答

1

我猜你的服務器程序,你沒有一個循環,而是這樣的事情:

public static void main(String args[]) { 
ServerSocket server = new ServerSocket(...); 
Socket con = server.accept(); 
//process the client connection ... 
//done, exit! 
} 

而不是

public static void main(String args[]) { 
ServerSocket server = new ServerSocket(...); 
Socket con = null; 
while(condition /* e.g. shutdown server message received */) { 
    con = server.accept(); 
    //process the client connection ... 
    //then keep waiting for the next request 
} 
//done, exit! 
} 

請記住上面的示例只處理一個客戶端一次!您將需要進入多線程處理同時處理的客戶端。