简体   繁体   中英

libuv based tcp server not listening on specified port

Wrote a TCP server using libuv, it doesn't listen on the right port. For example, the following is supposed to be listen on TCP port 3005, but it appears to be listening on a random port according to the output of command netstat -antp | grep LISTEN netstat -antp | grep LISTEN running on Ubuntu 14.04. I am not port 3005 is not taken.

Any idea why?

#include <stdio.h>
#include <stdlib.h>
#include <uv.h>
#define DEFAULT_PORT 3005
#define DEFAULT_BACKLOG 1000
uv_loop_t *loop;

void on_new_connection(uv_stream_t *server, int status) {
}
int main() {
    loop = uv_default_loop();

    uv_tcp_t server;
    uv_tcp_init(loop, &server);

    struct sockaddr_in addr;
    uv_ip4_addr("0.0.0.0", DEFAULT_PORT, &addr);
    int r;
    r = uv_tcp_bind(&server, (const struct sockaddr*)&addr, 0);
    r = uv_listen((uv_stream_t*) &server, DEFAULT_BACKLOG, on_new_connection);
    if (r) {
        fprintf(stderr, "Listen error %s\n", uv_strerror(r));
        return 1;
    }
    return uv_run(loop, UV_RUN_DEFAULT);
}

Turned out the default libuv-dev for ubuntu 14.04 is of version 0.10. Ran the following steps to install the latest libuv and it worked great.

  • sudo apt-get purge libuv-dev
  • choose a directory to do the following step
  • git clone https://github.com/libuv/libuv.git
  • cd libuv
  • ./configure
  • make
  • sudo make install
  • gcc libuv_example.c -luv

Now it listens on the right port.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM