Linux(ubuntu)安装libevent

步骤如下

第一步下载压缩包文件  ----- libevent-2.1.8-stable.tar.gz:   地址:https://github.com/libevent/libevent/releases/download/release-2.1.8-stable/libevent-2.1.8-stable.tar.gz

或者
链接:https://pan.baidu.com/s/1XdzHZfRGS45R3rvC67Puvw?pwd=wldk 
提取码:wldk 
--来自百度网盘超级会员V2的分享

|------------------------------------------------------------------------------------------------------------------------------|
第二步解压压缩包文件:   tar -zxvf libevent-2.1.8-stable.tar.gz 


第三步创建文件夹 :  mkdir   /usr/lib/libevent

第四步进入libevent-2.1.8文件夹:   cd   libevent-2.1.8-stable 

第五步配置文件:  ./configure -prefix=/usr/lib/libevent

第六步编译:  make

第七步安装:make install

第八步安装: sudo apt-get install libevent-dev
|--------------------------------------------------------------------------------------------------------------------------------|
第九步骤测试 -----创建一个demo.cpp文件---编译是否成功

//-----------------------------------代码---------------------------------------------------|
#include 
  
#include 

struct event ev;
struct timeval tv;

using namespace std;

void time_cb(int fd, short event, void *argc)
{
    cout << "timer wakeup" << endl;
    event_add(&ev, &tv);
}

int main()
{
    struct event_base *base = event_init();

    tv.tv_sec = 10;
    tv.tv_usec = 0;
    evtimer_set(&ev, time_cb, NULL);
    event_add(&ev, &tv);
    event_base_dispatch(base);

    return 0;
}
//----------------------------------------------------------------------|

编译:g++  nn.cpp -o nn -levent



#include 
#include
#include
#include 
#include
#include 
#include 
#include 						
#include 

#include 
#include 	
#include
#include //stl
using namespace std;
//|----------------------------线程------------------------------------|
/*
   void * printP(void* arg)
   {

// system("./start.sh");
return 0;
}

pthread_t m_pthread;
pthread_create(&m_pthread,NULL,printP,NULL);

*/


struct  khdxx
{
	char gwip[INET_ADDRSTRLEN];
	int sk;
};

class fwq
{

	private:
		int sk;//套接字描述符

		int bdkhd;//绑定ip端口返回成功与失败
		int ljtjz;//监听客户端套接字成功与失败
		int khdsk;//提取客户端套接字描述符号成公与失败
		char cli_ip[INET_ADDRSTRLEN];//客户端ip
		char cli_dk[INET_ADDRSTRLEN];//客户端口号
		khdxx aa[60];//客户端信息
		int bb;

	public:
		int socket_o(void);//获取套接字ipv4第一个是参数ipv4第二个参数是面向字节流第三个参数是零就可以了返回值为套接字描述符出错返回-1
		int bind_o( void );//绑定客户端参数为套接字描述符成功返回0失败返回-1
		int listen_o(void);//监听为客户端第一个参数为套接字描述符,第二个为等待的多少,成功返回0,失败返回-1
		int accept_o(int aa); //提取客户端
		void  duxie_o(void);//读写客户端
};




//|--------------------------------------------------------------获取套接字--------------------------------------------------|
int fwq::socket_o (void)
{

	this->bb=0;
	this->sk=socket (AF_INET,SOCK_STREAM,0);
	return this->sk;
}


//|---------------------------------------------------------------绑定客户端--------------------------------------------------|
int fwq::bind_o( void)
{

	struct sockaddr_in my_addr;
	bzero(&my_addr, sizeof(my_addr));
	unsigned short port = 8080;	// 本地端口	
	my_addr.sin_family = AF_INET;	// ipv4
	my_addr.sin_port = htons(port); // 端口
	my_addr.sin_addr.s_addr = htonl(INADDR_ANY); // ip,INADDR_ANY为通配地址其值为0
	this->bdkhd = bind(this->sk, (struct sockaddr*)&my_addr, sizeof(my_addr));


	return this->bdkhd;
}
//|------------------------------------------------------------监听客户端连接-----------------------------------------------|
int fwq::listen_o(void)
{
	this->ljtjz=listen(this->sk, 124);
	return this->ljtjz;
}
//|-----------------------------------------------------------提取客户端了解------------------------------------------------|
int fwq::accept_o(int aa)
{

	struct sockaddr_in client_addr;		   
	//char cli_ip[INET_ADDRSTRLEN] = "";	   
	socklen_t cliaddr_len = sizeof(client_addr);    
	this->khdsk=accept(aa,(struct sockaddr *)&client_addr,&cliaddr_len);
	inet_ntop(AF_INET, &client_addr.sin_addr,this-> cli_ip, INET_ADDRSTRLEN);
	inet_ntop(AF_INET, &client_addr.sin_addr,this-> aa[bb].gwip, INET_ADDRSTRLEN);
	this->aa[bb].sk=this->khdsk;		
	printf("%s\n", this->aa[bb].gwip);
	printf("%d\n",this->aa[bb].sk);
	bb++;
	//this->
	return this->khdsk;
}
//|--------------------------------------------------------------读写-------------------------------------------------------|
void fwq::duxie_o(void)
{
	int fd= this->khdsk;
	char buf[1024]="";
	read(fd,buf,sizeof(buf));
	printf("%s\n",buf);
}


fwq  hh;

int rr;

struct  jjss
{

	struct event *ev;
	int ks;

};


std::vector myvector;//存储了上线数描述符和文件描述符的结构体数组

struct event *fhxs(int sk)//返回需要下线树的描述符
{
	
         struct event *ev;

        for (auto it = myvector.crbegin(); it != myvector.crend(); ++it)
	{
              
              if( it->ks==sk)
              {
                  ev=it->ev;
                 	  
                  break;
	      }
                
	}
      
        return ev;
}

void xiaxiang(int cfd)//下线数据吗
{
int ztys=myvector.size();

cout<<"当前数组的元素个数:"<::iterator iter=myvector.begin(); iter!=myvector.end(); )
{
     if( iter->ks == cfd)
          iter = myvector.erase(iter);
      else
          iter ++ ;
}    

}
void cfdcb(int cfd,short event,void *arg)//监听客户发送消息以及下线
{
	char buf[1500]="";
	int n=read(cfd,buf,sizeof(buf));

	if(n<=0)
	{
		perror("err or clise\n");
		event_del(fhxs(cfd));//把树描述符下线
		xiaxiang(cfd);//取出结构体元素
		close(cfd);
		cout<<"描述符号->"<"<

你可能感兴趣的:(ll,linux,网络)