Java 的 readInt() 在 C 中等效吗?

发布于 2024-09-27 15:04:44 字数 350 浏览 0 评论 0原文

在一侧,我有一个 Java 客户端将 ints 写入其输出流:

int a = 20;
dataout.writeInt(a);
dataout.flush();

在另一侧,我有一个 C 服务器正在侦听连接:

int client = accept(...);

如何读取 Java 发送的 int?

如果我有一个 Java 服务器,我可以轻松地写:

int a = dataIn.readInt();

How to do this in C?

谢谢

In one side I have a Java client writing ints into its outputstream:

int a = 20;
dataout.writeInt(a);
dataout.flush();

From the other side I have a C server listening the connection:

int client = accept(...);

How to read the int sent by Java?

If I had a Java server, i could easily write:

int a = dataIn.readInt();

How to do this in C?

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

扛起拖把扫天下 2024-10-04 15:04:44

将其读入 char 缓冲区并使用 atoi() 将字符串转换为 int。

Read it into a char buffer and use atoi() to convert the string into a int.

琉璃繁缕 2024-10-04 15:04:44

您可以使用 read 函数,但您必须小心从套接字消耗 sizeof(int) 字符,然后您必须担心字节顺序。

You can use the read function, but you have to be careful to consume sizeof(int) chars from the socket and then you have to worry about endianess.

慕巷 2024-10-04 15:04:44

如果您不介意以纯文本发送,fscanf 应该可以工作:

fscanf(dataIn, "%d", &a);

纯文本规则:-)
只需确保在刷新之前从源发送“\n”(或非数字的内容)即可。

fscanf should work if you don't mind sending in plain text:

fscanf(dataIn, "%d", &a);

Plain text rules :-)
Just be sure to send a '\n' (or something not a digit) from the source before the flush.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文