简体   繁体   English

基于libuv的tcp服务器未在指定端口上侦听

[英]libuv based tcp server not listening on specified port

Wrote a TCP server using libuv, it doesn't listen on the right port. 使用libuv编写了一个TCP服务器,它没有在正确的端口上侦听。 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 例如,以下内容应该在TCP端口3005上侦听,但是根据命令netstat -antp | grep LISTEN的输出,它似乎在随机端口上netstat -antp | grep LISTEN netstat -antp | grep LISTEN running on Ubuntu 14.04. 在Ubuntu 14.04上运行的netstat -antp | grep LISTEN I am not port 3005 is not taken. 我不是端口3005不被占用。

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. 原来,ubuntu 14.04的默认libuv-dev版本为0.10。 Ran the following steps to install the latest libuv and it worked great. 运行以下步骤来安装最新的libuv,效果很好。

  • sudo apt-get purge libuv-dev 须藤apt-get清除libuv-dev
  • choose a directory to do the following step 选择目录以执行以下步骤
  • git clone https://github.com/libuv/libuv.git git clone https://github.com/libuv/libuv.git
  • cd libuv 光盘库
  • ./configure 。/配置
  • make 使
  • sudo make install 须藤使安装
  • gcc libuv_example.c -luv gcc libuv_example.c -luv

Now it listens on the right port. 现在,它在正确的端口上侦听。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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