Java 的 readInt() 在 C 中等效吗?
在一侧,我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将其读入 char 缓冲区并使用 atoi() 将字符串转换为 int。
Read it into a char buffer and use
atoi()
to convert the string into a int.您可以使用 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.
如果您不介意以纯文本发送,
fscanf
应该可以工作:纯文本规则:-)
只需确保在刷新之前从源发送“\n”(或非数字的内容)即可。
fscanf
should work if you don't mind sending in plain text:Plain text rules :-)
Just be sure to send a '\n' (or something not a digit) from the source before the flush.