I just write a simple client, and try to connect to a server which NOT
running.
libevent will trigger eventcb too times, the first time is BEV_EVENT_ERROR
and the second time is BEV_EVENT_CONNECTED. Is there some wrong with me?
The code like following:
static void eventcb(struct bufferevent *bev, short events, void *ptr)
{
if (events & BEV_EVENT_CONNECTED) {
printf("Connected\n");
//login(bev);
} else if (events & BEV_EVENT_ERROR) {
printf("NOT Connected\n");
}
}
static void readcb(struct bufferevent *bev, void *ctx)
{
}
int main(int argc, char **argv)
{
if (argc != 3) {
printf("Usage: client <host> <port>\n");
return 1;
}
const char *host = argv[1];
int port = atoi(argv[2]);
struct event_base *base = event_base_new();
if (!base) {
printf("Couldn't open event base\n");
return 1;
}
struct bufferevent *bev = bufferevent_socket_new(base, -1,
BEV_OPT_CLOSE_ON_FREE);
bufferevent_setcb(bev, readcb, NULL, eventcb, NULL);
bufferevent_enable(bev, EV_READ|EV_WRITE);
printf("Connecting to %s:%d\n", host, port);
if(bufferevent_socket_connect_hostname(bev, NULL, AF_INET, host, port)
!= 0)
{
printf("Connect host failed\n");
return 1;
}
event_base_dispatch(base);
bufferevent_free(bev);
event_base_free(base);
return 0;
}
***********************************************************************
unsubscribe libevent-users in the body.