Java 客户端/服务器 - 通过 Socket 连接发送多个字符串 [英] Java client/server - sending multiple strings over a Socket connection

查看:62
本文介绍了Java 客户端/服务器 - 通过 Socket 连接发送多个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a program Client that sends the server the string LISTALL. After that, the server needs to check for all running processes on the system (Linux) using the ps -e command, and then return the result to the client. Obviously the result is composed of multiple strings, so how can i get the client to receive and store them all?

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

Right now, this is what i get from the client:

发送到服务器的消息:LISTALL从服务器收到的消息:null

来自服务器:

服务器启动并监听..从客户端收到的消息:LISTALL

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

And then it lists the results from the ps -e command as expected.

我的代码:

服务器:

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();
            }
        }
    }
}

推荐答案

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

Your read loops are incorrect. They should be of the general form:

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

目前你正在扔掉每一个奇数行,如果有奇数行,则打印一个空值.

At present you are throwing away every odd line, and printing a null if there is an odd number of lines.

这篇关于Java 客户端/服务器 - 通过 Socket 连接发送多个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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