当前位置:文档之家› 基于Socket的文件传输协议的设计

基于Socket的文件传输协议的设计

程序清单:

服务器:

#include"Winsock.h"

#include"windows.h"

#include"stdio.h"

#pragma comment(lib,"wsock32.lib")

#define RECV_PORT 1024

#define SEND_PORT 1025

#define MAX_FILESIZE 32*1024

SOCKET sock,sockl;

sockaddr_in ServerAddr;

sockaddr_in ClientAddr;

struct Filedata

{ char ffname[30];

char ffdata[MAX_FILESIZE];

int len;}DataPacket;

int Addrlen;

DWORD StartSock()

{ WSADATA WSAData;

if(WSAStartup(MAKEWORD(2,2),&WSAData)!=0)

{printf("sock初始化失败!\n");

return(-1);}

return(1);}

DWORD CreateSocket()

{ sock=socket(AF_INET,SOCK_STREAM,0);

if(sock==SOCKET_ERROR)

{ printf("sock创建失败!\n");

WSACleanup();

return(-1);}

ServerAddr.sin_family=AF_INET;

ServerAddr.sin_addr.s_addr=htonl(INADDR_ANY);

ServerAddr.sin_port=htons(RECV_PORT);

if(bind(sock,(struct sockaddr FAR*)&ServerAddr, sizeof(ServerAddr))==SOCKET_ERROR)

{ printf("绑定出错");

return(-1);}

return(1);}

DWORD WriteFile(char*fname,char*fdata,int flen) { int i;

FILE *fp;

fp=fopen(fname,"w");

if(fp==NULL)

{printf("不能打开文件\n");}

i=0;

for(i=0;i

{fputc(fdata[i],fp);}

fclose(fp);

return(1);}

DWORD ConnectProcess()

{ Addrlen=sizeof(sockaddr_in);

if(listen(sock,5)<0)

{ printf("监听出错");

return(-1);}

printf("监听...\n");

for(;;)

{sockl=accept(sock,(struct sockaddr FAR*)&ClientAddr,&Addrlen);

for(;;)

{ memset(DataPacket.ffname,0,30);

memset(DataPacket.ffdata,0,MAX_FILESIZE);

DataPacket.len=0;

if(recv(sockl,(char*)&DataPacket,sizeof(DataPacket),0)<=0)

{break;}

printf("已经接收到文件:%s\n",DataPacket.ffname);

printf("文件大小:%d\n",DataPacket.len);

WriteFile(DataPacket.ffname,DataPacket.ffdata,DataPacket.len);

printf("\n");}}}

int main()

{ if(StartSock()==-1)

return(-1);

if(CreateSocket()==-1)

return(-1);

if(ConnectProcess()==-1)

return(-1);

return(1);}

客户机:

#include"Winsock.h"

#include"windows.h"

#include"stdio.h"

#include"stdlib.h"

#include"string.h"

#pragma comment(lib,"wsock32.lib")

#define RECV_PORT 1024

#define SEND_PORT 1025

#define MAX_FILESIZE 32*1024

SOCKET sock;

sockaddr_in ServerAddr;

struct Filedata

{ char ffname[30];

char ffdata[MAX_FILESIZE];

int len;

}DataPacket;

DWORD StartSock()

{ WSADATA WSAData;

if(WSAStartup(MAKEWORD(2,2),&WSAData)!=0)

{ printf("sock初始化失败!\n");

return(-1);}

ServerAddr.sin_family=AF_INET;

ServerAddr.sin_addr.s_addr=inet_addr("127.0.0.1");

ServerAddr.sin_port=htons(RECV_PORT);

return(1);}

DWORD CreateSocket()

{ sock=socket(AF_INET,SOCK_STREAM,0);

if(sock==SOCKET_ERROR)

{ printf("sock创建失败!\n");

WSACleanup();

return(-1);}

return(1);}

void CallServer()

{ CreateSocket();

while(connect(sock,(struct sockaddr*)&ServerAddr, sizeof(ServerAddr))==SOCKET_ERROR)

{printf("连接...\n");}}

DWORD GetFile(char*fname)

{ FILE*fp;

int Filesize;

int i;

int count,total=0;

char buffer[100];

char Senddata[MAX_FILESIZE];

fp=fopen(fname,"r");

if(fp==NULL)

{ printf("不能打开文件\n");

return(0);}

i=0;

Filesize=0;

memset(Senddata,0,MAX_FILESIZE);

while(!feof(fp))

{count=fread(buffer,sizeof(char),100,fp);

if(ferror(fp))

{printf("读文件出错");

break;}

Filesize+=count;

相关主题