[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[Libevent-users] Connect to server failed, libevent return BEV_EVENT_ERROR event and BEV_EVENT_CONNECTED event?
- To: <libevent-users@xxxxxxxxxxxxx>
- Subject: [Libevent-users] Connect to server failed, libevent return BEV_EVENT_ERROR event and BEV_EVENT_CONNECTED event?
- From: Dawen Rie <dawenhing@xxxxxxx>
- Date: Sat, 14 Apr 2012 11:23:37 +0800
- Delivered-to: archiver@xxxxxxxx
- Delivered-to: libevent-users-outgoing@xxxxxxxx
- Delivered-to: libevent-users@xxxxxxxx
- Delivery-date: Fri, 13 Apr 2012 23:54:15 -0400
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=126.com; s=s110527; h=Received:User-Agent:Date:Subject:From:To:Message-ID: Thread-Topic:Mime-version:Content-type: Content-transfer-encoding; bh=xG7QQCx03BIzUfwJltbpQC1CznxI+hwgxH iQLM62WF4=; b=AaCpA8ZVz+Um/4/rIVazkx6C6OYqfk8XMAQ0A/mSBPiQWA9AYz c/APUS5PxCBjGyUzHmRoAtxaoTVH4KTsf0YMQ8qIbX7R5PziTFLvtUl2iSCKTbTX 9wuvJJRSytthBgg9sJZbbUhcig3vW5Vk00jjBDlpphXTX/MqUvH+QHrhI=
- Reply-to: libevent-users@xxxxxxxxxxxxx
- Sender: owner-libevent-users@xxxxxxxxxxxxx
- Thread-topic: Connect to server failed, libevent return BEV_EVENT_ERROR event and BEV_EVENT_CONNECTED event?
- User-agent: Microsoft-MacOutlook/14.10.0.110310
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.