为什么我的程序会跳过if语句

时间:2013-07-22 20:40:20

标签: java logic tcpserver

我有这个tcp / ip程序,如果用户输入“Bye”。它会停止连接,并结束程序。但是,当我输入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();
        }

}

3 个答案:

答案 0 :(得分:3)

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

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

上述代码发生在while声明之后的if循环中。但是如果您的if语句为真,则循环中断并且未达到这些语句。

答案 1 :(得分: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();

答案 2 :(得分:0)

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