2012-03-19 68 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);