[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
RE: [Libevent-users] Multiple tcp client connections in single thread with using libevent
That looks like the basic outline but for a single thread I would suggest
building the buffer events with BEV_OPT_DEFER_CALLBACKS so the callbacks are
routed into the main thread. Otherwise the callbacks can be called from the
worker threads directly and you could have a lot of threading fun to deal
with.
KB
> -----Original Message-----
> From: owner-libevent-users@xxxxxxxxxxxxx [mailto:owner-libevent-
> users@xxxxxxxxxxxxx] On Behalf Of ravone
> Sent: Thursday, April 14, 2011 2:33 AM
> To: libevent-users@xxxxxxxx
> Subject: [Libevent-users] Multiple tcp client connections in single thread
> with using libevent
>
> 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.
***********************************************************************
To unsubscribe, send an e-mail to majordomo@xxxxxxxxxxxxx with
unsubscribe libevent-users in the body.