Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 结构中char数组上的Cout在通过套接字发送后在末尾显示垃圾_C++_Sockets_Struct_Cout_Garbage - Fatal编程技术网

C++ 结构中char数组上的Cout在通过套接字发送后在末尾显示垃圾

C++ 结构中char数组上的Cout在通过套接字发送后在末尾显示垃圾,c++,sockets,struct,cout,garbage,C++,Sockets,Struct,Cout,Garbage,我有一个结构,它有一个文本字符数组。此结构将通过UDP发送。在发送端,显示文本没有问题。在大多数情况下,文本的长度等于字符数组的大小。在发送端使用cout时,显示的文本没有错误。但是,在接收端,每次都会出现一个垃圾值 在发送端: bzero(c, DATA_SIZE+1); file.read(c, DATA_SIZE); strcpy(window[i].data, c); cout << window[i].data; // this shows the text correct

我有一个结构,它有一个文本字符数组。此结构将通过UDP发送。在发送端,显示文本没有问题。在大多数情况下,文本的长度等于字符数组的大小。在发送端使用cout时,显示的文本没有错误。但是,在接收端,每次都会出现一个垃圾值

在发送端:

bzero(c, DATA_SIZE+1);
file.read(c, DATA_SIZE);
strcpy(window[i].data, c);
cout << window[i].data; // this shows the text correctly

您的数据大小与sizeof(rudp_数据包)相比有多大?数据大小=1464,sizeof(rudp_数据包)=1472是否可以添加结构声明?接收端有数据大小字符,但发送端有数据大小+1个字符。
bzero(&recd_packet, sizeof(rudp_packet));
recvfrom(udp_socket, &recd_packet, sizeof(rudp_packet),0,NULL, NULL);

int rec_no = recd_packet.seq_no;
cout << "\n\n\n\nSequence number: " << rec_no << "\n";
cout << recd_packet.data; // this shows garbage at the end
struct rudp_packet {
    bool ack;
    bool fin;
    int seq_no;
    char data[DATA_SIZE];
};