I implement echo server given in the libevent book.
I modify accept_conn_cb
function so that the server prints the IPv4 address of a newly created connection (in decimal dot notation). The following is my callback
static void accept_conn_cb(
struct evconnlistener *listener,
evutil_socket_t fd,
struct sockaddr *address,
int socklen,
void *ctx)
{
char ipAddress[INET_ADDRSTRLEN];
struct sockaddr_in * saddr_in = (struct sockaddr_in *) &address;
if (!inet_ntop(AF_INET, &(saddr_in->sin_addr), ipAddress, INET_ADDRSTRLEN))
puts("Couldn't retrieve IPv4 address");
printf("A new connection established from %s\n", ipAddress);
/* ... */
When I compile and run it it always prints the follwoing strange addresses:
A new connection established from 252.127.0.0 or
A new connection established from 253.127.0.0 or
A new connection established from 255.127.0.0
no matter from what machine I connect. I use telnet for testing connections.
I have written another version of an echo server written in pure C (without libevent).When I run it it always returns the right address.