通过UDP文本传输基本文件

时间:2013-04-01 20:13:08

标签: c udp file-transfer

我正在尝试通过UDP发送文件,这可能会使用64> 1024个字节,我遇到的问题是无法管理发送服务器看到的所有部分: 码 服务器:

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

输出示例:

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! 

客户端:

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

输出:

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

正如您所看到的,只发送了文本的第一部分。有什么想法吗? PS:EOF被识别,但不知何故,我的客户只得到文本的一部分...

编辑:我很确定这个问题围绕着这一行:}while(fread(tempString,1,256, fd) > 0)在客户端......

1 个答案:

答案 0 :(得分:1)

在客户端中打开文件时:

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

该文件为空。然后你在里面写点什么:

fwrite(tempString, 1, 256, fd);

文件点指向文件结尾。此时,如果您阅读它,您将立即到达EOF。

fread(tempString,1,256, fd)

所以do {} while循环将会只运行一轮。

如果要在连接关闭时中断循环,则应检查recvfrom()(http://linux.die.net/man/2/recvfrom)的返回值:

  

返回值:这些调用返回接收的字节数,如果发生错误则返回-1。当对等体执行有序关闭时,返回值将为0。