V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
b00tyhunt3r
V2EX  ›  程序员

Linux C Socket 编程 如何知道(检测)某个端口是否已建立监听?

  •  
  •   b00tyhunt3r · 2020-01-13 09:01:43 +08:00 · 1817 次点击
    这是一个创建于 1536 天前的主题,其中的信息可能已经有所发展或是发生改变。

    简单说想实现一个本地网络 p2p 通信程序,用户在控制台输入命令,程序开始检测本地网络 9990 这个端口,如果 9990 为空闲端口,则在此建立监听。如果 9990 位置已经有其他用户使用本程序建立好的监听,则 connect 加入。如果端口被其他程序占用,报错。 那么该如何得到端口状态呢?谢谢解答!!!!

    mrcn
        1
    mrcn  
       2020-01-13 09:23:42 +08:00 via Android
    记得是如果占用了,listen 还是 bind 会出错,返回值-1。
    chuhades
        2
    chuhades  
       2020-01-13 10:41:53 +08:00
    bind:

    ```
    RETURN VALUES
    Upon successful completion, a value of 0 is returned. Otherwise, a value of -1 is returned and the global integer variable errno is set to indicate the
    error.
    ```
    paoqi2048
        3
    paoqi2048  
       2020-01-13 11:00:46 +08:00
    bind error: EADDRINUSE: The given address is already in use.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3254 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 11:46 · PVG 19:46 · LAX 04:46 · JFK 07:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.