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

Re: [Libevent-users] TCP client not calling writecb



On Apr 21, 2013, at 1:28 AM, Jan Danielsson wrote:

> Hello,
> 
>   I have a server which waits for incoming connections, and I want the
> client to connect to the server, and immediately send over a port
> number. In the client's main() I do this:
> 
>  --------------------------------------------------------
>  (void)memset(&sin, 0, sizeof(sin));
>  sin.sin_family = AF_INET;
>  sin.sin_addr.s_addr = htonl(0x7f000001);
>  sin.sin_port = htons(server_tcp_port);
> 
>  (void)memset(&ls, 0x00, sizeof(ls));
> 
>  bev = bufferevent_socket_new(base, -1, BEV_OPT_CLOSE_ON_FREE);
>  bufferevent_setcb(bev, readcb, writecb, eventcb, &ls);

You don't need to register the writecb - use NULL

>  if(bufferevent_socket_connect(bev, (struct sockaddr*)&sin,
>        sizeof(sin)) < 0) {
>    /* Error starting connection */
>    bufferevent_free(bev);
>    return 1;
>  }
>  bufferevent_enable(bev, EV_READ|EV_WRITE);
>  --------------------------------------------------------
> 
>   The eventcb looks like this:
>  --------------------------------------------------------
>  void eventcb(struct bufferevent *bev, short events, void *ptr)
>  {
>    if(events & BEV_EVENT_CONNECTED) {
>      puts("Connected!");

	send_port(bev);

>    } else if(events & BEV_EVENT_ERROR) {
>      /* An error occured while connecting. */
>      puts("ERROR!");
>    }
>  }
>  --------------------------------------------------------
> 
>   ..and I have:
>  --------------------------------------------------------
>  void writecb(struct bufferevent *bev, void *ctx)

Rename to send_port(struct bufferevent *bev)

>  {
>    linkstate_t *ls = ctx;
>    printf("Write!\n");
> 
>    if(ls->sent_port == 0) {
>      unsigned short nport = htons(tcp_listen_port);
>      bufferevent_write(bev, &nport, sizeof(nport));
>      bufferevent_flush(bev, EV_WRITE, BEV_FLUSH);

You don't need to flush

>      puts("Disabling write");
>      bufferevent_disable(bev, EV_WRITE);

You don't need to disable write - if the bufferevent is empty, it will not try to write.

> 
>      ls->sent_port = 1;
>    }
>  }
> 
>  void readcb(struct bufferevent *bev, void *ctx)
>  {
>    linkstate_t *ls = ctx;
> 
>    printf("Read!\n");
>  }
>  --------------------------------------------------------
> 
>   The server reports that a client has connected to it, and the client
> does print "Connected!" in eventcb(). But the client never prints "Write!".
> 
>   Rather than use a writecb(), I tried calling bufferevent_write() just
> after the bufferevent_socket_connect(), but the data didn't reach the
> server, which I found to be slightly peculiar.
> 
>   It's clear I've misunderstood something pretty fundamental about
> bufferevents .. but what?
> 
> -- 
> Kind regards,
> Jan Danielsson
> 
> ***********************************************************************
> 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.