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

[Libevent-users] TCP client not calling writecb



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);
  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!");
    } else if(events & BEV_EVENT_ERROR) {
      /* An error occured while connecting. */
      puts("ERROR!");
    }
  }
  --------------------------------------------------------

   ..and I have:
  --------------------------------------------------------
  void writecb(struct bufferevent *bev, void *ctx)
  {
    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);
      puts("Disabling write");
      bufferevent_disable(bev, EV_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.