网络编程- 客户端(select) 服务端 epoll

xiaoxiao2021-02-28  9

服务端,收到客户端的消息转给另一个线程进行转发

#include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h> #include<stdlib.h> #include<string.h> #include<string> #include<sys/epoll.h> #include<map> #include<typeinfo> #include<iostream> #include<algorithm> #include<queue> #include<map> using namespace std; #define BUFFER_SIZE 40 #define MAX_EVENTS 1234 #define DEFAULT_PORT 1231 queue<char *>q; map<int ,int>flag_fd; map<int ,int>fd_flag; void *send_queue_to_client(void *) { while(1) { while(!q.empty()) { char *s=q.front(); printf("------thread running now------ %s将要发送给%c\n",s,s[0]); int go=s[0]-'0'; go=flag_fd[go]; if(go==0) { printf("未指明要发送的对象,不做处理,直接舍弃\n"); } else { int flag_send=send(go,s,strlen(s),0); if(flag_send<0) printf("send失败"); printf("发送%s到%c成功\n",s,s[0]); } delete []s; q.pop(); sleep(3); } sleep(10); } } void thread_create(pthread_t send_buff_to_client ) { send_buff_to_client=pthread_create(&send_buff_to_client,NULL,send_queue_to_client,NULL); if(send_buff_to_client==0) printf("进程创建成功\n"); else printf("进程创建失败\n"); } void thread_wait(pthread_t send_buff_to_client) { if(send_buff_to_client!=0) { pthread_join(send_buff_to_client,NULL); printf("该线程已经结束\n"); } } int main(int argc ,char** argv ) { pthread_t send_buff_to_client; thread_create(send_buff_to_client);/create int server_sockfd,client_sockfd; //char buff[BUFFER_SIZE]; int len; struct sockaddr_in my_addr,remote_addr; socklen_t sin_size; memset(&my_addr,0,sizeof(my_addr)); my_addr.sin_family=AF_INET; my_addr.sin_addr.s_addr=INADDR_ANY; my_addr.sin_port=htons(DEFAULT_PORT); server_sockfd=socket(AF_INET,SOCK_STREAM,0);//socket if(server_sockfd<0) { perror("socket\n"); return 1; } unsigned int value = 1;//设置端口复用 setsockopt(server_sockfd,SOL_SOCKET,SO_REUSEADDR,(void *)&value,sizeof(value)); if(bind(server_sockfd,(struct sockaddr *)&my_addr,sizeof(struct sockaddr))<0)//bind { perror("bind\n"); return 1; } printf("----等待客户端请求中-----\n"); listen(server_sockfd,55555);//listen sin_size=sizeof(struct sockaddr_in); int epoll_fd; epoll_fd=epoll_create(MAX_EVENTS);//epoll_create if(epoll_fd==-1) { perror("epoll_create failed\n"); exit(EXIT_FAILURE); } struct epoll_event ev; struct epoll_event events[MAX_EVENTS]; ev.events=EPOLLIN|EPOLLET; ev.data.fd=server_sockfd; if(epoll_ctl(epoll_fd,EPOLL_CTL_ADD,server_sockfd,&ev)==-1) { perror("注册失败\n"); exit(EXIT_FAILURE); } int flag=1; int fds; while(1) { fds=epoll_wait(epoll_fd,events,MAX_EVENTS,-1); if(fds==-1) { perror("启动epoll等待失败\n"); exit(EXIT_FAILURE); } int i; for( i=0; i<fds; i++) { if(events[i].data.fd==server_sockfd) { //printf("events[i].data.fd:%d\n",events[i].data.fd); client_sockfd=accept(server_sockfd,(struct sockaddr *)&remote_addr,&sin_size); if(client_sockfd<0) { perror("客户端接收失败\n"); exit(EXIT_FAILURE); } ev.events=EPOLLIN; ev.data.fd=client_sockfd; flag_fd[flag]=client_sockfd; fd_flag[client_sockfd]=flag; flag++; if(epoll_ctl(epoll_fd,EPOLL_CTL_ADD,client_sockfd,&ev)==-1) { perror("客户端epoll注册失败\n"); exit(EXIT_FAILURE); } send(client_sockfd,inet_ntoa(remote_addr.sin_addr),sizeof(inet_ntoa(remote_addr.sin_addr))+1,0); sleep(2); send(client_sockfd,p,sizeof(int),0); printf("来自--%s-%d-的客户端\n",inet_ntoa(remote_addr.sin_addr),remote_addr.sin_port); } else { printf("events[i].data.fd:%d\n",events[i].data.fd); printf("已收到消息,准备接收------\n"); char *buff=new char[100]; len=recv(events[i].data.fd,buff,BUFFER_SIZE,0); if(len<0) { perror("接收数据失败\n"); exit(EXIT_FAILURE); } printf("收到来自客户端的数据书:%s\n",buff); q.push(buff); //send(events[i].data.fd,"successful",12,0); //printf("已向客户端发送确认消息:%s\n","successful"); printf("\n"); } } } thread_wait(send_buff_to_client); return 0; } 客户端:select的收发模式

#include<stdio.h> #include<pthread.h> #include<netinet/in.h> #include <pthread.h> #include <sys/poll.h> #include<stdlib.h> #include<string.h> #include<strings.h> #include<sys/types.h> #include<sys/socket.h> int main(int argc,char **argv) { int socketfd,ret; fd_set fd; FD_ZERO(&fd); FD_SET(0,&fd); socketfd = socket(AF_INET,SOCK_STREAM,0); FD_SET(socketfd,&fd); ret = select(socketfd+1,&fd,NULL,NULL,NULL); struct sockaddr_in saddr; memset(&saddr,0,sizeof(saddr)); saddr.sin_family = PF_INET; saddr.sin_port = htons(1231); saddr.sin_addr.s_addr = INADDR_ANY; if(connect(socketfd,(struct sockaddr *)&saddr,sizeof(struct sockaddr))<0) perror("connect() error!\n");//只要与服务器连上立马将自己的昵称发送过去 //send(socketfd,argv[0],strlen(argv[0]),0); char buf[50],buf1[50]; while(1) { bzero(buf,50); bzero(buf1,50); FD_SET(0,&fd); FD_SET(socketfd,&fd); ret = select(socketfd+1,&fd,NULL,NULL,NULL); if(FD_ISSET(socketfd,&fd)) { printf("有数据发来,准备接收:"); if(recv(socketfd,buf1,sizeof(buf1),0)<0) perror("recv() error!\n"); printf("接收成功,收到的是:%s\n",buf1); printf("暂时没有数据发来,你可以尝试输入你想发往服务端的数据:\n"); continue; } if(FD_ISSET(0,&fd)) { scanf("%s",buf); if(send(socketfd,buf,strlen(buf),0)<0) perror("send() error!\n"); continue; } } }

转载请注明原文地址: https://www.6miu.com/read-1950330.html

最新回复(0)