简体   繁体   English

在TCP端口上侦听时没有任何反应

[英]Nothing happens when listening on a TCP port

I'm trying to write a small, example app to listen to a TCP port, but it's not working for some reason. 我正在尝试编写一个小的示例应用程序来侦听TCP端口,但是由于某些原因它无法正常工作。

It see seems to reach accept(3p) , but lsof -i :1080 reports that nothing is listening on that port. 它看起来似乎达到了accept(3p) ,但是lsof -i :1080报告该端口上没有监听。 There are not errors, or anything else remotely obvious. 没有错误,或者其他任何明显的错误。

Any hints? 有什么提示吗?

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

const int THREADS = 4;
const int BUFFER_SIZE = 256;

void usage(char *app_name) {
    printf("Usage: %s PORT\n", app_name);
}

int main(int argc, char **argv) {
    struct sockaddr_in6 serv_addr, addr_cli;
    char buffer[BUFFER_SIZE];
    int sockfd, sock_cli, n, port;

    if (argc < 2) {
        usage(argv[0]);
        return -1;
    }

    port = atoi(argv[1]);

    if ((sockfd = socket(AF_INET6, SOCK_STREAM, 0)) < 0) {
        perror("Could not open socket");
        return errno;
    }

    serv_addr.sin6_family = AF_INET6;
    serv_addr.sin6_port = htonl(port);
    // other addresses: INADDR_LOOPBACK, INADDR_ANY
    serv_addr.sin6_addr = in6addr_any;
    serv_addr.sin6_scope_id = 0;

    if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) != 0) {
        perror("Could not bind to socket");
        return errno;
    }

    if (listen(sockfd, THREADS + 1) < 0) {
        perror("Could not listen to socket");
        return errno;
    }

    printf("Listening on :%d\n", port);

    unsigned long l;
    if ((sock_cli = accept(sockfd, (struct sockaddr *) &addr_cli, (socklen_t*) &l)) != 0) {
        perror("Could not accept connections on socket");
        return errno;
    }

    printf("Connection accepted\n");

    while ((n = recv(sock_cli, buffer, BUFFER_SIZE, 0)) > 0) {
        printf("> Got data: %s\n", buffer);
    }

    if (n < 0) {
        printf("Could not receive data\n");
        return errno;
    }

    printf("Bye!\n");
}

I got your program to work 我有你的程序可以工作

$ ./hugo 8888 &
[1] 25330
$ Listening on :8888
telnet -6 localhost 8888
Trying ::1...
Connected to localhost.
Escape character is '^]'.
Connection accepted
hello 
> Got data: hello
^]
telnet> quit
Connection closed.
Bye!
[1]+  Exit 10                 ./hugo 8888

making the following changes: 进行以下更改:

#include <netinet/in.h>
...
serv_addr.sin6_port = htons(port);
...
if ((sock_cli = accept(sockfd,  
                       (struct sockaddr *) &addr_cli, 
                       (socklen_t*) &l)) == -1) {

As the comment suggested, htons (3) causes the correct port to be listened to. 正如评论所建议的, htons (3)导致侦听正确的端口。 And accept (2) returns a socket, so you have test explicitly for -1 . 并且accept (2)返回一个套接字,因此您已明确测试-1

If I could suggest two small things, 如果我能提出两个小建议,

  • return EXIT_SUCCESS from main . main返回EXIT_SUCCESS。 :-) :-)
  • use err (3) instead of perror (3). 使用err (3)代替perror (3)。 Less typing, and you really don't want to return errno anyway. 更少的输入,您真的不想返回errno

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

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