/*server.c*/
#include<sys/socket.h>
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<memory.h>
#include<sys/mman.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<string.h>
#define PORT 1234
#define MAX_QUE 5
#define BUF_SIZE 1024
int main(int argc,char *argv[])
{
struct sockaddr_in local_sockaddr;
int socketfd;//本地socket套接字描述符
int clientfd;//客户端链接描述符
int recvbytes;//收到的字节数
FILE *fp;//文件打开描述符
size_t fsize;//写入内容的大小
char buf[BUF_SIZE];
if(argc!=2)
{
printf("参数错误!正确格式:./server filename\n");
exit(1);
}
if((socketfd=socket(AF_INET,SOCK_STREAM,0))<0)//建立socket连接
{
perror("socket");
exit(1);
}
printf("Socket id = %d\n",socketfd);
local_sockaddr.sin_family = AF_INET;
local_sockaddr.sin_port = htons(PORT);
local_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
printf("server ip =%s\n",inet_ntoa(local_sockaddr.sin_addr));//输出服务器ip地址
memset(local_sockaddr.sin_zero,0,8);
int i;
setsockopt(socketfd,SOL_SOCKET,SO_REUSEADDR,&i,sizeof(i));//允许重复使用本地的地址与套接字进行绑定
if(bind(socketfd,(struct sockaddr *)&local_sockaddr,sizeof(struct sockaddr))<0)//绑定函数bind()
{
perror("bind");
exit(1);
}
printf("Bind success!\n");
if(listen(socketfd,MAX_QUE)<0)//利用listen()设置被动监听
{
perror("listen");
exit(1);
}
printf("Listening>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n");
if((clientfd=accept(socketfd,(struct sockaddr*)NULL,NULL))<0)//调用accept()函数,等待客户端的链接
{
perror("accept");
exit(1);
}
printf("connect client success\n");
struct sockaddr_in client;
char client_ip[20];
socklen_t client_len = sizeof(client);
getpeername(clientfd, (struct sockaddr *)&client, &client_len);
inet_ntop(AF_INET, &client.sin_addr, client_ip, sizeof(client_ip));
printf("client ip:%s\tport:%d\n", client_ip, ntohs(client.sin_port));
memset(buf,0,sizeof(buf));//初始化缓冲区
if((recvbytes = recv(clientfd,buf,BUF_SIZE,0))<0)
{
perror("recv");
exit(1);
}
printf("Receive messages from ip:%s\tport:%d\n%s\n",client_ip,ntohs(client.sin_port),buf);
if(strcmp(buf,"")==0)
{
printf("Receive no message!There is no message to write to %s\n",argv[1]);
exit(1);
}
fp=fopen(argv[1],"w+");
if(fp==NULL)
{
perror("fopen");
exit(1);
}
if((fsize=fwrite(&buf,sizeof(buf),1,fp))<0)
{
perror("fwrite");
exit(1);
}
printf("write to %s success!\n",argv[1]);
fclose(fp);
close(clientfd);//关闭client链接
close(socketfd);//关闭socket连接
}
/*client.c*/
#include<sys/socket.h>
#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<memory.h>
#include<sys/mman.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#include<string.h>
#define PORT 1234
#define BUF_SIZE 1024
int main(int argc,char *argv[])
{
int socketfd;//客户端socket套接字描述符
FILE *fp;//文件打开描述符
int len;
size_t fsize;//读到的内容大小
struct sockaddr_in serv_addr;//服务器地址结构体
char serv_ip[20], clie_ip[20];
char buf[BUF_SIZE];
int sendbytes;//发送字节数
if(argc!=3)
{
printf("参数错误!正确格式:./client serverip filename\n");
exit(1);
}
if((socketfd=socket(AF_INET,SOCK_STREAM,0))<0)//建立socket连接
{
perror("socket");
exit(1);
}
printf("Socket id = %d\n",socketfd);
memset(buf,0,sizeof(buf));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
inet_aton(argv[1], &serv_addr.sin_addr);
memset(serv_addr.sin_zero,0,8);
if(connect(socketfd, (struct sockaddr*)&serv_addr, sizeof(struct sockaddr))< 0) //调用connect()函数连接服务器
{
printf("can not connect to %s, exit!\n", argv[1]);
printf("%s\n", strerror(errno));
exit(1);
}
else
printf("connect to %s success!\n",argv[1]);
fp=fopen(argv[2],"r+");
if(fp==NULL)
{
perror("fopen");
exit(1);
}
fseek(fp, 0L, SEEK_END);
len = ftell(fp);
rewind(fp);
if((fsize=fread(&buf,len,1,fp))<0)
{
perror("fread");
exit(1);
}
struct sockaddr_in serv;
socklen_t serv_len = sizeof(serv);
getpeername(socketfd, (struct sockaddr*)&serv, &serv_len);
inet_ntop(AF_INET, &serv.sin_addr, serv_ip, sizeof(serv_ip));
printf("server ip:%s\tport:%d\n", serv_ip, ntohs(serv.sin_port));
if((sendbytes=send(socketfd,buf,sizeof(buf),0))<0)
{
perror("send");
exit(1);
}
fclose(fp);
close(socketfd);//关闭socket连接
return 0;
}