如何从Java Client Server程序中的Console获取输入

时间:2015-10-05 04:38:57

标签: java sockets client-server

我写了一个简单的客户端服务器程序。我能够从客户端向服务器发送文本,反之亦然。但是在客户端上打印服务器数据后,我无法在客户端的CLI上打印任何内容。

我不确定我做错了什么。我怀疑行中的错误评论" Bug Here"。我无法找到这些行中的错误。

请在下面找到我的代码。

ATMClient.java

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

public class ATMClient
{
    public static void main(String args[])
    {
        try
        {
            Socket sock = new Socket("localhost", 9010);
            sock.setSoTimeout(10000);
            System.out.println("Connection established");
            String data = null;

            InputStreamReader input = new InputStreamReader(sock.getInputStream());
            BufferedReader bread = new BufferedReader(input);

            BufferedReader brCli = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Enter text to be sent to Server: ");
            String strCli = brCli.readLine();

            PrintWriter pwrite = new PrintWriter(sock.getOutputStream(), true);
            pwrite.println(strCli);

        // Bug Here: The control in not coming out of the while loop
            while((data = bread.readLine()) != null)
            {
                System.out.println(data);
            }

        // Bug Here: The following line does not get printed.
            System.out.print("Enter an Option: ");

            pwrite.close();
            bread.close();
            input.close();
        }
        catch(IOException ex)
        {
            System.err.println(ex);
        }
    }
}

ATMServer.java

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

public class ATMServer
{
    public static void main(String args[])
    {
        try
        {
            ServerSocket server = new ServerSocket(9010);
            Socket client = server.accept();
            System.out.println("Connection Established");

            InputStream input = client.getInputStream();
            BufferedReader bufread = new BufferedReader(new InputStreamReader(input));

            PrintWriter pwrite = new PrintWriter(client.getOutputStream(), true);

            pwrite.println("1. Deposit");
            pwrite.println("2. Withdrawal");
            pwrite.println("3. Balance");
            pwrite.println("4. Exit");

            String data = null;
            while((data = bufread.readLine()) != null)
            {
                System.out.println(data);
            }

            pwrite.close();
            bufread.close();
            input.close();
            server.close();
            client.close();
        }
        catch(Exception ex)
        {
            System.err.println(ex);
        }
    }
}

输出:

user1$ java ATMServer
Connection Established
hello

user1$ java ATMClient
Connection established
Enter text to be sent to Server: hello
1. Deposit
2. Withdrawal
3. Balance
4. Exit
Deposit
^C user1$

你能帮我弄清楚如何在客户端的控制台上输入信息吗?感谢。

1 个答案:

答案 0 :(得分:1)

只要远程套接字的输入流打开, bread.readLine()永远不会返回null, 因此这个循环永远不会结束:

while((data = bread.readLine()) != null)
{
    System.out.println(data);
}

您需要添加某种信号,例如文本" ENDMSG"。当服务器完成与客户端的通话时,服务器应该发送它,客户端应该识别它,并退出循环。例如:

while((data = bread.readLine()) != null)
{
    if (data.equals("ENDMSG")) {
        break;
    }
    System.out.println(data);
}