一、发送端
//初始化winsocket库
WORD ver;
WSADATA WSAData;
ver=MAKEWORD(1,1);
if(WSAStartup(ver,&WSAData))
{
MessageBox("Startup error");
return;
}
if ( LOBYTE( WSAData.wVersion ) != 1 ||
HIBYTE( WSAData.wVersion ) != 1 )
{
MessageBox("Ver error");
WSACleanup();
return;
}
//与本地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)
{
MessageBox("CreateFile Fail");
return ;
}
dwFileSize = GetFileSize (hFile, &dwHighSize);//获得文件的大小
if (dwHighSize)
{
CloseHandle (hFile) ;
MessageBox("GetFileSize Fail");
return ;
}
//发送文件大小
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))
{
free (readfile) ;
MessageBox("ReadFile Fail");
return;
}
//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
for(j=0;j
eachBuf[j]=readfile[i*eachSend+j];
}
retval=sendto(sock,(char*)eachBuf,eachSend,0, (SOCKADDR*)&addrR,len);
if(retval!=eachSend)
{
MessageBox("sendto error");
return;
}
memset(eachBuf,0,50*1024);
}
//再发送剩下的字符
for(i=0;i
eachBuf[i]=readfile[n*eachSend+i];
}
retval=sendto(sock,(char*)eachBuf,yu,0, (SOCKADDR*)&addrR,len);
if(retval!=yu)
{
MessageBox("sendto error");
return;
}
//中断winsocket库
closesocket(sock);
WSACleanup();
二、接收端
//初始化winsocket库
WORD ver;
WSADATA WSAData;
ver=MAKEWORD(1,1);
if(WSAStartup(ver,&WSAData))
{
MessageBox("Startup error");
return;
}
if ( LOBYTE( WSAData.wVersion ) != 1 ||
HIBYTE( WSAData.wVersion ) != 1 )
{
MessageBox("Ver error");
WSACleanup();
return;
}
//与本地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)))
{
MessageBox("bind error");
WSACleanup();
return;
}
//获取要接收文件的大小
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
retval=recvfrom(sock,eachBuf,eachRecv,0,(SOCKADDR*)&addrS,&len);
if(retval!=eachRecv)
{
MessageBox("recvfrom error");
return;
}
for(j=0;j
recvbuf[i*eachRecv+j]=(BYTE)eachBuf[j];
}
memset(eachBuf,0,50*1024);
}
retval=recvfrom(sock,eachBuf,yu,0, (SOCKADDR*)&addrR,&len);
if(retval!=yu)
{
MessageBox("recvfrom yu error");
return;
}
for(i=0;i
recvbuf[n*eachRecv+i]=(BYTE)eachBuf[i];
}
//写入文件
FILE *fp;
fp=fopen("f:\\222.bmp","wb");
fwrite(recvbuf,dwFileSize,1,fp);
fclose(fp);
//中断winsocket库
closesocket(sock);
WSACleanup();