如何从Java客户端服务器程序中的控制台获取输入 [英] How to get input from Console in Java Client Server Program

查看:57
本文介绍了如何从Java客户端服务器程序中的控制台获取输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have written a simple client server program. I am able to send text from client to server and vice-versa. But after printing the Server's data on the client, I am not able to print anything on the Client's CLI.

我不确定自己在做什么错.我怀疑该行中的错误评论了这里有问题".我找不到这些行中的问题.

I am not sure what I am doing wrong. I suspect bug in the lines commented "Bug Here". I am unable to find what is wrong in those lines.

请在下面找到我的代码.

Please find my code below.

ATMClient.java

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

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$

您能帮我弄清楚如何在客户控制台上获得输入吗?谢谢.

Can you help me in figuring out how to get an input on Client's console ? Thanks.

推荐答案

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

As long as the input stream from the remote socket is open, bread.readLine() will never return null, and therefore this loop will never end:

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

您需要添加某种信号,例如文本"ENDMSG".服务器完成与客户端的对话后,应发送此消息,客户端应这样识别并退出循环.例如:

You need to add some kind of signal, for example the text "ENDMSG". The server should send this when it's done talking to the client, and the client should recognize it as such, and exit from the loop. For example:

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

这篇关于如何从Java客户端服务器程序中的控制台获取输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆