【问题标题】:Keep communication open between server and client - Java保持服务器和客户端之间的通信畅通 - Java
【发布时间】:2015-02-16 00:46:39
【问题描述】:

如何让客户端能够随时向服务器发送多条消息,从而使服务器始终监听消息。

现在我写了一些代码,只允许我发送一次消息。我认为这是因为我关闭了输入/输出流和套接字。所以我已经玩了一段时间了,我似乎做不到!

客户:

public class Client {
    private Socket socket;
    private OutputStream os;

    public Client() {}

    public void connectToServer(String host, int port) {
        try {
            socket = new Socket(host, port);
        } catch (IOException e) {
            e.printStackTrace();
        }
        sendMessage();
    }

    public void sendMessage() {
        try {
            os = socket.getOutputStream();

            String string = "Anthony";
            byte[] b = string.getBytes(Charset.forName("UTF-8"));

            os.write(b);
            os.flush();

            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void STOP() {
        stopOutput();
        stopServer();
    }

    public void stopServer() {
        try {
            socket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void stopOutput() {
        try {
            os.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

服务器:

public class ConnectionHandler implements Runnable {
    private Socket clientSocket;
    private BufferedReader in;

    public ConnectionHandler(Socket clientSocket) {
        this.clientSocket = clientSocket;
        String clientAddress = clientSocket.getInetAddress().toString()
            .substring(1);
        System.out.println("Connected to " + clientAddress);

        try {
            in = new BufferedReader(new InputStreamReader(this.clientSocket.getInputStream()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        while (true) {
            try {
                ArrayList<String> data = new ArrayList<String>();
                String inputLine;
                while ((inputLine = in.readLine()) != null) {
                    data.add(inputLine);
                }

                if (data.size() > 0) {
                    System.out.println(data.toString());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void STOP() {
        stopInput();
        stopConnection();
    }

    public void stopInput() {
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void stopConnection() {
        try {
            clientSocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

目前在客户端,我会在打开套接字后立即发送一条消息,但是当我从另一个类调用 send 函数后它不会发送...

我应该怎么做?还是我做错了什么?

提前致谢。

附言我猜客户端-服务器与服务器-客户端是一样的,所以如果我知道如何做一种方法,我可以轻松地切换它......对吗?

【问题讨论】:

  • 了解名为多线程的概念

标签: java sockets client server serversocket


【解决方案1】:

原来这是一个简单的错误。

我将(发送客户端)作为 OutputStream 进行写入,但是我将(接收服务器)作为 BufferedReader 进行读取!哈哈

给任何人的快速提示,请确保您以与发送消息相同的方式接收消息!

感谢所有尝试提供帮助的人。

【讨论】:

    【解决方案2】:

    您的服务器一直在接受数据,因此您只需将客户端的 OutputStream 保存在某个地方并不时向其写入数据。但是不要关闭它,因为那样你也会关闭客户端套接字。

    完成此操作后,您需要更改其他内容,因为现在您对in.readLine() 的调用阻塞了您的服务器,因为它等待客户端发送某些内容。为防止这种情况发生,您可以尝试在要关闭客户端时向服务器添加“close”之类的字符串,如下所示:

    public void STOP() {
        os.write("close".getBytes(Charset.forName("UTF-8")));
        stopOutput();
        stopServer();
    }
    

    并将服务器中的代码更改为

    try {
        ArrayList<String> data = new ArrayList<String>();
        String inputLine;
        while (!(inputLine = in.readLine()).equals("close")) {
            data.add(inputLine);
        }
    
        if (data.size() > 0) {
            System.out.println(data.toString());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    

    【讨论】:

    • 是的,我试过这个,在我移动的客户端:os = socket.getOutputStream();在'sendMessage()'中,到'connectToServer()'下面:socket = new Socket(host, port);然后显然拿走了 os.close();在“sendMessage()”中,但是一旦我这样做了,服务器甚至没有收到我在创建套接字后发送的第一条消息。当你说每隔一段时间时,你的意思是每隔 2 小时?为什么这不起作用? :S 感谢您的快速回复:)
    • 我进行了更改,我理解您的意思,但除非我立即关闭 OutputStream,否则它不起作用。我做错了什么!?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-10
    • 2012-11-21
    • 1970-01-01
    相关资源
    最近更新 更多