我正在尝试通过 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)
在客户端...