通过C / C ++通过套接字发送int [英] Send int over socket in C/C++
本文介绍了通过C / C ++通过套接字发送int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在通过套接字发送整数数组时遇到麻烦。
代码如下:
I have troubles with sending an array of ints over a socket. the code looks like this
程序1 :(在Windows上运行)
Program 1: (running on windows)
int bmp_info_buff[3];
/* connecting and others */
/* Send informations about bitmap */
send(my_socket, (char*)bmp_info_buff, 3, 0);
程序2 :(在中微子上运行)
Program 2: (running on neutrino)
/*buff to store bitmap information size, with, length */
int bmp_info_buff[3];
/* stuff */
/* Read informations about bitmap */
recv(my_connection, bmp_info_buff, 3, NULL);
printf("Size of bitmap: %d\nwidth: %d\nheight: %d\n", bmp_info_buff[0], bmp_info_buff[1], bmp_info_buff[2]);
应打印
位图大小:64
宽度:8
高度:8
It should print
Size of bitmap: 64
width: 8
height: 8
位图大小:64
宽度:6
高度:4096
我该怎么办?
Size of bitmap: 64
width: 6
height: 4096
What do I do wrong?
推荐答案
当您发送 bmp_info_buff
数组作为char数组, bmp_info_buff
的大小不是3,而是 3 * sizeof(int)
When you send the bmp_info_buff
array as char array, the size of bmp_info_buff
is not 3 but is 3 * sizeof(int)
recv
替换
send(my_socket, (char*)bmp_info_buff, 3, 0);
recv(my_connection, bmp_info_buff, 3, NULL);
by
send(my_socket, (char*)bmp_info_buff, 3*sizeof(int), 0);
recv(my_connection, bmp_info_buff, 3*sizeof(int), NULL);
这篇关于通过C / C ++通过套接字发送int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文