C中的UDP:发送数据时丢失第一个字节
我目前正在使用 c 中的 UDP 开发一个基本的发送和接收程序。我目前让它半正确地发送文件,唯一的问题是它丢失了发送的每个数据块的第一个字符。例如,如果我要发送独立宣言,则第一个块发送的开头将如下所示:
“^@N CONGRESS,1776 年 7 月 4 日。”与“1776 年 7 月 4 日国会”相反。
接收并附加到文件的每个块的开头都以“^@”开头,而不是正确的字母。我尝试打印出recvData[0],它打印出大量空白(至少 100 个换行符)。
另外,这个程序在我的本地主机(OS X)上运行 100% 正常,但是当上传到服务器(Ubuntu)时,它用 ^@ 替换第一个字符,所以我完全不知道问题是什么。
这是我的发送函数的源代码:
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);
}}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 tempData 数组正在溢出:
没有元素 1201,因此您可能正在破坏 sendData 数组的第一个字节。也许您的意思是
tempData[1200] = '\0'
。更安全的替代方法是使用这个:
snprintf
函数保证以空终止目标字符串。Your are overflowing your tempData array:
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:
The
snprintf
function guarantees to null-terminate the destination string.