程序代码
server.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#define PORT 8888
void *Send(void arg)
{
char buf[32]={0};
while(1)
{
scanf("%s",buf);
send((int *)arg,buf,strlen(buf),0);
memset(buf,0,32);
}
}
int main()
{
int ret,sockfd,fd;
struct sockaddr_in server_addr;//服务器信息
struct sockaddr_in client_addr;//客户端信息
/*
1、IPV4协议
2、流式套接字(TCP)
*/
if(-1==sockfd)
{
perror("socket");
exit(1);
}
memset(&server_addr,0,sizeof(server_addr));
server_addr.sin_family=PF_INET;
server_addr.sin_port=htons(PORT);
server_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
int length=sizeof(server_addr);
ret=bind(sockfd,(struct sockaddr *)&server_addr,length);
if(-1==ret)
{
perror("bind");
exit(1);
}
ret=listen(sockfd,5);
if(-1==ret)
{
perror("listen");
exit(1);
}
length=sizeof(client_addr);
fd=accept(sockfd,(struct sockaddr *)&client_addr,&length);
if(-1==fd)
{
perror("accept");
exit(1);
}
printf("accept fd %d,port %d\n",fd,client_addr.sin_port);
pthread_t tid;
ret=pthread_create(&tid,NULL,Send,&fd);
if(-1==ret)
{
perror("pthread_create");
exit(1);
}
char buf[32]={0},buf1[100]={0};
FILE *fp;
int fret;
while(1)
{
ret=recv(fd,buf,32,0);
if(-1==ret)
{
perror("recv");
}
printf("%s\n",buf);
if(strcmp(buf,"bye")==0)
{
break;
}
memset(buf,0,32);
}
if(strcmp(buf,"bye")==0)
{
sleep(3);
fp=fopen("hello.txt","a+");
if(NULL==fp)
{
perror("fopen");
exit(1);
}
fseek(fp,0,SEEK_SET);
fret =fread(buf1,1,100,fp);
if(0==fret)
{
perror("ffread");
exit(1);
}
// fclose(fp);
printf("内容为:%s\n",buf1);
}
fclose(fp);
return 0;
}
client.c程序
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <pthread.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#define PORT 8888
void *Recv(void arg)
{
char buf[32]={0};
while(1)
{
recv((int *)arg,buf,32,0);
printf("%s\n",buf);
memset(buf,0,32);
}
}
int main()
{
int sockfd;
struct sockaddr_in server_addr;
sockfd=socket(PF_INET,SOCK_STREAM,0);
if(-1==sockfd)
{
perror(“socket”);
exit(1);
}
memset(&server_addr,0,sizeof(server_addr));
server_addr.sin_family=PF_INET;
server_addr.sin_port=htons(PORT);
server_addr.sin_addr.s_addr=inet_addr("127.0.0.1");
int length=sizeof(server_addr);
int ret=connect(sockfd,(struct sockaddr *)&server_addr,length);
if(-1==ret)
{
perror("connect");
exit(1);
}
pthread_t tid;
ret=pthread_create(&tid,NULL,Recv,&sockfd);
if(-1==ret)
{
perror("pthread_create");
exit(1);
}
char buf[32]={0};
/*
FILE *fp;
fp=fopen(“hello.txt”,“w+”);
if(NULL==fp)
{
perror("fopen");
exit(1);
}
*/
int fd;
fd=open(“hello.txt”,O_WRONLY | O_CREAT, 00700);
if(-1==fd) //如果打开失败
{
perror(“open”);
exit(1);
}
while(1)
{
scanf("%s",buf);
sleep(1);
ret=send(sockfd,buf,strlen(buf),0);
if(-1==ret)
{
perror("send");
}
/*
ret=fwrite(buf,1,sizeof(buf),fp);
if(ret==-1)
{
perror(“fwrite”);
exit(1);
}
*/
ret=write(fd,buf,strlen(buf));
if(-1==ret)
{
perror("write");
exit(1);
}
if(strcmp(buf,"bye")==0)
{
close(fd);
break;
}
memset(buf,0,32);
}
sleep(2);
return 0;
}
运行结果图