主要是复习socket通信过程,客户端过程:申请socket,connect,recv/send ,close
客户端过程:socket,bind,listen,accept,send/recv,close
/***客户端输入你所要传输的文件名,服务器端就是输入接收后保存的文件名***/
客户端demo:
/*****文件传输之客户端*****/
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
#include<arpa/inet.h>
#include<errno.h>
#include<fcntl.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<pthread.h>
#include<stdio.h>
#include<sys/ipc.h>
#define FILE_NAME_MAX_SIZE 512
#define BUFFER_SIZE 1024
int main(int argc, char *argv[])
{
int client_sockfd;
struct sockaddr_in remote_addr; //服务器端网络地址结构体
memset(&remote_addr,0,sizeof(remote_addr)); //数据初始化--清零
remote_addr.sin_family=AF_INET; //设置为IP通信
remote_addr.sin_addr.s_addr=inet_addr("127.0.0.1");//服务器IP地址
remote_addr.sin_port=htons(8000); //服务器端口号
/*创建客户端套接字--IPv4协议,面向连接通信,TCP协议*/
if((client_sockfd=socket(PF_INET,SOCK_STREAM,0))<0)
{
perror("create client socket error.\n");
exit(EXIT_FAILURE);
}
/*将套接字绑定到服务器的网络地址上*/
if(connect(client_sockfd,(struct sockaddr *)&remote_addr,sizeof(struct sockaddr))<0)
{
perror("connect error\n");
return -1;
}
char file_name[FILE_NAME_MAX_SIZE+1];
bzero(file_name,FILE_NAME_MAX_SIZE+1);
printf("please input the file name:\n");
scanf("%s",file_name);
char buffer[BUFFER_SIZE];
bzero(buffer,BUFFER_SIZE);
strncpy(buffer,file_name,strlen(file_name)>BUFFER_SIZE ?BUFFER_SIZE:strlen(file_name)); //文件最大长度规定为1024
send(client_sockfd,buffer,BUFFER_SIZE,0);
FILE *fp=fopen(file_name,"r");
if(fp==NULL)
{
printf("file:\t %s can not open to write.\n",file_name);
exit(EXIT_FAILURE);
}
else
{
bzero(buffer,BUFFER_SIZE);
int file_block_length=0;
while((file_block_length=fread(buffer,sizeof(char),BUFFER_SIZE,fp))>0)
{
if(send(client_sockfd,buffer,file_block_length,0)<0)
{
printf("send file error\n");
break;
}
bzero(buffer,BUFFER_SIZE);
}
fclose(fp);
printf("file:\t %s transfer finished\n",file_name);
}
close(client_sockfd);
return 0;
}
服务器端demo:
/*****文件传输之服务器端*****/
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<unistd.h>
#include<stdlib.h>
#include<netinet/in.h>
#include<string.h>
#include<pthread.h>
#include<signal.h>
#include<time.h>
#include<errno.h>
#include<dirent.h>
#include<sys/stat.h>
#define FILE_NAME_MAX_SIZE 512
int main(int argc, char *argv[])
{
int server_sockfd;//服务器端套接字
int client_sockfd;//客户端套接字
int len;
struct sockaddr_in my_addr; //服务器网络地址结构体
struct sockaddr_in remote_addr; //客户端网络地址结构体
int sin_size;
memset(&my_addr,0,sizeof(my_addr)); //数据初始化--清零
my_addr.sin_family=AF_INET; //设置为IP通信
my_addr.sin_addr.s_addr=INADDR_ANY;//服务器IP地址--允许连接到所有本地地址上
my_addr.sin_port=htons(8000); //服务器端口号
/********TCP/IP协议**********/
/*创建服务器端套接字--IPv4协议,面向连接通信,TCP协议*/
if((server_sockfd=socket(PF_INET,SOCK_STREAM,0))<0)
{
fprintf(stderr,"creat socket fd error\n");
exit(EXIT_FAILURE);
}
/*将套接字绑定到服务器的网络地址上*/
if (bind(server_sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))<0)
{
perror("bind error\n");
exit(EXIT_FAILURE);
}
/*监听连接请求--监听队列长度为5*/
if((listen(server_sockfd,1000))==-1)
{
fprintf(stderr,"create socket fd error \n");
exit(EXIT_FAILURE);
}
char file_name[FILE_NAME_MAX_SIZE+1];
bzero(file_name,FILE_NAME_MAX_SIZE+1);
printf("enter the name:\n");
scanf("%s",file_name);
while(1)
{
sin_size=sizeof(struct sockaddr_in);
if((client_sockfd=accept(server_sockfd,(struct sockaddr *)&remote_addr,&sin_size))<0)
{
perror("accept");
return 1;
}
char buf[BUFSIZ]; //数据传送的缓冲区
bzero(buf,BUFSIZ);
int length=recv(client_sockfd,buf,BUFSIZ,0);
if(length<0)
{
printf("server receive data failed.\n");
break;
}
FILE *fp=fopen(file_name,"w");
if(NULL==fp)
{
printf("file:\t %s not found..\n",file_name);
}
else
{
bzero(buf,BUFSIZ);
int length=0;
while(length=recv(client_sockfd,buf,BUFSIZ,0))
{
if(length<0)
{
printf("receive file failed..\n");
break;
}
int write_length=fwrite(buf,sizeof(char),BUFSIZ,fp);
if(write_length<length)
{
printf("file:\t %s failed..\n",file_name);
break;
}
bzero(buf,BUFSIZ);
}
printf("finish..\n");
}
fclose(fp);
close(client_sockfd);
close(server_sockfd);
return 0;
}
}