[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [Libevent-users] Connect to server failed, libevent return BEV_EVENT_ERROR event and BEV_EVENT_CONNECTED event?



As nobody answer my question for a long time, 
I try to solve the problem by adding a 
event_base_loopbreak(base);
When BEV_EVENT_ERROR occurred. 

Is it really the normal solution for such kind of problem?

在 12-4-14 上午11:23, "Dawen Rie" <dawenhing@xxxxxxx> 写入:

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;
}



***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users    in the body.