[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[Libevent-users] TCP client not calling writecb
- To: libevent-users@xxxxxxxxxxxxx
- Subject: [Libevent-users] TCP client not calling writecb
- From: Jan Danielsson <jan.m.danielsson@xxxxxxxxx>
- Date: Sun, 21 Apr 2013 00:28:24 +0200
- Delivered-to: archiver@xxxxxxxx
- Delivered-to: libevent-users-outgoing@xxxxxxxx
- Delivered-to: libevent-users@xxxxxxxx
- Delivery-date: Sat, 20 Apr 2013 18:28:36 -0400
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:message-id:date:from:organization:user-agent :mime-version:to:subject:x-enigmail-version:openpgp:content-type :content-transfer-encoding; bh=JXgZWeXBKEBduylp/9DwQpxGRURPqPSsJJWrELYkC/I=; b=kcHVjeAPYctCtiNP6QG2gh9SPDaNMgHEaFNvqpBlu/u71HumiOWxL4EsSsu1f3G5AT 0U1SjjMKi168J2UtSVzjpwXOQunsNgChqFFVm2ri9LJqQS1MFp6kUvtQw3//xxnQY2Qn UISZ7KrHaoHQqo7Et516uiveamhZa/Lh9bVRVYr61okESQsmvKSVf7M9eBAu9ICj8joa Kumf4nuqtx9YC8FU+lNwnBnY8/2pnF0I5shh90lxxsFHrWoREoSBqjKwJHDTggC0m+nz tKkwInNP5CsWRDdz7SiTfHDkZuZSlZZr8aM22Ur/KgXQaflldHB29tRAbNGkR6bjV6TP LFLg==
- Openpgp: id=13D7A8ED
- Organization: La Cosa Nostra
- Reply-to: libevent-users@xxxxxxxxxxxxx
- Sender: owner-libevent-users@xxxxxxxxxxxxx
- User-agent: Mozilla/5.0 (X11; NetBSD amd64; rv:17.0) Gecko/20130413 Thunderbird/17.0.5
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.