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

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

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

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

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