C中的UDP:发送数据时丢失第一个字节(UDP in C: Losing first byte when sending data)

编程入门 行业动态 更新时间:2024-10-23 13:22:37
C中的UDP:发送数据时丢失第一个字节(UDP in C: Losing first byte when sending data)

我目前正在使用c中的UDP工作一个基本的发送和接收程序。 我目前正在以半正确的方式发送文件,唯一的问题是它正在丢失发送的每个数据块的第一个字符。 例如,如果我要发送独立声明,则第一个块发送的开始将如下所示:

“^ @ N大会,1776年7月4日。” 而不是“1776年7月4日举行的大会”。

每个接收并附加到文件的块的开头都以“^ @”开头,而不是正确的字母。 我尝试打印出recvData [0],并打印出大量空白(至少100个换行符)。

另外,这个程序在我的本地主机(OS X)上运行得很好,但是当上传到服务器(Ubuntu)时,它用^ @替换了第一个字符,所以我完全丧失了什么问题。

这是我的send函数的源代码:

void sendFile() { // Announce who we're sending data to if(DEBUG) { printf("\nSending %s to %s:%d\n", filename, address, port); } // Open file FILE * file = fopen(filename, "rb"); if (file == NULL) { perror("Invalid File\n"); exit(1); } // Get size of the file fseek(file, 0, SEEK_END); int filesize = ftell(file); rewind(file); int curPos = 0; int dataSize = 0; while(curPos < filesize) { struct sockaddr_in server_addr; struct hostent *recvr; char sendData[BUFSIZE]; // stores message to be sent memset(sendData, 0, BUFSIZE); int byte, i; for(i = 0; i < BUFSIZE; i++){ if((filesize - curPos) > 0) { byte = fgetc(file); sendData[i] = byte; curPos++; dataSize++; } else { break; } } recvr = gethostbyname(address); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(port); server_addr.sin_addr = *((struct in_addr *) recvr->h_addr); bzero(&(server_addr.sin_zero), 8); if(DEBUG) { char tempData[1201]; strncpy(tempData, sendData, 1200); tempData[1201] ='\0'; printf("%s\n\n\n\n\n", tempData); } sendto(sock, sendData, dataSize, 0, (struct sockaddr *) &server_addr, sizeof (struct sockaddr)); dataSize = 0; } fclose(file);}

这是我的接收函数的源代码:

void receiveFile() { int addr_len, bytesRead; char recvData[BUFSIZE]; // Buffer to store received data struct sockaddr_in client_addr; addr_len = sizeof (struct sockaddr); printf("\nWaiting for data on port %d\n", port); //Keep reading data from the socket while (1) { FILE *fp; fp=fopen("dummyfile.txt", "ab"); memset(recvData, 0, BUFSIZE); bytesRead = recvfrom(sock, recvData, BUFSIZE, 0, (struct sockaddr *) &client_addr, &addr_len); if(DEBUG) { printf("%c\n", recvData[0]); } int x; for(x = 0; x < bytesRead; x++) { fputc(recvData[x], fp); } // Print out who we're receiving from and what we're recieving printf("Receiving data from %s : %d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); fclose(fp); }}

I am currently working on a basic send and receive program using UDP in c. I currently have it sending the file semi-correctly, the only issue is that it is losing the first character of every chunk of data sent. For example if I were to send the declaration of independence, the beginning of the first chunk send would look like this:

"^@N CONGRESS, July 4, 1776." as opposed to "IN CONGRESS, July 4, 1776."

The beginning of every chunk received and appended to the file starts with "^@" as opposed to the correct letter. I tried printing out recvData[0] and it prints out as a lot of whitespace (at least 100 newlines).

Also, this program works 100% fine, on my localhost (OS X) but when uploaded to a server (Ubuntu) it replaces the first character with a ^@, so I am at a complete loss as to what the problem is.

Here is my source code for the send function:

void sendFile() { // Announce who we're sending data to if(DEBUG) { printf("\nSending %s to %s:%d\n", filename, address, port); } // Open file FILE * file = fopen(filename, "rb"); if (file == NULL) { perror("Invalid File\n"); exit(1); } // Get size of the file fseek(file, 0, SEEK_END); int filesize = ftell(file); rewind(file); int curPos = 0; int dataSize = 0; while(curPos < filesize) { struct sockaddr_in server_addr; struct hostent *recvr; char sendData[BUFSIZE]; // stores message to be sent memset(sendData, 0, BUFSIZE); int byte, i; for(i = 0; i < BUFSIZE; i++){ if((filesize - curPos) > 0) { byte = fgetc(file); sendData[i] = byte; curPos++; dataSize++; } else { break; } } recvr = gethostbyname(address); server_addr.sin_family = AF_INET; server_addr.sin_port = htons(port); server_addr.sin_addr = *((struct in_addr *) recvr->h_addr); bzero(&(server_addr.sin_zero), 8); if(DEBUG) { char tempData[1201]; strncpy(tempData, sendData, 1200); tempData[1201] ='\0'; printf("%s\n\n\n\n\n", tempData); } sendto(sock, sendData, dataSize, 0, (struct sockaddr *) &server_addr, sizeof (struct sockaddr)); dataSize = 0; } fclose(file);}

Here is my source code for the receive function:

void receiveFile() { int addr_len, bytesRead; char recvData[BUFSIZE]; // Buffer to store received data struct sockaddr_in client_addr; addr_len = sizeof (struct sockaddr); printf("\nWaiting for data on port %d\n", port); //Keep reading data from the socket while (1) { FILE *fp; fp=fopen("dummyfile.txt", "ab"); memset(recvData, 0, BUFSIZE); bytesRead = recvfrom(sock, recvData, BUFSIZE, 0, (struct sockaddr *) &client_addr, &addr_len); if(DEBUG) { printf("%c\n", recvData[0]); } int x; for(x = 0; x < bytesRead; x++) { fputc(recvData[x], fp); } // Print out who we're receiving from and what we're recieving printf("Receiving data from %s : %d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port)); fclose(fp); }}

最满意答案

你正在溢出你的tempData数组:

tempData[1201] ='\0';

没有元素1201,所以你可能会破坏sendData数组的第一个字节。 也许你的意思是tempData[1200] = '\0' 。

一个更安全的选择是使用这个:

char tempData[1201]; snprintf(tempData, sizeof(tempData), "%s", sendData); printf("%s\n\n\n\n\n", tempData);

snprintf函数保证空终止目标字符串。

Your are overflowing your tempData array:

tempData[1201] ='\0';

There is no element 1201, so you are probably clobbering the first byte of your sendData array. Perhaps you meant tempData[1200] = '\0'.

A safer alternative is to use this:

char tempData[1201]; snprintf(tempData, sizeof(tempData), "%s", sendData); printf("%s\n\n\n\n\n", tempData);

The snprintf function guarantees to null-terminate the destination string.

更多推荐

本文发布于:2023-08-02 19:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1379660.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:第一个   字节   数据   UDP   data

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!