什么时候java客户端可以从c服务器接收-1?

时间:2020-12-20 09:50:07

My client is writen in java. The server is written in C. When the client read from the server, the read function return -1. I kown in java if the outputstream closed before the socket closed, java client will receive -1. but when in C, when the java client will receive -1?

我的客户端是用java编写的。服务器是用C语言编写的。当客户端从服务器读取时,读取函数返回-1。我知道如果在socket关闭之前输出流关闭,java客户端会收到-1。但是在C中,当java客户端会收到-1?

JAVA CASE:
OutputStream out = socket.getOutputStream();
out.write(buf);
out.close();
out.write(otherbuf);

In this case the when the client call read() it will return -1.

在这种情况下,当客户端调用read()时,它将返回-1。

If server is writen in c, when the java client will return -1?? Thanks.

如果服务器在c中写入,当java客户端将返回-1 ??谢谢。

1 个解决方案

#1


4  

What language the server is written in is irrelevant. It could be written in anything, the client does not know and should not care (in general).

服务器编写的语言是无关紧要的。它可以写成任何东西,客户不知道也不应该关心(一般而言)。

A read will return -1 when it reaches the end of stream, as documented. The end of stream happens when the other side closed that side of the (TCP) socket.

如文档所述,读取将在到达流末尾时返回-1。当另一侧关闭(TCP)套接字的那一侧时,流的结束发生。

(On the C side, assuming a POSIX system, the socket can be closed either with the close(2) system call, or with the shutdown(2) system call. The later allows the program to only close one "side" of the connection, i.e. close the write side but keep the read side open. close shuts both sides down.)

(在C端,假设POSIX系统,可以通过close(2)系统调用或shutdown(2)系统调用关闭套接字。后者允许程序只关闭一个“侧”连接,即关闭写入侧但保持读取侧打开。关闭两侧。)

#1


4  

What language the server is written in is irrelevant. It could be written in anything, the client does not know and should not care (in general).

服务器编写的语言是无关紧要的。它可以写成任何东西,客户不知道也不应该关心(一般而言)。

A read will return -1 when it reaches the end of stream, as documented. The end of stream happens when the other side closed that side of the (TCP) socket.

如文档所述,读取将在到达流末尾时返回-1。当另一侧关闭(TCP)套接字的那一侧时,流的结束发生。

(On the C side, assuming a POSIX system, the socket can be closed either with the close(2) system call, or with the shutdown(2) system call. The later allows the program to only close one "side" of the connection, i.e. close the write side but keep the read side open. close shuts both sides down.)

(在C端,假设POSIX系统,可以通过close(2)系统调用或shutdown(2)系统调用关闭套接字。后者允许程序只关闭一个“侧”连接,即关闭写入侧但保持读取侧打开。关闭两侧。)