在上次文件传输的程序基础上,利用循环面向连接、循环无连接方式完成linux和windows平台的文件传输,并完成客户端、服务器端代码的封装。
1.使用TCP实现
2.使用UDP实现
1.tcp_server.c
//Run in Linux
#include
#include
#include
#include
#include
#include
#include
#define PORT 8888
#define BUFFER_SIZE 1024
void get_filename(char *filepath,char *filename)
{
/*解析文件名*/
int i=0,k=0;
for(i=strlen(filepath);i>=0;i--)
{
if(filepath[i]!='/')
{
k++;
}
else
break;
}
strcpy(filename,filepath+(strlen(filepath)-k)+1);
}
void process_conn_server(int sd)
{
ssize_t size = 0;
char buffer[BUFFER_SIZE];
FILE *stream;
char filepath[100];
strcpy(buffer,"please enter a path!\n");
send(sd,buffer,BUFFER_SIZE,0);
int length = 0;
memset(filepath,'\0',sizeof(filepath));
length = recv(sd,filepath,100,0);
if(length < 0){
printf("recv error!\n");
}
else
{
char filename[100] = {'\0'};
get_filename(filepath,filename);
printf("server: filename:%s\n",filename);
if( (stream=fopen(filename, "w")) == NULL){
printf("server:open file error!\n");
return;
}
while(1){/*读取文件并写入文件流*/
size = recv(sd, buffer, BUFFER_SIZE,0);
printf("server:size:%d\n",size);
if(size <= 0){
break;
}
int write_len=fwrite(buffer, sizeof(char), size, stream);
}
printf("recv finished!\n");
fclose(stream);
}
}
int main(int argc, char *argv[]){
int socksd,sockcd;
struct sockaddr_in server,client;
pid_t pid;
if( (socksd = socket(AF_INET,SOCK_STREAM,0)) < 0)
{
printf("socket create error!\n");
return -1;
}
printf("socket create success!\n");
/*加入此代码是为了避免再次打开服务器程序出现bind error的错误*/
int on = 1;
int ret = setsockopt(socksd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));
memset(&server,0,sizeof(server));
server.sin_family = AF_INET;
server.sin_addr.s_addr = htonl(INADDR_ANY);
server.sin_port = htons(PORT);
if( (bind(socksd,(struct sockaddr*)&server,sizeof(server)) < 0))
{
printf("socket bind error!\n");
return -1;
}
printf("socket bind success!\n");
if( (listen(socksd,10)) < 0)
{
printf("socket listen error!\n");
return -1;
}
printf("socket listen success!\n");
printf("waiting...\n");
/*显示核是sigchld信号*/
if(signal(SIGCHLD, SIG_IGN) == SIG_ERR){
perror("signal error");
return EXIT_SUCCESS;
}
while(1){
socklen_t addr_len = sizeof(struct sockaddr);
if( (sockcd = accept(socksd, (struct sockaddr*)&client, &addr_len)) < 0)
{
//出错
continue;
}
printf("server:accept\n");
//different from exp2
process_conn_server(sockcd);
close(sockcd);/*在子进程中关闭服务器的监听*/
}
return 0;
}
2.tcp_client.c
//Run in Windows
#pragma comment(lib,"ws2_32")
#include
#include
#include
#define PORT 8888
#define BUFFER_SIZE 1024
typedef struct sockaddr_in addr;
int Initsocket(void);
void process_conn_client(SOCKET s);
int Initsocket(void)
{
WSADATA wsadata;
WORD version;
int err;
version = MAKEWORD(2, 2);
err = WSAStartup(version, &wsadata);
if(err)
{
printf("Error %d:winsock not available\n",err);
return 1;
}
return 0;
}
void process_conn_client(SOCKET s)
{
ssize_t size = 0;
char buffer[BUFFER_SIZE];
FILE *stream;
char filepath[100] = {'\0'};
size = recv(s, buffer, BUFFER_SIZE,0);
if(size < 0)
printf("read error!\n");
printf("%s",buffer);
scanf("%s",filepath);
send(s,filepath,100,0);
if( (stream = fopen(filepath,"r")) == NULL) {
printf("client:open file error!\n");
return;
}
printf("sending!\n");
while(1){
size = fread(buffer,sizeof(char),BUFFER_SIZE,stream);
if(size <= 0){
break;
}
send(s,buffer,size,0);
}
printf("send finished!\n");
fclose(stream);
}
int main()
{
SOCKET client;
int err;
addr server_addr;
Initsocket();
if( (client = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET )
{
printf("no more socket resource\n");
return 1;
}
//服务器地址初始化
server_addr.sin_family = PF_INET;
server_addr.sin_port = htons(PORT);
server_addr.sin_addr.S_un.S_addr = inet_addr("192.168.195.137");//使用自己服务器端的IP
if( (err = connect(client, (struct sockaddr*)
&server_addr, sizeof(addr))) == INVALID_SOCKET)
{
printf("error %d:cannot connect to server\n",err);
return 1;
}
else
printf("link server is successful\n");
process_conn_client(client);
closesocket(client);
WSACleanup();
return 0;
}
3.运行结果
1.udp_server.c
//Run in Windows
#pragma comment(lib,"ws2_32")
#include
#include
#include
#define PORT 8888
#define BUFFER_SIZE 1024
typedef struct sockaddr_in addr;
int Initsocket(void);
int Initsocket(void)
{
WSADATA wsadata;
WORD version;
int err;
version = MAKEWORD(2, 2);
err = WSAStartup(version, &wsadata);
if(err)
{
printf("Error %d:winsock not available\n",err);
return 1;
}
return 0;
}
void get_filename(char *filepath,char *filename)
{
/*解析文件名*/
int i=0,k=0;
for(i=strlen(filepath);i>=0;i--)
{
if(filepath[i]!='/')
{
k++;
}
else
break;
}
strcpy(filename,filepath+(strlen(filepath)-k)+1);
}
void process_conn_server(SOCKET s)
{
addr client;
int addrlen;
char filename[100];
char filepath[100];
char *buffer;//file buffer
int fileTrans;
buffer = (char *)malloc(sizeof(char)*BUFFER_SIZE);
memset(buffer,0,BUFFER_SIZE);
int lenfilepath;
FILE *fp;
int writelength;
addrlen = sizeof(client);
while(1)
{
memset(filename,'\0',sizeof(filename));
memset(filepath,'\0',sizeof(filepath));
//接受文件路径,返回文件路径的长度
lenfilepath = recvfrom(s,filepath,100,0,(struct sockaddr*)&client,&addrlen);
ntohl(client.sin_addr.s_addr);
printf("%s",inet_ntoa(client.sin_addr));
printf("filepath :%s\n",filepath);
if(lenfilepath<0)
{
printf("recv error!\n");
exit(1);
}
//从路径中提取出文件名
else
{
get_filename(filepath,filename);
}
printf("filename :%s\n",filename);
//以写的方式打开文件
fp = fopen(filename,"w");
if(fp!=NULL)
{
//接受文件内容buffer,存在buffer中
while((fileTrans =recvfrom(s,buffer,BUFFER_SIZE,0,(struct sockaddr*)&client,&addrlen)))
{
printf("fileTrans: %d\n",fileTrans);
if(fileTrans<0)
{
printf("recv error!\n");
break;
}
//把buffter中的文件内容写到fp指针指的文件中
writelength = fwrite(buffer,sizeof(char),fileTrans,fp);
if(writelength
2.udp_client.c
//Run in Linux
#include
#include
#include
#include
#include
#include
#include
#include //for malloc
#include
#define BUFFER_SIZE 1024
int main()
{
int sockcd;
struct sockaddr_in server;
char filepath[100];//file to translate
FILE *fp;
int lenpath; //filepath length
char *buffer;//file buffer
int fileTrans;
buffer = (char *)malloc(sizeof(char)*BUFFER_SIZE);
bzero(buffer,BUFFER_SIZE);
//memset(buffer,0,sizeof(buffer));
//创建套接字
if((sockcd = socket(AF_INET,SOCK_DGRAM,0))<0)
{
printf("socket build error!\n");
}
memset(&server,0,sizeof(server));
server.sin_family= AF_INET;
server.sin_port = htons(8888);
if(inet_pton(AF_INET,"192.168.195.1",&server.sin_addr)<0)
{
printf("inet_pton error!\n");
}
printf("file path:\n");
scanf("%s",filepath);//get filepath
//以读的方式打开文件
fp = fopen(filepath,"r");//opne file
//文件找不到直接退出程序
if(fp==NULL)
{
printf("filepath not found!\n");
return 0;
}
printf("filepath : %s\n",filepath);
lenpath = sendto(sockcd,filepath,strlen(filepath),0,(struct sockaddr *)&server,sizeof(server));// put file path to sever
if(lenpath<0)
{
printf("filepath send error!\n");
}
else
{
printf("filepath send success!\n");
}
sleep(3);
while((fileTrans = fread(buffer,sizeof(char),BUFFER_SIZE,fp)) >= 0)
{
printf("fileTrans =%d\n",fileTrans);
//发送文件的内容
int flag;
if( (flag = sendto(sockcd,buffer,fileTrans,0,(struct sockaddr *)&server,sizeof(server))) < 0)
{
printf("send failed!\n");
break;
}
else if(flag == 0){
printf("send finished!\n");
break;
}
bzero(buffer,BUFFER_SIZE);
//memset(buffer,0,sizeof(buffer));
}
fclose(fp);
close(sockcd);
return 0;
}
3.运行结果
LINUX作为客户端
WINDOWS作为服务器端
传输成功