简体   繁体   English

套接字(C / Java)BufferedReader readLine()不会停止

[英]Socket (C/Java) BufferedReader readLine() doesn't stop

I'm creating a forum with a Java interface and a C server. 我正在创建一个带有Java接口和C服务器的论坛。

I have trouble sending a message from C to Java.... 我无法从C向Java发送消息。

I created a socket (named "socket") that works, like this : 我创建了一个工作的套接字(名为“ socket”),如下所示:

socket = new Socket(adr, port);
//adr and port are defined before

But when doing this on the Java: 但是在Java上执行此操作时:

String str =null;
try {
    BufferedReader br = new BufferedReader( new InputStreamReader(socket.getInputStream()) );
    while ((str=br.readLine()) != null && str.length()>0)
    {
        System.out.println("str = " + str);
    }
        br.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

It just can't receive the message from the server. 它只是无法接收来自服务器的消息。 It only shows the message when I brutally close the C server. 仅在我残酷地关闭C服务器时才显示该消息。

Here is how the server sends the message through the Socket : 这是服务器通过Socket发送消息的方式:

write(client_sock,"0\0",strlen("0\0"));

I have no idea how to receive this "0" without closing the server. 我不知道如何在不关闭服务器的情况下接收此“ 0”。 Any ideas ? 有任何想法吗 ?

You're looping until readLine() returns null or a blank line. 您一直循环播放,直到readLine()返回null或空白行。

It returns null at end of stream. 它在流的末尾返回null As you're reading from a socket, that only happens when the peer closes the connection. 当您从套接字读取数据时,仅在对等方关闭连接时才会发生。

It returns a blank line when the input contains a blank line. 当输入包含空行时,它将返回空行。

Ergo the peer is neither closing the connection nor sending a blank line. 埃尔戈对端既不关闭连接也不发送一个空行。

readLine() has read the send data, but blocks until a line feed or carriage return was read readLine()已读取发送数据,但在读取换行符或回车符之前一直阻塞
If your client sees the data only if you are closing the server, than your server never sends a line feed or a carriage return. 如果您的客户仅在关闭服务器时才看到数据,则服务器将永远不会发送换行或回车。
Assuming, you want to send a file line by line to the client(example in java, it is my main language): 假设您要逐行向客户端发送文件(以Java为例,这是我的主要语言):

Socket client = server.accept();
OutputStream os = client.getOutputStream();

BufferedReader br = new BufferedReader( new InputStreamReader(Files.newInputStream(file.toPath())));
String str;
while ((str = br.readLine()) != null && str.length() > 0) {
     os.write((str).getBytes());
}
br.close();
os.close();

As you can see, the server don't sends a line feed or carriage return. 如您所见,服务器不会发送换行符或回车符。 These were removed by readLine()! 这些已由readLine()删除!
So, the readLine() on your clientside is blocking until the socket will closed. 因此,客户端上的readLine()将阻塞,直到套接字将关闭。

If you change the write instruction in that way: 如果您以这种方式更改写指令:

os.write((str+"\n").getBytes());

your client is able to read the lines, even if the server is still writing the following lines. 即使服务器仍在编写以下行,您的客户端也能够读取这些行。

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

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