#include <stdio.h>
#include <stdlib.h>
#include <winsock.h>
#include <windows.h>
#include <conio.h>
#pragma comment(lib,"ws2_32.lib ")
#define SERVER_PORT 6666
DWORD WINAPI servproc (LPVOID pParam);
int main()
{
WORD wVersionRequested;
WSADATA wsadata;
int ret, length;
SOCKET sServer, sListen;
struct sockaddr_in saServer,saClient;
/*FILE *fps, *fpd;
char buf[188*1024];
char ch, filesize[256];
int chance,ifilelen,ilen,finishlen;
char srcfile[256];// = "./servfile/upload/1.txt";
char dstfile[256];// = "./servfile/download/5.txt";
*/
// char ch;
HANDLE thread;
wVersionRequested = MAKEWORD(2,2);
ret = WSAStartup(wVersionRequested,&wsadata);
if(ret != 0)
{
printf("WSAStartup() failed \n");
return 1;
}
//确认Winsock DLL支持2.2版本
if(LOBYTE(wsadata.wVersion) != 2 || HIBYTE(wsadata.wVersion) != 2)
{
WSACleanup();
printf("Invalid winsock version \n");
return 2;
}
sListen = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(sListen == INVALID_SOCKET)
{
WSACleanup();
printf("socket failed! \n");
return 3;
}
saServer.sin_family = AF_INET;
saServer.sin_port = htons(SERVER_PORT);
saServer.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
ret = bind(sListen,(struct sockaddr *)&saServer,sizeof(saServer));
if( ret == SOCKET_ERROR)
{
printf("bind() failed! error code: %d\n \n",WSAGetLastError());
closesocket(sListen);
WSACleanup();
return 4;
}
printf("Server is listenning!\n");
printf("Waiting for client connecting !\n");
printf("Ctrl + C to quit \n");
while(1)
{
ret = listen(sListen,5);
if( ret == SOCKET_ERROR)
{
printf("listem() failed! error code: %d\n \n",WSAGetLastError());
closesocket(sListen);
WSACleanup();
return 5;
}
length = sizeof(saClient);
sServer = accept(sListen,(struct sockaddr *)&saClient, &length);
if(sServer == INVALID_SOCKET)
{
printf("accept() failed! error code : %d\n",WSAGetLastError());
closesocket(sListen);
WSACleanup();
return 6;
}
thread = CreateThread(NULL,0,servproc,(LPVOID)sServer,0,0);
CloseHandle(thread);
printf("Accepted client: %s:%d\n",
inet_ntoa(saClient.sin_addr),ntohs(saClient.sin_port));
// Sleep(10);
/* memset(buf, 0, sizeof(buf));
ret = recv(sServer,buf,sizeof(buf),0);
if(ret == SOCKET_ERROR)
{
printf("recv() failed \n");
return 8;
}
chance = atoi(&buf[0]);
memset(buf, 0, sizeof(buf));
//printf("%d\n",chance);
//chance = 1;
if(chance ==1 )
{
printf("Please input upload file address:\n");
scanf("%s",srcfile);
fps = fopen(srcfile,"rb");
if(fps == NULL)
{
printf("Open upload file failed!\n");
return 7;
}
fseek(fps,0,SEEK_END);
ifilelen = ftell(fps);
fseek(fps,0,SEEK_SET);
itoa(ifilelen,filesize,10);
send(sServer,filesize,256+1,0);
//printf("%d\n",ifilelen);
printf("文件开始上传!\n");
while(ifilelen>0)
{
memset(buf, 0, sizeof(buf));
ilen = fread(buf,1,sizeof(buf),fps);
ifilelen -= ilen;
ret = send(sServer,buf,ilen,0);
if(ret==SOCKET_ERROR)
{
//发送错误
closesocket(sServer);
return 9;
}
memset(buf, 0, ilen);
}
printf("文件上传完成!\n");
}
else
{
printf("Please input download file address:\n");
scanf("%s",dstfile);
fpd = fopen(dstfile,"wb");
if( fpd == NULL )
{
printf("Open download file failed!\n");
return 7;
}
memset(buf, 0, sizeof(buf));
recv(sServer,buf,256+1,0);
memmove(filesize,buf,256+1);
ifilelen = atoi(filesize);
printf("文件开始下载!\n");
finishlen = 0;
while( ifilelen - finishlen > 0 )
{
memset(buf, 0, sizeof(buf));
ret = recv(sServer,buf,sizeof(buf),0);
finishlen +=ret;
if(ret == SOCKET_ERROR)
{
printf("recv() failed \n");
break;
}
if(ret == 0)
{
printf("Server has closed the connection \n");
break;
}
fwrite(buf, 1, ret, fpd);
memset(buf, 0, sizeof(buf));
printf("文件下载完成%2d%%\n",finishlen*100/ifilelen);
}
if(ret==0)
printf("文件下载被迫中断!\n");
else
printf("文件下载完成!\n");
}*/
//puts("press Esc to exit!Press any other key to continue!\n");
//if( (ch = getch())== 27 )
// break;
}
closesocket(sListen);
WSACleanup();
return 0;
}
DWORD WINAPI servproc (LPVOID pParam)
{
SOCKET sServer;
FILE *fps, *fpd;
char buf[188*1024];
char filesize[256];
int chance,ret;
long ilen,finishlen,ifilelen;
char srcfile[256];// = "./servfile/upload/1.txt";
char dstfile[256];// = "./servfile/download/5.txt";ch,
sServer = (SOCKET)pParam;
memset(buf, 0, sizeof(buf));
ret = recv(sServer,buf,sizeof(buf),0);
if(ret == SOCKET_ERROR)
{
printf("recv() failed \n");
return 8;
}
chance = atoi(&buf[0]);
memset(buf, 0, sizeof(buf));
//printf("%d\n",chance);
//chance = 1;
if(chance ==1 )
{
printf("Please input upload file address:\n");
scanf("%s",srcfile);
fps = fopen(srcfile,"rb");
if(fps == NULL)
{
printf("Open upload file failed!\n");
return 7;
}
fseek(fps,0,SEEK_END);
ifilelen = ftell(fps);
fseek(fps,0,SEEK_SET);
itoa(ifilelen,filesize,10);
send(sServer,filesize,256+1,0);
//printf("%d\n",ifilelen);
printf("文件开始上传!\n");
while(ifilelen>0)
{
memset(buf, 0, sizeof(buf));
ilen = fread(buf,1,sizeof(buf),fps);
ifilelen -= ilen;
ret = send(sServer,buf,ilen,0);
if(ret==SOCKET_ERROR)
{
//发送错误
closesocket(sServer);
return 9;
}
memset(buf, 0, ilen);
}
fclose(fps);
printf("文件上传完成!\n");
}
else
{
printf("Please input download file address:\n");
scanf("%s",dstfile);
fpd = fopen(dstfile,"wb");
if( fpd == NULL )
{
printf("Open download file failed!\n");
return 7;
}
memset(buf, 0, sizeof(buf));
recv(sServer,buf,256+1,0);
memmove(filesize,buf,256+1);
ifilelen = atoi(filesize);
printf("文件开始下载!\n");
finishlen = 0;
while( ifilelen - finishlen > 0 )
{
memset(buf, 0, sizeof(buf));
ret = recv(sServer,buf,sizeof(buf),0);
finishlen +=ret;
if(ret == SOCKET_ERROR)
{
printf("recv() failed \n");
break;
}
if(ret == 0)
{
printf("Server has closed the connection \n");
break;
}
fwrite(buf, 1, ret, fpd);
memset(buf, 0, sizeof(buf));
printf("文件下载完成%.2f%%\n",finishlen/1000*1.0/ifilelen*100*1000);
}
if(ret==0)
printf("文件下载被迫中断!\n");
else
printf("文件下载完成!\n");
fclose(fpd);
}
printf("Server is listenning!\n");
printf("Waiting for client connecting !\n");
printf("Ctrl + C to quit \n");
return 0;
}