AnthonyJK
AnthonyJK

Reputation: 61

UDP in C: Losing first byte when sending data

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);
}}

Upvotes: 1

Views: 1346

Answers (1)

Marcelo Cantos
Marcelo Cantos

Reputation: 185862

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.

Upvotes: 6

Related Questions