简体   繁体   English

java-通过套接字发送连续数据

[英]java - continuous data sending through Socket

I'm writing a Java client/server application. 我正在编写Java客户端/服务器应用程序。 It should allow clients to send text data to the server. 它应该允许客户端将文本数据发送到服务器。 This kind of communication should be repeatable many times using the same connection. 使用相同的连接,这种通信应可重复多次。

I write it like this: 我这样写:

// On a server:
ServerSocket serverSocket = new ServerSocket(port);
Socket socket = serverSocket.accept();
socket.setKeepAlive(true);
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
if (reader.ready()) {
    for (String line = reader.readLine(); line != null; line = reader.readLine()) {
        // do something with line
    }
}

// On a client:
Socket socket = new Socket(host, port);
socket.setKeepAlive(true);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
writer.write("Some data from client to server");
writer.flush();

The problem is: I can't read on a server before I close OutputStream on a client. 问题是:关闭客户端上的OutputStream之前,我无法在服务器上阅读。 Or I can't open OutputStream on a client again, if it was already closed. 否则,如果已经关闭,则无法在客户端上再次打开OutputStream。 How can I do continuous sending and reading of data? 如何连续发送和读取数据?

两端需要两个线程,一个线程用于读取数据,另一线程用于写入数据。

The problem is: I can't read on a server before I close OutputStream on a client. 问题是:关闭客户端上的OutputStream之前,我无法在服务器上阅读。

Yes you can. 是的你可以。 You just can't get to the case where readLine() returns null. 您只是无法了解readLine()返回null的情况。 It isn't the same thing. 这不是同一回事。

Or I can't open OutputStream on a client again, if it was already closed. 否则,如果已经关闭,则无法在客户端上再次打开OutputStream。

Of course not. 当然不是。 You have to create a new Socket. 您必须创建一个新的套接字。

How can I do continuous sending and receiving of data? 如何连续发送和接收数据?

I don't understand the question. 我不明白这个问题。 The code you posted doesn't attempt that. 您发布的代码不会尝试这样做。

If your goal is to send many mesages over the same socket connection, these messages will have to be delimited by an application-level protocol. 如果您的目标是通过同一套接字连接发送许多消息,则这些消息将必须由应用程序级协议定界。 In other words, you won't be able to rely on any system calls like reader.ready() or reader.readLine() == null to detect the end of the message on te server. 换句话说,您将无法依靠任何系统调用(例如reader.ready()reader.readLine() == null来检测te服务器上消息的结尾。

One way to achieve this is to begin each message with its length in characters. 实现此目的的一种方法是,以每个字符的长度开始每个消息。 The server will then read exactly that number of charecters, and then stop and wait for a new message. 然后,服务器将准确读取该数量的字符,然后停止并等待新消息。 Another is to define a special character sequence which concludes each message. 另一个是定义一个特殊的字符序列,以结束每个消息。 The server will react to reading that particular sequence by ending the reading of the current message and returning to the "wait for new message" state. 服务器将通过结束当前消息的读取并返回到“等待新消息”状态来响应读取特定序列。 You must ensure that this sequence never appears in the message itself. 您必须确保此序列永远不会出现在消息本身中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM