简体   繁体   English

在C中通过TCP发送和接收整数值

[英]Send and receive an integer value over TCP in C

In a program, I need to send an integer value over the TCP socket. 在程序中,我需要通过TCP套接字发送一个整数值。 I used the send() and recv() functions for the purpose but they are sending and receiving it only as string. 我为此目的使用了send()recv()函数,但它们只是以字符串形式发送和接收它。

Is there any alternative for the send() and recv() so as to send and receive integer values? send()recv()有什么替代方法可以发送和接收整数值吗?

send() and recv() don't send strings. send()recv()不发送字符串。 They send bytes . 他们发送字节 It is up to you to provide an interpretation of the bytes that makes sense. 您需要提供有意义的字节解释。

If you wish to send integers, you need to decide what size integers you are going to send -- 1 byte, 2 bytes, 4 bytes, 8 bytes, etc. You also need to decide on an encoding format . 如果您希望发送整数,则需要确定要发送的整数大小 - 1字节,2字节,4字节,8字节等。您还需要确定编码格式 "Network order" describes the convention of Big-Endian formatting. “网络顺序”描述了Big-Endian格式的约定。 You can convert to and from network order using the following functions from <arpa/inet.h> or <netinet/in.h> : 您可以使用<arpa/inet.h><netinet/in.h>的以下函数转换为网络顺序和从网络顺序转换:

   uint32_t htonl(uint32_t hostlong);
   uint16_t htons(uint16_t hostshort);
   uint32_t ntohl(uint32_t netlong);
   uint16_t ntohs(uint16_t netshort);

If you stick to using htonl() before sending (host-to-network, long) and ntohl() after receiving (network-to-host, long), you'll do alright. 如果您在接收(网络到主机,长)之后发送(主机到网络,长)和ntohl() htonl()之前坚持使用htonl() ),那么你就可以了。

You can send the integer as four bytes, of course (or however many bytes your integer happens to be); 当然,您可以将整数作为四个字节发送(或者整数恰好是多少字节); but you have to be careful. 但你必须要小心。 See the function htonl() , presented on your system probably in <arpa/inet.h> . 请参阅系统中提供的函数htonl() ,可能在<arpa/inet.h>

Assuming a four-byte integer n, this might do what you want: 假设一个四字节整数n,这可能会做你想要的:

uint32_t un = htonl(n);
send(sockfd, &un, sizeof(uint32_t), flags);

Then you can code the complement on the receiving end. 然后你可以在接收端编码补码。

The reason for the call to htonl() is that the highest-order byte is conventionally transmitted first over the Internet, whereas some computer architectures like the ubiquitous x86 store the lowest-order byte first. 调用htonl()的原因是,最高位字节通常首先通过Internet传输,而某些计算机体系结构(如无处不在的x86)首先存储最低位字节。 If you don't fix the byte order, and the machine on one end is an x86, and the machine on the other end is something else, then without the htonl() the integer will come through garbled. 如果你不修复字节顺序,并且一端的机器是x86,而另一端的机器是其他的,那么没有htonl() ,整数将通过乱码。

I am not an expert on the topic, incidentally, but in the absence of experter answers, this answer might serve. 我不是在主题的专家,顺便提一句,但在没有experter答案,这个答案或许可以。 Good luck. 祝好运。

I tried it, too. 我也试过了。 But write didn't work. 但写不起作用。 So I found another solution. 所以我找到了另一个解决方

// send
char integer[4];                  // buffer
*((int*)integer) = 73232;         // 73232 is data which I want to send.
send( cs, integer, 4, 0 );        // send it

// receive
char integer[4];                  // buffer
recv( s, integer, 4, 0 );         // receive it

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

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