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

[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.