发送端:
/**************************************
客户端
**************************************/
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <pthread.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int sockfd;
//./main 192.168.5.2 filename
//获取文件的大小
int get_file_size(char *filename)
{
int fd = open(filename,O_RDWR);
if(fd < 0)
{
perror("open fail:");
exit(0);
}
//把文件光标偏移到末尾获取文件的大小
int size = lseek(fd,0,SEEK_END);
close(fd);
return size;
}
int main(int argc, char **argv)
{
if(argc != 3)
{
perror("input IP and filename:");
return -1;
}
//1.创建网络通信对象
sockfd = socket(AF_INET, SOCK_STREAM , 0);
if (sockfd == -1)
{
perror("socket error!");
return 0;
}
//2.定义网络信息结构体
struct sockaddr_in seraddr;
seraddr.sin_family = AF_INET;
seraddr.sin_port = htons(3333);
seraddr.sin_addr.s_addr = inet_addr(argv[1]);
//3.链接网络服务器
int ret = connect(sockfd, (struct sockaddr *)&seraddr, sizeof(struct sockaddr));
if (ret == -1)
{
perror("connect error");
return 0;
}
else
{
printf("connect ok\n");
}
//打开文件并获取文件的大小
int filesize = get_file_size(argv[2]);
//发送文件名与文件的大小
char cmd[1024]={0};
//数据格式 filename size
sprintf(cmd,"%s %d",argv[2],filesize);
write(sockfd,cmd,strlen(cmd));
//等待对方回应
char ret_data[10];
printf("等待对方回应\n");
read(sockfd,ret_data,10);
//发送文本内容
if(strstr(ret_data,"OK"))
{
//打开源文件发送数据
int fd = open(argv[2],O_RDWR);
//发送文本内容
while(1)
{
char buf[1024]={0};
int size = read(fd,buf,1024);
if(size == 0)
{
printf("文件发送完毕\n");
close(fd);
break;
}
write(sockfd,buf,size);
}
}
else
{
printf("对方拒绝接收文件\n");
exit(0);
}
}
接收端:
/**************************************
服务端
**************************************/
#include <stdio.h>
#include <sys/types.h> /* See NOTES */
#include <sys/socket.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h> /* superset of previous */
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int new_sockfd;
int main(int argc, char **argv)
{
//1.创建网络通信对象
int sockfd = socket(AF_INET, SOCK_STREAM , 0);
if (sockfd == -1)
{
perror("socket error!");
return 0;
}
//2.设置服务器的IP地址信息
struct sockaddr_in seraddr;
seraddr.sin_family = AF_INET;
seraddr.sin_port = htons(3333);
seraddr.sin_addr.s_addr = inet_addr("0.0.0.0");
//0.0.0.0 万用地址,让系统自动监听本地网卡
//3.绑定(注册)服务器IP地址信息
int ret = bind(sockfd, (struct sockaddr *)&seraddr,sizeof(struct sockaddr));
if (ret != 0)
{
perror("bind error");
return 0;
}
else
{
printf("bind ok\n");
}
//4.把网络地址设置为监听模式
ret = listen(sockfd, 10);
if (ret != 0)
{
perror("listen error");
return 0;
}
else
{
printf("listen ok\n");
}
//5.等待链接请求
printf("等待监听请求\n");
//获取客户端信息
struct sockaddr_in address;
int len = sizeof(address);
new_sockfd = accept(sockfd, (struct sockaddr *)&address, &len);
if (new_sockfd < 0)
{
printf("accept error\n");
return 0;
}
printf("port = %d\n", ntohs(address.sin_port));
printf("ip = %s\n", inet_ntoa(address.sin_addr));
//接受客户端发送过来的数据
//读取头信息
char head[1024]={0};
read(new_sockfd,head,1024);
if(strstr(head," "))
{
printf("用户发送文件是否接收 1 OK 2 NO\n");
int a = 0;
scanf("%d",&a);
if(a == 1) //接收
{
write(new_sockfd,"OK",strlen("OK"));
//创建新文件
char filename[1024]={0};
int filesize=0;
sscanf(head,"%s %d",filename,&filesize);
filename[0] = 'N';
printf("%s %d\n",filename,filesize);
int fd = open(filename,O_RDWR|O_CREAT|O_TRUNC,0777);
//开始接收文本数据
int len=0;
while(1)
{
char buf[1024]={0};
int ret = read(new_sockfd,buf,1024);
len+= ret;
write(fd,buf,ret);
if(len == filesize)
{
printf("文件接收完毕\n");
close(fd);
break;
}
}
}
else
{
write(new_sockfd,"NO",strlen("NO"));
}
}
else
{
printf("head=%s\n",head);
}
}