服务端,收到客户端的消息转给另一个线程进行转发
#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,N