近日小弟做了個linux下使用者資料報協議大檔案傳輸程式發上來與大家共勉。
**在 redhat 9.0 下編譯通過。
最大測試無差錯傳輸檔案:288m
最大測試傳輸速度:6.5m/s
可能這裡並不需要這種型別的帖子,但希望各位能代小弟轉貼,因為網路上很難搜尋到這種可以解決丟包問題的**級實現方法,希望每乙個需要的人都能看的到。
伺服器端:
#include;
#include;
#include;
#include;
#include;
#include;
#include;
#include;
#include;
#include;
#include;
#include;
#include;
#define serv_port 2500
#define max_size 1024*40
void recvudp(char name[20],int sockfd)
else if(ret==0)
if(fd_isset(sockfd,&rdset))
if(!strcmp(mesg,"end"))
wlen = write(fd,mesg,rlen);
if(wlen != rlen )
}printf("the %d times write\n",i);
}close(fd);
}int main(int argc, char *argv)
r = fcntl(sockfd, f_getfl, 0);
fcntl(sockfd, f_setfl, r & ~o_nonblock);
recvudp(argv[1],sockfd);
return 0;
客戶端:
#include;
#include;
#include;
#include;
#include;
#include;
#include;
#include;
#include;
#include;
#include;
#include;
#include;
#include;
#define max_size 1024*40
#define serv_port 2500
void connectudp(char name[20],int sockfd,struct sockaddr *pservaddr,socklen_t servlen)
else
printf("connect server ok!\n");
fd = open(name,o_rdonly);
if(fd==-1)
i=0;
while(1)
else if(ret==0)
memset(buf,0,max_size);
if(fd_isset(sockfd,&wrset))
else if(rlen==0)
printf("all complete\n");
close(fd);
close(sockfd);
exit(0);
}wlen = write(sockfd,buf,rlen);
if(wlen != rlen)
i++;
usleep(500);
printf("the %d times read\n",i);}}
}int main(int argc ,char *argv)
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family= af_inet;
servaddr.sin_port = htons(serv_port);
if(inet_pton(af_inet,argv[1],&servaddr.sin_addr) <= 0)
fh = argv[2];
sockfd =socket(af_inet,sock_dgram,0);
r = fcntl(sockfd, f_getfl, 0);
fcntl(sockfd, f_setfl, r & ~o_nonblock);
connectudp(argv[2],sockfd,(struct sockaddr *)&servaddr,sizeof(servaddr));
fprintf(stderr,"serverip:\t%s\n",argv[1]);
if(stat(argv[2],&fsize) == -1)
perror("failed to get fiel statusi\n");
else
fprintf(stderr,"file name:\t%s\nfile size:\t%dk\n",argv[2],fsize.st_size/1024);
error2=sysinfo(&s_info);
time2 = s_info.uptime;
printf("tranfice file time =%fs\n",(time2-time1));
}
makefile
all:send recv
send:send.c
gcc -wall send.c -o send
recv:recv.c
gcc -wall recv.c -o recv
clean:
rm -rf send recv
如何做linux下udp大檔案傳輸
伺服器端 客戶端 include include include include include include include include include include include include include include define max size 1024 40 defin...
window下實現UDP通訊 大檔案傳輸
include include include include pragma comment lib,ws2 32.lib define bufsize 512 typedef struct packinfo typedef struct backinfo intserversocketinit 執...
Linux下用UDP實現檔案傳輸
原文 udp程式設計框圖 udp協議中伺服器和客戶端的互動存在於資料的收發過程中。進行網路資料收發的時候,伺服器和客戶端的資料是對應的 客戶端傳送資料的動作,對伺服器來說是接收資料的動作 客戶端接收資料的動作,對伺服器來說是傳送資料的動作。實現檔案的傳輸大概分為這幾個步驟 客戶端讀檔案,將內容放在c...