为什么我的声明不会发送到客户端?

时间:2013-07-24 14:57:18

标签: java oop tcp

我只想向客户端发送一个简单的字符串,但它没有这样做。 它将跳过out.println语句,只是正确完成程序的其余部分。 out.println是从服务器端发送到客户端的错误语句吗?

我只想使用此代码发送“hello”。

out.println("hello");

服务器端程序

public class TcpServerCompareCSV {

    public static void main(String[] args) throws IOException{
        Scanner console = new Scanner(System.in);
        System.out.println("Type in CSV file location: ");
        //String csvName = console.nextLine();
          String csvName = "C:\\Users\\Downloads\\orders.csv";


        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;
        Boolean comp;
        while ((inputLine = in.readLine()) != null) 
            { 

            ***out.println("hello");***

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

             Scanner input1 = new Scanner(new File(csvName));
             Scanner input2 = new Scanner(new File(csvName));
             Scanner input3 = new Scanner(new File(csvName));
             Scanner input4 = new Scanner(new File(csvName));

             System.out.println ("Server: " + inputLine); 

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

             if(comp)
                 out.println(noError);
             else
                 out.println(Error);


             input1.close();
             input2.close();
             input3.close();
             input4.close();



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

    }

客户端计划

public class TcpClient {

            public static void main(String[] args) throws IOException {
            String serverHostname = new String ("WA12345"); //127.0.0.1

                if (args.length > 0)
                   serverHostname = args[0];
                System.out.println ("Attemping to connect to host " +
                serverHostname + " on port 57634.");

                Socket echoSocket = null;
                PrintWriter out = null;
                BufferedReader in = null;

                try {
                    // echoSocket = new Socket("taranis", 7);
                    echoSocket = new Socket(serverHostname, 57634);
                    out = new PrintWriter(echoSocket.getOutputStream(), true);
                    in = new BufferedReader(new InputStreamReader(
                                                echoSocket.getInputStream()));
                } catch (UnknownHostException e) {
                    System.err.println("Don't know about host: " + serverHostname);
                    System.exit(1);
                } catch (IOException e) {
                    System.err.println("Couldn't get I/O for "
                                       + "the connection to: " + serverHostname);
                    System.exit(1);
                }

            BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
            String userInput;

                System.out.print ("input: ");

            while ((userInput = stdIn.readLine()) != null) {
                out.println(userInput);


                 if (userInput.equals("Bye.")){
                     System.out.println("Exit program");
                       break;        
                 }
              getValueLog(parseFixMsg(userInput,userInput));
                  System.out.print ("input: ");

            }

            out.close();
            in.close();
            stdIn.close();
            echoSocket.close();
            }

1 个答案:

答案 0 :(得分:1)

您永远不会从客户端程序中的套接字输入流中读取(由语句in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()));创建),因此您实际上从未尝试过接收它。

之后,您需要在循环中的客户端程序中使用in.readLine()
while ((userInput = stdIn.readLine()) != null) {
                out.println(userInput);

它应该是这样的:

System.out.println(in.readLine());