• 请不要在回答技术问题时复制粘贴 AI 生成的内容
b00tyhunt3r
V2EX  ›  程序员

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

  •  
  •   b00tyhunt3r · Jan 13, 2020 · 2860 views
    This topic created in 2506 days ago, the information mentioned may be changed or developed.

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

    mrcn
    mrcn
    Jan 13, 2020 via Android
    1
    记得是如果占用了,listen 还是 bind 会出错,返回值-1。
    chu1337
    chu1337
    Jan 13, 2020
    2
    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
    paoqi2048
    Jan 13, 2020
    3
    bind error: EADDRINUSE: The given address is already in use.
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2869 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 10:26 · PVG 18:26 · LAX 02:26 · JFK 05:26
    ♥ Do have faith in what you're doing.