1
votes

I'm trying to send a file via UDP, which may eb send using 64 > 1024 bytes, the problem I have, is can't manage to send all the parts the server sees: code server:

void* sender(void* p){
    pthread_mutex_lock(&mtx);

    struct sockaddr_in addr;
    char tString[256];
    int state,
    sock = *(int *)p;
    socklen_t slen = sizeof(tempAddr);
    addr = tempAddr;
    memset(&tempAddr, 0x00, sizeof(tempAddr));

    pthread_mutex_unlock(&mtx);
    FILE* fd = fopen("foo.txt", "r");
    int offset = 0;

    while( fread(tString,1,256, fd) > 0){
        if( (ferror(fd)))
            ERR("!! ?? ferror"); 

        fwrite(tString, 1, 256, stdout);
        if (sendto(sock, tString, strlen(tString), 0, (struct sockaddr *) &addr, slen) < 0){
            ERR("!! ?? sendto");
        }

        if (feof(fd)){
           printf("~ EOF! ~\n");
           break;
        }
        memset(tString,0,256);
    }

    fclose(fd);
    pthread_exit(NULL);
}

output example:

Received pack from 127.0.0.1:55523
Data: asd

server received datagram from localhost (127.0.0.1)
server received 3/256 bytes: asd
Old unsatiable our now but considered travelling impression. In excuse hardly summer in basket misery. By rent an part need. At wrong of of water those linen. Needed oppose seemed how all. Very mrs shed shew gave you. Oh shutters do removing reserved wandered an. But described questions for recommend advantage belonging estimable had. Pianoforte reasonable as so am inhabiting. Chatty design remark and his abroad figure but its.Affronting discretion as do is announcing. Now months esteem oppose nearer enable too six. She numerous unlocked you perceive speedily. Affixed offence spirits or ye of offices between. Real on shot it were four an as. Absolute bachelor rendered six nay you juvenile. Vanity entire an chatty to. ~ EOF! 

client:

 FILE* fd = fopen("fooo.txt","w");
    int offset = 0;
    do{
        if (recvfrom(sockfd, tempString, 256, 0, (struct sockaddr*)&sAddr, &slen) < 0){
            ERR("!! ?? recvfrom()");
        }
        fwrite(tempString, 1, 256, fd);
        printf("Received packet from %s:%d\nData: %s\n\n", inet_ntoa(sAddr.sin_addr), ntohs(sAddr.sin_port), tempString);
    }while(fread(tempString,1,256, fd) > 0);

output:

Enter data to send(Type exit and press enter to exit) : asd
Received packet from 127.0.0.1:1234
Data: Old unsatiable our now but considered travelling impression. In excuse hardly summer in basket misery. By rent an part need. At wrong of of water those linen. Needed oppose seemed how all. Very mrs shed shew gave you. Oh shutters do removing reserved wande

As you can observe only the first part of the text is sent. Any ideas why? PS: the EOF is recognized but somehow my client gets only 1 part of the text...

EDIT: I'm pretty sure the issues is around this line: }while(fread(tempString,1,256, fd) > 0) in client...

1

1 Answers

1
votes

When you open the file in client:

FILE* fd = fopen("fooo.txt","w");

The file is empty. Then you write something in it:

fwrite(tempString, 1, 256, fd);

The file point is lead to end of file. At this point, if you read it, you will immediately reach the EOF.

fread(tempString,1,256, fd)

So the do{}while loop will and only will work one round.

If you want to break the loop when the connection is closed, you should check the return value of recvfrom() (http://linux.die.net/man/2/recvfrom):

Return Value: These calls return the number of bytes received, or -1 if an error occurred. The return value will be 0 when the peer has performed an orderly shutdown.