//服务器的代码 只能传输文本文件
#include <WinSock2.h>
#include <stdio.h>
#pragma comment (lib,"ws2_32.lib")
#define FILEFROMADDR "F://521.jpg"
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return;
}
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 ) {
WSACleanup( );
return;
}
SOCKET sockServer = socket(AF_INET,SOCK_STREAM,0);
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.s_addr = INADDR_ANY;
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(6000);
int iResult;
iResult=bind(sockServer,(SOCKADDR*)&addrSrv,sizeof(addrSrv));
if (SOCKET_ERROR==iResult)
{
printf("failed to bind/n");
WSACleanup();
return;
}
SOCKET sockAccept;
SOCKADDR_IN addrAccept;
int iAcceptlen=sizeof(addrAccept);
listen(sockServer,5);
fd_set socketSet;
fd_set readSet;
FD_ZERO(&socketSet);
FD_SET(sockServer,&socketSet);
printf("wait for start server.......");
while(true)
{
fd_set readSet=socketSet;
select(NULL,&readSet,NULL,NULL,NULL);
for (int i=0;i < socketSet.fd_count;i++)
{
if (FD_ISSET(socketSet.fd_array[i],&readSet))
{
if (socketSet.fd_array[i] == sockServer)
{
sockAccept=accept(sockServer,(SOCKADDR*)&addrAccept,&iAcceptlen);
FD_SET(sockAccept,&socketSet);
printf("%s have connect server/n",inet_ntoa(addrAccept.sin_addr));
}
else
{
FILE *sendFile;
char sendBuf[100];
char recvBuf[100];
memset(sendBuf,0,100);
sendFile = fopen(FILEFROMADDR,"rb");
if (NULL == sendFile)
{
printf("Can not open the file /n");
return;
}
if(!feof(sendFile))
{
int readLen = fread(sendBuf,1,100,sendFile);
int iSendSize=send(socketSet.fd_array[i],sendBuf,readLen,0);
printf("%d/n",sendBuf);
memset(sendBuf,0,100);
if (SOCKET_ERROR == iSendSize)
{
closesocket(socketSet.fd_array[i]);
FD_CLR(socketSet.fd_array[i],&socketSet);
i--;
}
if (0 == iSendSize)
{
//客户socket关闭
printf("%s:%d has closed!/r/n",inet_ntoa(addrAccept.sin_addr),
ntohs(addrAccept.sin_port));
closesocket(socketSet.fd_array[i]);
FD_CLR(socketSet.fd_array[i],&socketSet);
i--;
}
}
fclose(sendFile);
}
}
}
}
}
//客户端的代码
#include <WinSock2.h>
#include <stdio.h>
#include <io.h>
#include <string>
#pragma comment (lib,"ws2_32.lib")
#define FILESAVEADDR "E://521.jpg"
void main()
{
WORD wVersionRequested;
WSADATA wsaData;
int err;
wVersionRequested = MAKEWORD( 2, 2 );
err = WSAStartup( wVersionRequested, &wsaData );
if ( err != 0 ) {
return;
}
if ( LOBYTE( wsaData.wVersion ) != 2 ||
HIBYTE( wsaData.wVersion ) != 2 )
{
WSACleanup( );
return;
}
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.s_addr=inet_addr("127.0.0.1");
addrSrv.sin_family=AF_INET;
addrSrv.sin_port=htons(6000);
SOCKET clientSock=socket(AF_INET,SOCK_STREAM,0);
int iResult;
iResult=connect(clientSock,(SOCKADDR*)&addrSrv,sizeof(addrSrv));
if (SOCKET_ERROR == iResult)
{
printf("Failed to connect server error is %d/r/n",WSAGetLastError());
WSACleanup();
return;
}
if (0 != iResult)
{
int iErrorCode;
iErrorCode = WSAGetLastError();
printf("Failed to connect server,error:%d./r/n",iErrorCode);
WSACleanup();
return;
}
char recvBuf[100]="";//用来得到文件的下载地址
FILE *fileClient= fopen(FILESAVEADDR,"w+");
send(clientSock,"dd",3,0);
recv(clientSock,recvBuf,sizeof(recvBuf),0);
printf("the length %d/n",strlen(recvBuf));
printf("%s/n",recvBuf);
fwrite(recvBuf,sizeof(char),strlen(recvBuf),fileClient);
memset(recvBuf,0,100);
printf("文件已经下载完毕!下载的目录地址为 %s/n",FILESAVEADDR);
fclose(fileClient);
}