2012-03-19 71 views
1

客戶請求響應消息不同步的意外行爲的

import java.io.*; 
import java.net.*; 
import java.util.Scanner; 

public class HTCPCPClient { 

    public static void main(String[] args) throws IOException { 

     HTCPCPClient client = new HTCPCPClient(); 
     System.out.println("WELCOME TO THE COFFEE POT APPLICATION!"); 
     client.startClient(); 
    } 

    private void startClient() throws IOException { 
     final String HOST = "localhost"; 
     final int PORT_NUMBER = 4444; 
     Socket clientSocket = null; 
     PrintWriter outToServer = null; 
     BufferedReader in = null; 
     String serverSentence = null; 
     String clientSentence = null; 
     BufferedReader inFromServer = null; 

     // create new socket 
     clientSocket = new Socket(HOST, PORT_NUMBER); 
     outToServer = new PrintWriter(clientSocket.getOutputStream(), true); 
     in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 

     do { // wait for 'QUIT'    
      // Create input stream 
      inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); 

      kbd = new Scanner(System.in); 
      clientSentence = null; 
      kbdInput = null; 

       System.out.println("Enter Method (e.g. BREW)"); 
       // next line of kbdInput from keybd. 

       kbdInput = kbd.nextLine().trim(); 

       clientSentence = kbdInput + " coffee://127.0.0.1/pot-1 HTCPCP-new Accept-Additions: "; 
       clientSentence = clientSentence + "\nstart\[email protected]@";  

      // Send clientSentence to server 
      outToServer.println(clientSentence); 
      outToServer.flush(); 

      System.out.println("\nMESSAGE FROM SERVER:"); 

      do { 
       serverSentence = inFromServer.readLine(); 
       System.out.println("\t" + serverSentence); 

       if (serverSentence.equals("@@") == true) { 
        break; 
       } 
      } while (true); 
      // read and print message from server 

     } while (!clientSentence.contains("QUIT")); 

     // close connections 
     outToServer.close(); 
     in.close(); 
     inFromServer.close(); 
     clientSocket.close(); 
    } 

} 

服務器線程

import java.io.*; 
import java.net.*; 

public class HTCPCPClientWorker extends Thread { 

    Socket cwsocket = null; 

    public HTCPCPClientWorker(Socket cwsocket) { 
     super("ClientWorker"); 
     this.cwsocket = cwsocket; 
    } 

    @Override 
    public void run() { 

     String clientSentence = null; 
     BufferedReader inFromClient = null; 
     PrintWriter outToClient = null; 

     try { 
      inFromClient = new BufferedReader(new InputStreamReader(cwsocket.getInputStream())); 
      outToClient = new PrintWriter(cwsocket.getOutputStream(), true); 
     } catch (IOException ex) { 
      System.err.println("Cannot create streams"); 
     } 

     try { 

      do { // end when client says QUIT 

       StringBuffer clientInputLine[] = new StringBuffer[3]; 

       clientInputLine[0] = new StringBuffer(); 
       clientInputLine[1] = new StringBuffer(); 

       // Get next message from client 
       for (int i = 0; i <= clientInputLine.length; i++) { 

        // read input line from BufferedReader 
        clientSentence = inFromClient.readLine(); 

        // wait for EOF = @@ 
        System.out.println("\tInput: " + clientSentence); 
        if (clientSentence.equals("@@") == true) { 
         break; 
        } 
        clientInputLine[i].append(clientSentence); 


        if (clientSentence.contains("BREW")) { 
         outToClient.println("Message: " + clientSentence); 
         outToClient.println("HTCPCP-new 200 OK BREW START command completed."); 
         outToClient.println("Content-length: " + clientSentence.length()); 
         outToClient.println("@@"); 
         outToClient.flush(); 
        } else { 
         outToClient.println("Message: " + clientSentence); 
         outToClient.println("HTCPCP-new 400 Bad Request."); 
         outToClient.println("Content-length: " + clientSentence.length()); 
         outToClient.println("@@"); 
         outToClient.flush(); 
        } 


       } // end for loop 


      } while (!clientSentence.contains("QUIT")); 

      outToClient.println("GOODBYE!"); 
      outToClient.flush(); 

      System.out.println("\tClient has disconnected."); 
      cwsocket.close(); 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } // end run 

} end HTCPCPClientWorker.java 

客戶端控制檯

WELCOME TO THE COFFEE POT APPLICATION! 

Select an option: 
1. Brew 
2. Quit 
1 
Enter URL (e.g. BREW coffee://127.0.0.1/pot-1 HTCPCP-new) 
BREW 

MESSAGE FROM SERVER: 
    Message: BREW Accept-Additions: 
    HTCPCP-new 200 OK BREW START command completed. 
    Content-length: 23 
    @@ 

Select an option: 
1. Brew 
2. Quit 
1 
Enter URL (e.g. BREW coffee://127.0.0.1/pot-1 HTCPCP-new) 
BREW 

MESSAGE FROM SERVER: 
    Message: start 
    HTCPCP-new 400 Bad Request. 
    Content-length: 5 
    @@ 

Select an option: 
1. Brew 
2. Quit 

注意,來自服務器的消息,儘管同樣是不同的正在輸入網址。

任何想法我錯了嗎?

回答

1

在你的服務器上,你有這個上你的循環的每次迭代:

if (clientSentence.contains("BREW")) { 
    outToClient.println("Message: " + clientSentence); 
    outToClient.println("HTCPCP-new 200 OK BREW START command completed."); 
    outToClient.println("Content-length: " + clientSentence.length()); 
    outToClient.println("@@"); 
    outToClient.flush(); 
} else { 
    outToClient.println("Message: " + clientSentence); 
    outToClient.println("HTCPCP-new 400 Bad Request."); 
    outToClient.println("Content-length: " + clientSentence.length()); 
    outToClient.println("@@"); 
    outToClient.flush(); 
} 

因此,服務器將讀取「BREW」(ETC),然後吐出所有的輸出,結尾@@。你的客戶端顯示所有這些,然後詢問下一個輸入...但服務器不會完成發送,因爲它將讀取下一個行輸入,這是「開始」。然後它會打印出第二個響應,即使它仍在讀取第一個請求。

我建議你讀完請求然後寫出來的響應...

請注意,您的輸入迴路也應該有一個獨家上限,太:

for (int i = 0; i <= clientInputLine.length; i++) { 
    ... 
    // This will blow up if i == clientInputLine.length 
    clientInputLine[i].append(clientSentence);