Java客户端/服务器 - 通过Socket连接发送多个字符串

时间:2017-02-05 02:04:37

标签: java sockets tcp

我有一个程序Client,它向服务器发送字符串LISTALL。 之后,服务器需要使用ps -e命令检查系统(Linux)上的所有正在运行的进程,然后将结果返回给客户端。 显然结果是由多个字符串组成的,那么如何让客户端接收并存储它们呢?

现在,这是我从客户那里得到的:

Message sent to the server: LISTALL Message received from the server: null

从服务器:

Server started and listening to.. Message received from client: LISTALL

然后它按预期列出ps -e命令的结果。

我的代码:

服务器

public class Server
{

    private static Socket socket;

    public static void main(String[] args)
    {
        try
        {

            int port = 25000;
            ServerSocket serverSocket = new ServerSocket(port);
            System.out.println("Server Started and listening to the port 25000");

            //Server is running always. This is done using this while(true) loop
            while(true)
            {
                //Reading the message from the client
                socket = serverSocket.accept();
                InputStream is = socket.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                String list = br.readLine();
                System.out.println("Message received from client is "+list);


                //Sending the response back to the client.
                OutputStream os = socket.getOutputStream();
                OutputStreamWriter osw = new OutputStreamWriter(os);
                BufferedWriter bw = new BufferedWriter(osw);

                String returnMessage;
                try
                {
                    ProcessBuilder build = new ProcessBuilder("ps", "-e");
                    Process proc = build.start();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                while (reader.readLine() != null) {
                    returnMessage=reader.readLine();
                          bw.write(returnMessage);

                    proc.waitFor();

                    System.out.println("Message sent to the client is "+returnMessage);
                    bw.flush();}
                }
                catch(NumberFormatException e)
                {
                    //Input was not a number. Sending proper message back to client.
                    returnMessage = "Please send a proper number\n";
                }


            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                socket.close();
            }
            catch(Exception e){}
        }
    }
}

客户端:

public class Client
{

    private static Socket socket;

    public static void main(String args[])
    {
        try
        {
            String host = "localhost";
            int port = 25000;
            InetAddress address = InetAddress.getByName(host);
            socket = new Socket(address, port);

            //Send the message to the server
            OutputStream os = socket.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(os);
            BufferedWriter bw = new BufferedWriter(osw);

            String list = "LISTALL";

            String sendMessage = list + "\n";
            bw.write(sendMessage);
            bw.flush();
            System.out.println("Message sent to the server : "+sendMessage);

            //Get the return message from the server
            InputStream is = socket.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            while (br.readLine() != null) {         
            String message = br.readLine();
            System.out.println("Message received from the server : " +message);
        }}
        catch (Exception exception)
        {
            exception.printStackTrace();
        }
        finally
        {
            //Closing the socket
            try
            {
                socket.close();
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您的读取循环不正确。它们应该是一般形式:

String line;
while ((line = reader.readLine()) != null) {
    // Do something with `line`
}

目前你丢掉每一条奇数行,如果有奇数行,则打印一个空格。