no message
parent
dc0632ae59
commit
e7d1f310b7
|
@ -1,5 +1,38 @@
|
|||
#include <iostream>
|
||||
#include "tcp_server_libevent.h"
|
||||
#include "winsock2.h"
|
||||
#include <stdio.h>
|
||||
|
||||
|
||||
int main(){
|
||||
std::cout<<"hello world\r\n";
|
||||
WORD wdVersion = MAKEWORD(2, 2);//定义自己需要的网络库版本,这里是2.2
|
||||
WSADATA wdSockMsg;//这是一个结构体
|
||||
int nRes = WSAStartup(wdVersion, &wdSockMsg);//打开一个套接字
|
||||
if (0 != nRes)
|
||||
{
|
||||
switch (nRes)
|
||||
{
|
||||
case WSASYSNOTREADY:
|
||||
printf("check your library");
|
||||
break;
|
||||
case WSAVERNOTSUPPORTED:
|
||||
printf("need updated");
|
||||
break;
|
||||
case WSAEINPROGRESS:
|
||||
printf("need reboot");
|
||||
break;
|
||||
case WSAEPROCLIM:
|
||||
printf("sdfsdfsa");
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (2 != HIBYTE(wdSockMsg.wVersion) || 2 != LOBYTE(wdSockMsg.wVersion))
|
||||
{
|
||||
printf("");
|
||||
WSACleanup();
|
||||
return 0;
|
||||
}
|
||||
std::cout<<"start server libevent\r\n";
|
||||
test_tcp_server();
|
||||
|
||||
}
|
|
@ -12,19 +12,16 @@ void read_cb(struct bufferevent *bev, void *arg)
|
|||
char* ip = (char*)arg;
|
||||
|
||||
bufferevent_read(bev, buf, sizeof(buf));
|
||||
|
||||
cout << "client " << ip << " say:" << buf << endl;
|
||||
|
||||
|
||||
//写数据给客户端
|
||||
const char *p = "i am server, i received your msg!" ;
|
||||
bufferevent_write(bev, p, strlen(p)+1);
|
||||
bufferevent_write(bev,p,strlen(p) + 1);
|
||||
}
|
||||
/************************************
|
||||
@ Brief: 写缓冲区回调
|
||||
@ Author: woniu201
|
||||
@ Created: 2018/11/21
|
||||
@ Return:
|
||||
@ Return:
|
||||
************************************/
|
||||
void write_cb(struct bufferevent *bev, void *arg)
|
||||
{
|
||||
|
@ -61,16 +58,48 @@ void cb_listener(struct evconnlistener *listener, evutil_socket_t fd, struct soc
|
|||
{
|
||||
struct sockaddr_in* client = (sockaddr_in*)addr ;
|
||||
cout << "connect new client: " << inet_ntoa(client->sin_addr) << "::"<< ntohs(client->sin_port)<< endl;
|
||||
|
||||
struct event_base *base = (struct event_base*)ptr;
|
||||
|
||||
//添加新事件
|
||||
struct bufferevent *bev;
|
||||
bev = bufferevent_socket_new(base, fd, BEV_OPT_CLOSE_ON_FREE);
|
||||
|
||||
//给bufferevent缓冲区设置回调
|
||||
bufferevent_setcb(bev, read_cb, write_cb, event_cb, inet_ntoa(client->sin_addr));
|
||||
|
||||
//启动 bufferevent的 读缓冲区。默认是disable 的
|
||||
bufferevent_enable(bev, EV_READ);
|
||||
}
|
||||
|
||||
int test_tcp_server()
|
||||
{
|
||||
#ifdef WIN32
|
||||
WORD wVersionRequested;
|
||||
WSADATA wsaData;
|
||||
wVersionRequested = MAKEWORD(2, 2);
|
||||
(void)WSAStartup(wVersionRequested, &wsaData);
|
||||
#endif
|
||||
// init server
|
||||
struct sockaddr_in serv;
|
||||
|
||||
memset(&serv, 0, sizeof(serv));
|
||||
serv.sin_family = AF_INET;
|
||||
serv.sin_port = htons(8888);
|
||||
serv.sin_addr.s_addr = htonl(INADDR_ANY);
|
||||
|
||||
// 创建 event_base
|
||||
struct event_base * base;
|
||||
base = event_base_new();
|
||||
|
||||
// 创建套接字
|
||||
// 绑定
|
||||
// 接收连接请求
|
||||
struct evconnlistener* listener;
|
||||
listener = evconnlistener_new_bind(base, cb_listener, base, LEV_OPT_CLOSE_ON_FREE|LEV_OPT_REUSEABLE, 36, (struct sockaddr*)&serv, sizeof(serv));
|
||||
if(NULL != listener){
|
||||
// 启动循环监听
|
||||
event_base_dispatch(base);
|
||||
evconnlistener_free(listener);
|
||||
event_base_free(base);
|
||||
return 0;
|
||||
}else{
|
||||
return -1;
|
||||
}
|
||||
}
|
|
@ -20,9 +20,11 @@ extern "C"{
|
|||
#include "event2/thread.h"
|
||||
};
|
||||
#include <iostream>
|
||||
// #include "PackageReceiver.h"
|
||||
#include <mutex>
|
||||
#include <thread>
|
||||
using namespace std;
|
||||
|
||||
|
||||
int test_tcp_server();
|
||||
|
||||
#endif
|
Loading…
Reference in New Issue