【问题标题】:UDP in C: Losing first byte when sending dataC中的UDP:发送数据时丢失第一个字节
【发布时间】:2014-07-31 01:42:00
【问题描述】:

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

“^@N 国会,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);
}}

【问题讨论】:

    标签: c sockets udp


    【解决方案1】:

    你的 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 函数保证以空值结尾的目标字符串。

    【讨论】:

      猜你喜欢
      • 2014-11-26
      • 2021-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-11
      • 2021-01-26
      • 2018-10-11
      相关资源
      最近更新 更多