Search code examples
csocketsudpdatagram

recvfrom() gets empty buffer even though the client sent nothing


client: argv[1]: ip address, argv[2]: port number, argv[3]: file name The client first sends argv[3] to the server and send the file in packets in UDP connection. Once it finds there's no data read it gets out of the loops and is terminated.

server: argv[1]: port number This is iterative so repeats receiving forever until the system gets ^C. First is receives the file name and gets into the loop to get data. Actually still I don't know what the hell I have done to the escape condition.. I think it will make some problem when the last packet size is the same as BUFFER_SIZE. Anyway it works without problem so far, so this part is not the biggest task to take care of.

The problem I faced is that a recvfrom() for receiving file name gets empty data. What I had expected is that the function waits for the file name to be sent from the client. Two source codes and the result shown on the terminal are added below. Can anyone help me stop recvfrom() from getting empty buffer?

SERVER:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <arpa/inet.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <fcntl.h>

#define BUFFER_SIZE 50

int main(int argc, char *argv[]){
    int sockfd;//socket file descriptor
    int byteRcvd;//gets received bytes when recvfrom() is called
    int addr_size = sizeof(struct sockaddr);//used to bind
    int fp;//to handle file
    int i;

    struct sockaddr_in addr;//contains address information

    char fileName[20];//gets file name
    char buf[BUFFER_SIZE];//buffer

    //create a socket
    sockfd = socket(PF_INET, SOCK_DGRAM, 0);

    //set and addr
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl(INADDR_ANY);//address here
    addr.sin_port = htons(atoi(argv[1]));//port # here
    bind(sockfd, (struct sockaddr*)&addr, sizeof(addr));

    //repetition of receiving files
    while(1){
        //get file name and open
        memset(buf, 0, sizeof(buf));
        byteRcvd = recvfrom(sockfd, buf, sizeof(buf), 0,
            (struct sockaddr*)&addr, &addr_size);
        fp = open(buf, O_RDWR | O_CREAT, 0644);
            printf("file \"%s\" is opened\n", buf);

        //get file
        memset(buf, 0, sizeof(buf));//reset buffer
        //byteRcvd = recvfrom(sockfd, buf, sizeof(buf), 0,
            //(struct sockaddr*)&addr, &addr_size);

        byteRcvd = BUFFER_SIZE;
        while(byteRcvd){
            if(byteRcvd != BUFFER_SIZE)
                break;

            //rcv data and show its size
            if((byteRcvd = recvfrom(sockfd, buf, sizeof(buf), 0,
                (struct sockaddr*)&addr, &addr_size)) == EOF){
                printf("Out of loop by EOF\n");
                break;
            }
            printf("%dB Rcvd ", byteRcvd);

            //printf rcvd data
            for(i = 0; i < BUFFER_SIZE; i++)
                if(buf[i] == '\n')
                    printf("#", buf[i]);
                else
                    printf("%c", buf[i]);

            printf("\n");
            //getchar();

            write(fp, buf, byteRcvd);

            memset(buf, 0, sizeof(buf));
        }
        printf("byteRcvd: %d\n", byteRcvd);

        close(sockfd);
        close(fp);
        fp = 0;

        printf("Continue??\n");
        getchar();
    }
    return 0;
}

CLIENT:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <fcntl.h>

#define BUFFER_SIZE 50

int sockfd;//socket file descriptor
struct sockaddr_in addr;//variable to contain IPv4 and port#

//argv[1]: IP address
//argv[2]: port #
//argv[3]: file name to be sent
int main(int argc, char *argv[]){
    int i, n;
    int byteSent = 0;//size of data sent at each time
    int addr_size = sizeof(struct sockaddr);

    int fp = open(argv[3], O_RDONLY);

    char buf[BUFFER_SIZE];//buffer to contain data
    char temp[BUFFER_SIZE];

    //check argument number
    if(argc != 4)
        exit(1);

    //create a socket
    if((sockfd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
        exit(1);

    //set addr
    memset(&addr, 0, sizeof(addr));
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr(argv[1]);
    addr.sin_port = htons(atoi(argv[2]));

    //send the file name to the server
    byteSent = sendto(sockfd, argv[3], sizeof(argv[3]), 0,
        (struct sockaddr*)&addr, addr_size);

    memset(buf, 0, sizeof(buf));

    //while((byteSent = read(fp, buf, BUFFER_SIZE))){
        byteSent = read(fp, buf, BUFFER_SIZE);
    while(byteSent){
        getchar();
        sendto(sockfd, buf, byteSent, 0,
            (struct sockaddr*)&addr, addr_size);
        printf("%d byte sent\n", byteSent);
        for(i = 0; i < BUFFER_SIZE; i++){
            if(i == 0)
                printf("[");
            if(buf[i] == '\n')
                printf("@");
            else
                printf("%c", buf[i]);
            if(i == 39)
                printf("]");}
        printf("\n");
        memset(buf, 0, sizeof(buf));
        byteSent = read(fp, buf, BUFFER_SIZE);
        printf("Next byteSent is to be %d\n", byteSent);
    }

    close(sockfd);
    close(fp);
    return 0;
}

l.txt:

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Terminal of SERVER:

[21100610@localhost server]$ ./s 70000
file "l.txt" is opened
50B Rcvd Lorem ipsum dolor sit amet, consectetur adipiscing
50B Rcvd  elit, sed do eiusmod tempor incididunt ut labore
50B Rcvd et dolore magna aliqua. Ut enim ad minim veniam, q
50B Rcvd uis nostrud exercitation ullamco laboris nisi ut a
50B Rcvd liquip ex ea commodo consequat. Duis aute irure do
50B Rcvd lor in reprehenderit in voluptate velit esse cillu
50B Rcvd m dolore eu fugiat nulla pariatur. Excepteur sint
50B Rcvd occaecat cupidatat non proident, sunt in culpa qui
46B Rcvd  officia deserunt mollit anim id est laborum.#
byteRcvd: 46
Continue??   <=== at this point I typed enter

file "" is opened
Out of loop by EOF
byteRcvd: -1
Continue??   <=== at this point I typed enter

file "" is opened
Out of loop by EOF
byteRcvd: -1
Continue??   <=== at this point I typed enter

file "" is opened
Out of loop by EOF
byteRcvd: -1
Continue??
^C
[21100610@localhost server]$

Terminal of CLIENT:

[21100610@localhost client]$ ./c 127.0.0.1 70000 l.txt

50 byte sent
[Lorem ipsum dolor sit amet, consectetur ]adipiscing
Next byteSent is to be 50

50 byte sent
[ elit, sed do eiusmod tempor incididunt ]ut labore
Next byteSent is to be 50

50 byte sent
[et dolore magna aliqua. Ut enim ad minim] veniam, q
Next byteSent is to be 50

50 byte sent
[uis nostrud exercitation ullamco laboris] nisi ut a
Next byteSent is to be 50

50 byte sent
[liquip ex ea commodo consequat. Duis aut]e irure do
Next byteSent is to be 50

50 byte sent
[lor in reprehenderit in voluptate velit ]esse cillu
Next byteSent is to be 50

50 byte sent
[m dolore eu fugiat nulla pariatur. Excep]teur sint
Next byteSent is to be 50

50 byte sent
[occaecat cupidatat non proident, sunt in] culpa qui
Next byteSent is to be 46

46 byte sent
[ officia deserunt mollit anim id est lab]orum.@
Next byteSent is to be 0
[21100610@localhost client]$

Solution

  • Is there a question here?

    You call recvfrom to receive file contents until you receive a packet of less than 50 bytes. When that happens, you close the socket and loop back attempting to read more data (another file?), but that recvfrom will always return -1 (error) with EBADF, because the socket has been closed.

    You are comparing the return value of recvfrom with EOF, which is nonsense -- EOF is for <stdio.h> functions, not socket functions. It just happens that EOF is -1, and socket functions return -1 for error, so by checking for EOF, you're actually checking for error...