利用UDP協議傳送與接收檔案

2021-06-02 00:40:42 字數 3388 閱讀 2281

一、傳送端

//初始化winsocket庫

word ver;

wsadata wsadata;

ver=makeword(1,1);

if(wsastartup(ver,&wsadata))

if ( lobyte( wsadata.wversion ) != 1 ||

hibyte( wsadata.wversion ) != 1 )

//與本地ip繫結

socket sock=socket(af_inet,sock_dgram,0);

sockaddr_in addrs;

addrs.sin_addr.s_addr=htonl(inaddr_any);

addrs.sin_family=af_inet;

addrs.sin_port=htons(8899);

bind(sock,(sockaddr*)&addrs,sizeof(sockaddr));

//接收端的ip位址

sockaddr_in addrr;

addrr.sin_addr.s_addr=inet_addr("192.168.1.6");

addrr.sin_family=af_inet;

addrr.sin_port=htons(9988);

int len=sizeof(sockaddr);

//開啟要傳送檔案

bool 

bsuccess ;

handle  hfile ;

dword 

dwfilesize, dwhighsize , dwbytesread;

hfile = createfile ("f:\\111.bmp", generic_read, file_share_read, null,

open_existing, file_flag_sequential_scan, null) ;

if (hfile == invalid_handle_value)

dwfilesize = getfilesize (hfile, &dwhighsize);//獲得檔案的大小

if (dwhighsize)

//傳送檔案大小

char strfilesize[20];

itoa(dwfilesize,strfilesize,10 );

sendto(sock,strfilesize,20,0, (sockaddr*)&addrr,len);

//讀檔案內容到readfile中

byte *readfile;

readfile=(byte*)malloc(dwfilesize);

bsuccess = readfile (hfile, readfile, dwfilesize, &dwbytesread, null) ;

closehandle (hfile) ;

if (!bsuccess || (dwbytesread != dwfilesize)) 

//eachsend表示每次傳送字元的大小

dword eachsend=50*1024,retval,n,yu,i,j;

byte  eachbuf[50*1024];

n=dwfilesize / eachsend;  //共需要幾次全額傳送

yu=dwfilesize % eachsend; //最後剩下的字元大小

//先傳送n次全額資料

for(i=0;i

retval=sendto(sock,(char*)eachbuf,eachsend,0, (sockaddr*)&addrr,len);

if(retval!=eachsend)

memset(eachbuf,0,50*1024);

}//再傳送剩下的字元

for(i=0;i

retval=sendto(sock,(char*)eachbuf,yu,0, (sockaddr*)&addrr,len);

if(retval!=yu)

//中斷winsocket庫

closesocket(sock);

wsacleanup(); 

二、接收端

//初始化winsocket庫

word ver;

wsadata wsadata;

ver=makeword(1,1);

if(wsastartup(ver,&wsadata))

if ( lobyte( wsadata.wversion ) != 1 ||

hibyte( wsadata.wversion ) != 1 )

//與本地ip繫結

socket sock=socket(af_inet,sock_dgram,0);

sockaddr_in addrr;

addrr.sin_addr.s_addr=htonl(inaddr_any);

addrr.sin_family=af_inet;

addrr.sin_port=htons(9988);

if(socket_error==bind(sock,(sockaddr*)&addrr,sizeof(sockaddr)))

//獲取要接收檔案的大小

sockaddr_in addrs;

int len=sizeof(sockaddr);

char strfilesize[20];

recvfrom(sock,strfilesize,20,0,(sockaddr*)&addrs,&len);

dword 

dwfilesize;

dwfilesize=atoi(strfilesize); //檔案大小

dword eachrecv=50*1024,n,yu,i,j,retval;

n=dwfilesize / eachrecv;

yu=dwfilesize % eachrecv;

char  eachbuf[50*1024];

byte *recvbuf=(byte*)malloc(dwfilesize);//初始化要存放資料的字串

for(i=0;i

for(j=0;j

memset(eachbuf,0,50*1024);

} retval=recvfrom(sock,eachbuf,yu,0, (sockaddr*)&addrr,&len);

if(retval!=yu)

for(i=0;i

//寫入檔案

file *fp;

fp=fopen("f:\\222.bmp","wb");

fwrite(recvbuf,dwfilesize,1,fp);

fclose(fp); 

//中斷winsocket庫

closesocket(sock);

wsacleanup(); 

Python利用socket傳送與接收檔案001

應用場景 語言python,利用socket進行檔案傳輸 針對windows本地檔案d盤主目錄下的6個子目錄對應6中檔案,如 d 主目錄 001 2021 01 01 每個子目錄每天會產生乙個新的次子目錄並附帶一些內部檔案以當天日期命名如 d 主目錄 002 2021 01 01 xx,001則產生...

C UDP傳送接收檔案 BMP

傳送檔案 include include include include pragma comment lib,ws2 32.lib define dest port 3000 define max data 51200 50kb define dest ip address 127.0.0.1 1...

linux c 簡單UDP應用,資料傳送與接收

主要函式 接收recvfrom ssize t recvfrom int sockfd,void buf,size t len,int flags,struct sockaddr src addr,socklen t addrlen 引數說明 sockfd 套接字buf 用於接收資料的bufferl...