[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[Libevent-users] Multiple tcp client connections in single thread with using libevent
- To: libevent-users@xxxxxxxx
- Subject: [Libevent-users] Multiple tcp client connections in single thread with using libevent
- From: ravone <ravonereal@xxxxxxxxx>
- Date: Thu, 14 Apr 2011 11:33:17 +0400
- Delivered-to: archiver@xxxxxxxx
- Delivered-to: libevent-users-outgoing@xxxxxxxx
- Delivered-to: libevent-users@xxxxxxxx
- Delivery-date: Thu, 14 Apr 2011 03:33:23 -0400
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:date:message-id:subject:from:to :content-type; bh=yHkust7n8fnQvR8RvTBli5ZSHnivWCzRDqnl3r4YpiY=; b=X+jCxmlmvmcNfe1KmxeNHPhrx5HY76jHMVpyu/hwoa3nMeuCgogeeAj3h2wGjojtIH AUWkzC//J3RQEiCo2UVKCD+PEYupwEitgos/qFOrlF5JdBxdcXUwBdHdzXq4vI2H/X33 rUYxsxu5nsrh5+GscXjdMH390B3rdGpdMLsdI=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type; b=LSjUF28BApt1SLXJucHjYZGOoixbmnWrbu8cZWxfaNbnat3lQuvHeBV+5Mntpzj+pq VCauZE/mnBYGIIwAEoiUqvCEPv523YUilinPLeDYhiZGyLjZZzKQOMqYPtSN8X9hefIR ZP55ooBO4Bwkn5LWWKaVjZ0+DiwhXp30lnDvQ=
- Reply-to: libevent-users@xxxxxxxxxxxxx
- Sender: owner-libevent-users@xxxxxxxxxxxxx
Is it possible to use libevent for create multiple tcp connections to
different servers in one thread? Could you write a sample
implementation of such a task?
I have done so, but not sure that it's right:
...
int num_of_connect = 5; /*for example*/
struct event_base *evbase;
struct bufferevent *bev[num_of_connect];
struct sockaddr_in sin[num_of_connect];
evbase = event_base_new();
for(int i=0;i<=(num_of_connect-1);i++){
sin[i].sin_family = AF_INET;
sin[i].sin_addr.s_addr = inet_addr(/*some addr*/);
sin[i].sin_port = htons(/*some port*/);
bev[i] = bufferevent_socket_new(evbase, -1, BEV_OPT_CLOSE_ON_FREE);
bufferevent_setcb(bev[i], cb_evread, cb_evwrite, cb_event, NULL);
bufferevent_socket_connect(bev[i], (struct sockaddr *)&sin[i],
sizeof(struct sockaddr_in));
}
event_base_dispatch(evbase);
...
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users in the body.