Help us understand the problem. What is going on with this article?

Socketプログラムメモ: lsof でのlisten確認

More than 3 years have passed since last update.

socketの勉強のため、macでsocketでlistenして、lsofで確認しようとしてはまった。
port:12345をlistenしているはずなのに、lsof -n -i:12345しても何も出てこない。数時間はまったが、
sudo lsof -n -i:12345 -Pで解決しました。
sudo必要でした。
なお、netstatではnetstat -anで、sudoなしでも大丈夫でした。

以下は、確認に使ったプログラム。
このプログラムの実行時にsudoしているのだから、lsofsudo必要なのはもっと早く気がつくべきであった。

listen_test.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <unistd.h>

int main(int argc, char **argv)
{
    int listenfd;
    struct sockaddr_in servaddr;

    listenfd = socket(AF_INET, SOCK_STREAM, 0);
    if(listenfd < 0) {
        perror("socket() err");
        exit(EXIT_FAILURE);
    }

    bzero(&servaddr, sizeof(servaddr));
    servaddr.sin_family = AF_INET;
    servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    servaddr.sin_port = htons(12345);
    servaddr.sin_len = sizeof(servaddr);

    if(bind(listenfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0) {
        perror("bind() err");
        exit(EXIT_FAILURE);
    }

    if(listen(listenfd, SOMAXCONN) < 0) {
        perror("listen() err");
        exit(EXIT_FAILURE);
    }
    while(1);
}
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away