使用libevent启动server
struct sockaddr_in addr;
if (!StrToAddress(m_addr, m_port, &addr))
return -1;
m_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
if (evutil_make_socket_nonblocking(m_fd) != 0)
return -1;
if (evutil_make_listen_socket_reuseable(m_fd) != 0)
return -1;
if (bind(m_fd, (struct sockaddr*)&addr, sizeof(addr)) != 0)
return -1;
EventLooperImp &evLooper = (EventLooperImp&)EventLooper::GetInstance();
m_evBase = evLooper.CurrentEvBase();
m_connectEvent = event_new(m_evBase, m_fd, EV_READ | EV_PERSIST, on_accept, (void*)this);
event_add(m_connectEvent, NULL);
return listen(m_fd, 5); |