2

我有这个 tcp/ip 程序,如果用户在其中输入“再见”。它会停止连接,并结束程序。但是,当我键入 Bye 时。从客户端,它不会执行 if 语句,而是执行其他语句。

程序的一部分

while ((inputLine = in.readLine()) != null) 
    { 
     System.out.println ("Server: " + inputLine); 
     out.println(inputLine); 

     String csvline = getCsvLineVal (getLocation34CSV(getTag34Value(Tag34Location(getTagCSV( parseFixMsg(inputLine ,inputLine))), getValueCSV( parseFixMsg(inputLine ,inputLine))), getVal34(input1,  input2)), getCSVLine( input3,  input4) );
     compareClientFixCSV( getTagCSV( parseFixMsg(inputLine ,inputLine)), getValueCSV(parseFixMsg(inputLine ,inputLine)), getCSVTag(csvline), getCSVValue(csvline));

     if (inputLine.equals("Bye.")) {
         System.out.println("Exit program"); 
         break;
         } 

     out.close(); 
     in.close(); 
     clientSocket.close(); 
     serverSocket.close();
    }

程序的较大部分

public static void server(Scanner input1, Scanner input2, Scanner input3, Scanner input4) throws IOException{
        ServerSocket serverSocket = null; 
    try { 
         serverSocket = new ServerSocket(57634); 
        } 
    catch (IOException e) 
        { 
         System.err.println("Could not listen on port: 57635."); 
         System.exit(1); 
        } 

    Socket clientSocket = null; 
    System.out.println ("Waiting for connection.....");

    try { 
         clientSocket = serverSocket.accept(); 
        } 
    catch (IOException e) 
        { 
         System.err.println("Accept failed."); 
         System.exit(1); 
        } 

    System.out.println ("Connection successful");
    System.out.println ("Waiting for input.....");

    PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), 
                                      true); 
    BufferedReader in = new BufferedReader( 
            new InputStreamReader( clientSocket.getInputStream())); 

    String inputLine; 

    while ((inputLine = in.readLine()) != null) 
        { 
         System.out.println ("Server: " + inputLine); 
         out.println(inputLine); 

         String csvline = getCsvLineVal (getLocation34CSV(getTag34Value(Tag34Location(getTagCSV( parseFixMsg(inputLine ,inputLine))), getValueCSV( parseFixMsg(inputLine ,inputLine))), getVal34(input1,  input2)), getCSVLine( input3,  input4) );
         compareClientFixCSV( getTagCSV( parseFixMsg(inputLine ,inputLine)), getValueCSV(parseFixMsg(inputLine ,inputLine)), getCSVTag(csvline), getCSVValue(csvline));

         if (inputLine.equals("Bye.")) {
             System.out.println("Exit program"); 
             break;
             } 

         out.close(); 
         in.close(); 
         clientSocket.close(); 
         serverSocket.close();
        }

}
4

3 回答 3

3

从您的代码中,如果用户键入“再见”。它会中断while循环,但永远不会关闭输入/输出流,也不会关闭客户端/服务器套接字。

out.close(); 
in.close(); 
clientSocket.close(); 
serverSocket.close();

上面的代码在您的语句之后发生在您的while循环中。if但是,如果您的if陈述是正确的,则循环中断并且不会到达这些陈述。

于 2013-07-22T20:45:27.210 回答
1

while 循环中的中断正在退出循环。它应该看起来像这样,否则您的关闭将不会被清理。

 while ((inputLine = in.readLine()) != null) 
        { 
         System.out.println ("Server: " + inputLine); 
         out.println(inputLine); 

         String csvline = getCsvLineVal (getLocation34CSV(getTag34Value(Tag34Location(getTagCSV( parseFixMsg(inputLine ,inputLine))), getValueCSV( parseFixMsg(inputLine ,inputLine))), getVal34(input1,  input2)), getCSVLine( input3,  input4) );
         compareClientFixCSV( getTagCSV( parseFixMsg(inputLine ,inputLine)), getValueCSV(parseFixMsg(inputLine ,inputLine)), getCSVTag(csvline), getCSVValue(csvline));

         if (inputLine.equals("Bye.")) {
             System.out.println("Exit program"); 
             break;
             } 
        }

         out.close(); 
         in.close(); 
         clientSocket.close(); 
         serverSocket.close();
于 2013-07-22T20:49:26.303 回答
0

如果您想同时拥有多个客户端,您可能希望在一个新线程中处理每个客户端。

于 2013-07-22T20:47:50.880 回答