服务器端:
#include
#include
#include
#include
#include
#include
#include
#define PORT 33333
int main()
{
int sockfd;
int cfd;
int c_len;
char buffer[1024];
struct sockaddr_in addr;
struct sockaddr_in c_addr;
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd==-1)
{
perror("socket create error!");
exit(1);
}
printf("socket success!\n");
int opt=1;
setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
bzero(&addr,sizeof(struct sockaddr_in));
addr.sin_family=AF_INET;
addr.sin_port=htons(PORT);//字节序转化
addr.sin_addr.s_addr=inet_addr("192.168.204.130");
if(bind(sockfd,(struct sockaddr *)(&addr),sizeof(struct sockaddr_in))<0)
{
perror("bind error!");
exit(1);
}
printf("bind success!\n");
if(listen(sockfd,3)<0)
{
perror("listen error!");
exit(1);
}
printf("listen success!\n");
while(1)
{
memset(buffer,0,sizeof(buffer));
bzero(&c_addr,sizeof(struct sockaddr_in));
c_len=sizeof(struct sockaddr_in);
printf("accepting................!\n");
if((cfd=accept(sockfd,(struct sockaddr *)(&c_addr),&c_len))<0)
{
perror("accept error!\n");
exit(1);
}
printf("accept success!\n");
printf("port=%d ip=%s\n",ntohs(c_addr.sin_port),inet_ntoa(c_addr.sin_addr));
//read(cfd,buffer,sizeof(buffer));
recv(cfd,buffer,sizeof(buffer),0);
printf("recv=%s\n",buffer);
usleep(2);
//write(cfd,buffer,strlen(buffer));
send(cfd,buffer,sizeof(buffer),0);
shutdown(cfd,SHUT_RDWR);
}
return 0;
}
客户端:
#include
#include
#include
#include
#include
#include
#define PORT 33333
int main()
{
int sockfd;
struct sockaddr_in s_addr;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
perror("socket error!");
exit(1);
}
printf("client socket success!\n");
bzero(&s_addr,sizeof(struct sockaddr_in));
s_addr.sin_family=AF_INET;
s_addr.sin_port=htons(PORT);
s_addr.sin_addr.s_addr=inet_addr("192.168.204.130");
if(connect(sockfd,(struct sockaddr *)(&s_addr),sizeof(struct sockaddr_in))<0)
{
perror("connect error!");
exit(1);
}
printf("connect success!\n");
write(sockfd,"hello world",12);
char buffer[1024];
read(sockfd,buffer,sizeof(buffer));
printf("recv server=:%s\n",buffer);
return 0;
}
运行结果:
close关闭文件的缺点:
1、读写一起关,不能分开关
2、如果多个文件描述符指向了同一个连接时,如果只close关闭了其中某个文件描述符时, 只要其它的fd还打开着,那么连接不会被断开,直到所有的描述符都被close后才断开连接。
server.c
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 5555
int main()
{
int sockfd;
struct sockaddr_in s_addr;
char buffer[1024];
int opt=1;
setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0)
{
perror("socket error!");
exit(1);
}
bzero(&s_addr,sizeof(struct sockaddr_in));
s_addr.sin_family=AF_INET;
s_addr.sin_port=htons(PORT);
s_addr.sin_addr.s_addr=inet_addr("192.168.204.130");
if(bind(sockfd,(struct sockaddr*)(&s_addr),sizeof(struct sockaddr_in))<0)
{
perror("bind error!\n");
exit(1);
}
while(1)
{
bzero(&s_addr,sizeof(struct sockaddr_in));
s_addr.sin_family=AF_INET;
s_addr.sin_port=htons(5554);
s_addr.sin_addr.s_addr=inet_addr("192.168.204.130");
memset(buffer,0,sizeof(buffer));
printf("please input send message:\n");
scanf("%s",buffer);
sendto(sockfd,buffer,strlen(buffer),0,(struct sockaddr *)(&s_addr),sizeof(struct sockaddr_in));
usleep(3);
}
return 0;
}
recv.c
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 5554
int main()
{
int sockfd;
int addr_len;
struct sockaddr_in s_addr;
char buffer[1024];
struct sockaddr_in c_addr;
int opt=1;
setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0)
{
perror("socket error!");
exit(1);
}
bzero(&s_addr,sizeof(struct sockaddr_in));
s_addr.sin_family=AF_INET;
s_addr.sin_port=htons(PORT);
s_addr.sin_addr.s_addr=inet_addr("192.168.204.130");
if(bind(sockfd,(struct sockaddr*)(&s_addr),sizeof(struct sockaddr_in))<0)
{
perror("bind error!\n");
exit(1);
}
while(1)
{
memset(buffer,0,sizeof(buffer));
addr_len=sizeof(struct sockaddr_in);
recvfrom(sockfd,buffer,sizeof(buffer),0,(struct sockaddr *)(&c_addr),&addr_len);
printf("port=%d ip=%s\n",ntohs(c_addr.sin_port),inet_ntoa(c_addr.sin_addr));
printf("recv=%s\n",buffer);
usleep(3);
}
return 0;
}
server.c
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 33333
void *recv_message(void *arg)
{
int ret;
int cfd=*((int*)arg);
char buffer[1024];
while (1)
{
memset(buffer,0,sizeof(buffer));
if((ret=recv(cfd,buffer,sizeof(buffer),0))<0)
{
perror("recv error!");
exit(1);
}
if(ret==0)
{
printf("%d is close!\n",cfd);
pthread_exit(NULL);
}
printf("recv=%s\n",buffer);
usleep(3);
}
pthread_exit(NULL);
}
void *send_message(void *arg)
{
int ret;
int cfd=*((int*)arg);
while (1)
{
if((ret=send(cfd,"hello world",12,0))<0)
{
perror("send error!");
exit(1);
}
if(ret==0)
{
printf("%d is close!\n",cfd);
pthread_exit(NULL);
}
sleep(1);
}
pthread_exit(NULL);
}
int main()
{
int sockfd;
int cfd;
int c_len;
char buffer[1024];
pthread_t id;
struct sockaddr_in addr;
struct sockaddr_in c_addr;
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd==-1)
{
perror("socket create error!");
exit(1);
}
printf("socket success!\n");
int opt=1;
setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
bzero(&addr,sizeof(struct sockaddr_in));
addr.sin_family=AF_INET;
addr.sin_port=htons(PORT);
addr.sin_addr.s_addr=inet_addr("192.168.204.130");
if(bind(sockfd,(struct sockaddr *)(&addr),sizeof(struct sockaddr_in))<0)
{
perror("bind error!");
exit(1);
}
printf("bind success!\n");
if(listen(sockfd,3)<0)
{
perror("listen error!");
exit(1);
}
printf("listen success!\n");
while(1)
{
memset(buffer,0,sizeof(buffer));
bzero(&c_addr,sizeof(struct sockaddr_in));
c_len=sizeof(struct sockaddr_in);
printf("accepting................!\n");
if((cfd=accept(sockfd,(struct sockaddr *)(&c_addr),&c_len))<0)
{
perror("accept error!\n");
exit(1);
}
printf("accept success!\n");
printf("port=%d ip=%s\n",ntohs(c_addr.sin_port),inet_ntoa(c_addr.sin_addr));
if( pthread_create(&id,NULL,recv_message,(void*)(&cfd))!=0)
{
perror("recv pthread create error!");
exit(1);
}
if( pthread_create(&id,NULL,send_message,(void*)(&cfd))!=0)
{
perror("send pthread create error!");
exit(1);
}
usleep(3);
}
return 0;
}
client.c
#include
#include
#include
#include
#include
#include
#include
#define PORT 33333
void *recv_message(void *arg)
{
int ret;
int cfd=*((int*)arg);
char buffer[1024];
while (1)
{
memset(buffer,0,sizeof(buffer));
if((ret=recv(cfd,buffer,sizeof(buffer),0))<0)
{
perror("recv error!");
exit(1);
}
if(ret==0)
{
printf("%d is close!\n",cfd);
pthread_exit(NULL);
}
printf("recv server=%s\n",buffer);
usleep(3);
}
pthread_exit(NULL);
}
int main()
{
int sockfd;
struct sockaddr_in s_addr;
pthread_t id;
char buffer[1024];
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
perror("socket error!");
exit(1);
}
printf("client socket success!\n");
bzero(&s_addr,sizeof(struct sockaddr_in));
s_addr.sin_family=AF_INET;
s_addr.sin_port=htons(PORT);
s_addr.sin_addr.s_addr=inet_addr("192.168.204.130");
if(connect(sockfd,(struct sockaddr *)(&s_addr),sizeof(struct sockaddr_in))<0)
{
perror("connect error!");
exit(1);
}
printf("connect success!\n");
if(pthread_create(&id,NULL,recv_message,(void *)(&sockfd))!=0)
{
perror("pthread create error!");
exit(1);
}
while(1)
{
memset(buffer,0,sizeof(buffer));
printf("please input send message:\n");
scanf("%s",buffer);
write(sockfd,buffer,sizeof(buffer));
usleep(3);
}
shutdown(sockfd,SHUT_RDWR);
return 0;
}
fork_server.c
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 33333
int main()
{
int sockfd;
int cfd;
int c_len;
pid_t pid;
char buffer[1024];
pthread_t id;
struct sockaddr_in addr;
struct sockaddr_in c_addr;
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd==-1)
{
perror("socket create error!");
exit(1);
}
printf("socket success!\n");
int opt=1;
setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
bzero(&addr,sizeof(struct sockaddr_in));
addr.sin_family=AF_INET;
addr.sin_port=htons(PORT);
addr.sin_addr.s_addr=inet_addr("192.168.204.130");
if(bind(sockfd,(struct sockaddr *)(&addr),sizeof(struct sockaddr_in))<0)
{
perror("bind error!");
exit(1);
}
printf("bind success!\n");
if(listen(sockfd,3)<0)
{
perror("listen error!");
exit(1);
}
printf("listen success!\n");
while(1)
{
memset(buffer,0,sizeof(buffer));
bzero(&c_addr,sizeof(struct sockaddr_in));
c_len=sizeof(struct sockaddr_in);
printf("accepting................!\n");
if((cfd=accept(sockfd,(struct sockaddr *)(&c_addr),&c_len))<0)
{
perror("accept error!\n");
exit(1);
}
printf("accept success!\n");
printf("port=%d ip=%s\n",ntohs(c_addr.sin_port),inet_ntoa(c_addr.sin_addr));
pid=fork();
if(pid>0)
{
close(cfd);
}
else if(pid==0)
{
pid=fork();
if(pid>0)
{
int ret;
char buffer[1024];
while (1)
{
memset(buffer,0,sizeof(buffer));
if((ret=recv(cfd,buffer,sizeof(buffer),0))<0)
{
perror("recv error!");
exit(1);
}
printf("recv=%s\n",buffer);
if(ret==0)
{
printf("%d is close!\n",cfd);
_exit(1);
}
usleep(3);
}
}
else if(pid==0)
{
int ret;
while (1)
{
if((ret=send(cfd,"hello world",12,0))<0)
{
perror("send error!");
exit(1);
}
if(ret==0)
{
printf("%d is close!\n",cfd);
_exit(1);
}
sleep(1);
}
}
}
usleep(3);
}
return 0;
}
对多进程进行封装优化
read_msg.c
#include
#include
#include
#include
#include
#include
int main(int argc,char *argv[])
{
int ret;
int cfd=atoi(argv[1]);
char buffer[1024];
while (1)
{
memset(buffer,0,sizeof(buffer));
if((ret=recv(cfd,buffer,sizeof(buffer),0))<0)
{
perror("recv error!");
exit(1);
}
if(ret==0)
{
printf("%d is close!\n",cfd);
_exit(1);
}
printf("recv=%s\n",buffer);
usleep(3);
}
}
write_msg.c
#include
#include
#include
#include
#include
#include
int main(int argc,char *argv[])
{
int ret;
int cfd=atoi(argv[1]);
while (1)
{
if((ret=send(cfd,"hello world",12,0))<0)
{
perror("send error!");
exit(1);
}
if(ret==0)
{
printf("%d is close!\n",cfd);
_exit(1);
}
sleep(1);
}
}
fork_server.c
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 33333
int main()
{
int sockfd;
int cfd;
int c_len;
pid_t pid;
char buffer[1024];
pthread_t id;
struct sockaddr_in addr;
struct sockaddr_in c_addr;
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd==-1)
{
perror("socket create error!");
exit(1);
}
printf("socket success!\n");
int opt=1;
setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
bzero(&addr,sizeof(struct sockaddr_in));
addr.sin_family=AF_INET;
addr.sin_port=htons(PORT);
addr.sin_addr.s_addr=inet_addr("192.168.204.130");
if(bind(sockfd,(struct sockaddr *)(&addr),sizeof(struct sockaddr_in))<0)
{
perror("bind error!");
exit(1);
}
printf("bind success!\n");
if(listen(sockfd,3)<0)
{
perror("listen error!");
exit(1);
}
printf("listen success!\n");
while(1)
{
memset(buffer,0,sizeof(buffer));
bzero(&c_addr,sizeof(struct sockaddr_in));
c_len=sizeof(struct sockaddr_in);
printf("accepting................!\n");
if((cfd=accept(sockfd,(struct sockaddr *)(&c_addr),&c_len))<0)
{
perror("accept error!\n");
exit(1);
}
printf("accept success!\n");
printf("port=%d ip=%s\n",ntohs(c_addr.sin_port),inet_ntoa(c_addr.sin_addr));
pid=fork();
if(pid>0)
{
close(cfd);
}
else if(pid==0)
{
char buf[1024];
pid=fork();
if(pid>0)
{
sprintf(buf,"%d",cfd);
execl("./read_msg","./read_msg",buf,NULL);
}
else if(pid==0)
{
sprintf(buf,"%d",cfd);
execl("./write_msg","./write_msg",buf,NULL);
}
}
usleep(3);
}
return 0;
}
server.c
#include
#include
#include
#include
#include
#include
#include
#include
#define PORT 33333
struct message
{
int action;
char toname[20];
char msg[1024];
};
struct online
{
int cfd;
char name[20];
struct online *next;
};
struct online *head=NULL;
void insert_user(struct online *new)//注册时加入新成员
{
if(head==NULL)
{
new->next =NULL;
head=new;
}
else
{
new->next=head->next;
head->next=new;
}
}
int find_cfd(char *toname)//私发时用于查找发送对象
{
if(head==NULL)
{
return -1;
}
struct online *temp=head;
while(temp!=NULL)
{
if(strcmp(temp->name,toname)==0)
{
return temp->cfd;
}
temp=temp->next;
}
return -1;
}
void *recv_message(void *arg)
{
int ret;
int cfd=*((int*)arg);
int to_cfd;
//char buffer[1024];
struct online *new;
struct message *msg=(struct message *)malloc(sizeof(struct message));
while (1)
{
memset(msg,0,sizeof(struct message));
if((ret=recv(cfd,msg,sizeof(struct message),0))<0)
{
perror("recv error!");
exit(1);
}
if(ret==0)
{
printf("%d is close!\n",cfd);
pthread_exit(NULL);
}
switch(msg->action)
{
case 1:
{
new=(struct online*)malloc(sizeof(struct online));
new->cfd=cfd;
strcpy(new->name,msg->msg);
insert_user(new);
msg->action=1;
send(cfd,msg,sizeof(struct message),0);
break;
}
case 2:
{
to_cfd=find_cfd(msg->toname);
msg->action=2;
send(to_cfd,msg,sizeof(struct message),0);
break;
}
case 3:
{
struct online *temp=head;
while(temp!=NULL)
{
to_cfd=temp->cfd;
msg->action=3;
send(to_cfd,msg,sizeof(struct message),0);
temp=temp->next;
}
break;
}
}
usleep(3);
}
pthread_exit(NULL);
}
void *send_message(void *arg)
{
int ret;
int cfd=*((int*)arg);
while (1)
{
if((ret=send(cfd,"hello world",12,0))<0)
{
perror("send error!");
exit(1);
}
if(ret==0)
{
printf("%d is close!\n",cfd);
pthread_exit(NULL);
}
sleep(1);
}
pthread_exit(NULL);
}
int main()
{
int sockfd;
int cfd;
int c_len;
char buffer[1024];
pthread_t id;
struct sockaddr_in addr;
struct sockaddr_in c_addr;
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd==-1)
{
perror("socket create error!");
exit(1);
}
printf("socket success!\n");
int opt=1;
setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR,&opt,sizeof(opt));
bzero(&addr,sizeof(struct sockaddr_in));
addr.sin_family=AF_INET;
addr.sin_port=htons(PORT);//字节序转换
addr.sin_addr.s_addr=inet_addr("192.168.204.130");//IP地址格式转化
if(bind(sockfd,(struct sockaddr *)(&addr),sizeof(struct sockaddr_in))<0)
{
perror("bind error!");
exit(1);
}
printf("bind success!\n");
if(listen(sockfd,3)<0)
{
perror("listen error!");
exit(1);
}
printf("listen success!\n");
while(1)
{
memset(buffer,0,sizeof(buffer));
bzero(&c_addr,sizeof(struct sockaddr_in));
c_len=sizeof(struct sockaddr_in);
printf("accepting................!\n");
if((cfd=accept(sockfd,(struct sockaddr *)(&c_addr),&c_len))<0)
{
perror("accept error!\n");
exit(1);
}
printf("accept success!\n");
printf("port=%d ip=%s\n",ntohs(c_addr.sin_port),inet_ntoa(c_addr.sin_addr));
if( pthread_create(&id,NULL,recv_message,(void*)(&cfd))!=0)
{
perror("recv pthread create error!");
exit(1);
}
/*
if( pthread_create(&id,NULL,send_message,(void*)(&cfd))!=0)
{
perror("send pthread create error!");
exit(1);
}*/
usleep(3);
}
shutdown(cfd,SHUT_RDWR);
return 0;
}
client.c
#include
#include
#include
#include
#include
#include
#include
#define PORT 33333
struct message
{
int action;
char toname[20];
char msg[1024];
};
void *recv_message(void *arg)
{
int ret;
int cfd=*((int*)arg);
struct message *msg=(struct message *)malloc(sizeof(struct message));
while (1)
{
memset(msg,0,sizeof(struct message));
if((ret=recv(cfd,msg,sizeof(struct message),0))<0)
{
perror("recv error!");
exit(1);
}
if(ret==0)
{
printf("%d is close!\n",cfd);
pthread_exit(NULL);
}
switch(msg->action)
{
case 1:
{
printf("reg success!\n");
break;
}
case 2:
{
printf("recv:%s\n",msg->msg);
break;
}
case 3:
{
printf("all recv:%s\n",msg->msg);
break;
}
}
usleep(3);
}
pthread_exit(NULL);
}
int main()
{
int sockfd;
struct sockaddr_in s_addr;
pthread_t id;
char buffer[1024];
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
perror("socket error!");
exit(1);
}
printf("client socket success!\n");
bzero(&s_addr,sizeof(struct sockaddr_in));
s_addr.sin_family=AF_INET;
s_addr.sin_port=htons(PORT);
s_addr.sin_addr.s_addr=inet_addr("192.168.204.130");
if(connect(sockfd,(struct sockaddr *)(&s_addr),sizeof(struct sockaddr_in))<0)
{
perror("connect error!");
exit(1);
}
printf("connect success!\n");
if(pthread_create(&id,NULL,recv_message,(void *)(&sockfd))!=0)
{
perror("pthread create error!");
exit(1);
}
printf("cmd: reg send all!\n...");
char cmd[20];
char name[20];
char toname[20];
char message[1024];
struct message *msg =(struct message *)malloc(sizeof(struct message));
while(1)
{
printf("Please input cmd:\n");
scanf("%s",cmd);
if(strcmp(cmd,"reg")==0)
{
printf("Please input reg name:\n");
scanf("%s",name);
msg->action=1;
strcpy(msg->msg,name);
if(send(sockfd,msg,sizeof(struct message),0)<0)
{
perror("send error reg!");
exit(1);
}
}
if(strcmp(cmd,"send")==0)
{
printf("please input send to name:\n");
scanf("%s",toname);
printf("Please input send message:\n");
scanf("%s",message);
msg->action=2;
strcpy(msg->toname,toname);
strcpy(msg->msg,message);
send(sockfd,msg,sizeof(struct message),0);
}
if(strcmp(cmd,"all")==0)
{
printf("Please input all message:\n");
scanf("%s",message);
msg->action=3;
strcpy(msg->msg,message);
send(sockfd,msg,sizeof(struct message),0);
}
}
shutdown(sockfd,SHUT_RDWR);
return 0;
}