同一端口是否可以绑定到多个IP上(关于Socket编程中地址与端口绑定那些事)

 

2016年07月07日 20:28:31 皓月如我 阅读数:7026 标签: socketc++编程 收起

个人分类: c++

版权声明:本文为博主原创文章,转载请注明【转载自皓月如我的CSDN博客】,并贴出原始链接地址。 https://blog.csdn.net/fm0517/article/details/51854048

一块网卡有多个IP,是否能够将同一个端口绑定在多个IP地址上?

废话不多说,上源码: 
【源码1】

#include 
#include 

int main(int argc, char *argv[])
{

    QAbstractSocket *qAbsSocket1 = new QAbstractSocket(QAbstractSocket::UdpSocket, NULL);
    QAbstractSocket *qAbsSocket2 = new QAbstractSocket(QAbstractSocket::UdpSocket, NULL);

    QHostAddress addr1("172.20.1.177");
    QHostAddress addr2("192.168.199.177");

    int port1 = 7777;
    int port2 = 7778;

    bool bindOK;

    bindOK = qAbsSocket1->bind(addr1, port1);
    printf("[1] qAbsSocket1 bind port:%d to addr:%s %s.\n", port1, addr1.toString().toLocal8Bit().data(), bindOK?"ok":"fail");
    bindOK = qAbsSocket1->bind(addr1, port2);
    printf("[2] qAbsSocket1 bind port:%d to addr:%s %s.\n", port2, addr1.toString().toLocal8Bit().data(), bindOK ? "ok" : "fail");
    bindOK = qAbsSocket1->bind(addr2, port1);
    printf("[3] qAbsSocket1 bind port:%d to addr:%s %s.\n", port1, addr2.toString().toLocal8Bit().data(), bindOK ? "ok" : "fail");

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

输出结果: 
[1] qAbsSocket1 bind port:7777 to addr:172.20.1.177 ok. 
QNativeSocketEngine::bind() was not called in QAbstractSocket::UnconnectedState 
[2] qAbsSocket1 bind port:7778 to addr:172.20.1.177 fail. 
QNativeSocketEngine::bind() was not called in QAbstractSocket::UnconnectedState 
[3] qAbsSocket1 bind port:7777 to addr:192.168.199.177 fail. 
Press any key to continue …

【结论1】 
同一个Socket只可以将1个端口绑定到1个地址上。

【源码2】

#include 
#include 

int main(int argc, char *argv[])
{

    QAbstractSocket *qAbsSocket1 = new QAbstractSocket(QAbstractSocket::UdpSocket, NULL);
    QAbstractSocket *qAbsSocket2 = new QAbstractSocket(QAbstractSocket::UdpSocket, NULL);

    QHostAddress addr1("172.20.1.177");
    QHostAddress addr2("192.168.199.177");

    int port1 = 7777;
    int port2 = 7778;

    bool bindOK;

    bindOK = qAbsSocket1->bind(addr1, port1);
    printf("[1] qAbsSocket1 bind port:%d to addr:%s %s.\n", port1, addr1.toString().toLocal8Bit().data(), bindOK?"ok":"fail");
    bindOK = qAbsSocket2->bind(addr1, port1);
    printf("[2] qAbsSocket2 bind port:%d to addr:%s %s.\n", port1, addr1.toString().toLocal8Bit().data(), bindOK ? "ok" : "fail");

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

输出结果: 
[1] qAbsSocket1 bind port:7777 to addr:172.20.1.177 ok. 
[2] qAbsSocket2 bind port:7777 to addr:172.20.1.177 fail. 
Press any key to continue …

【结论2】 
即使不同的Socket也不能重复绑定相同的地址和端口。

【源码3】

#include 
#include 

int main(int argc, char *argv[])
{

    QAbstractSocket *qAbsSocket1 = new QAbstractSocket(QAbstractSocket::UdpSocket, NULL);
    QAbstractSocket *qAbsSocket2 = new QAbstractSocket(QAbstractSocket::UdpSocket, NULL);

    QHostAddress addr1("172.20.1.177");
    QHostAddress addr2("192.168.199.177");

    int port1 = 7777;
    int port2 = 7778;

    bool bindOK;

    bindOK = qAbsSocket1->bind(addr1, port1);
    printf("[1] qAbsSocket1 bind port:%d to addr:%s %s.\n", port1, addr1.toString().toLocal8Bit().data(), bindOK?"ok":"fail");
    bindOK = qAbsSocket2->bind(addr1, port2);
    printf("[2] qAbsSocket2 bind port:%d to addr:%s %s.\n", port2, addr1.toString().toLocal8Bit().data(), bindOK ? "ok" : "fail");

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

输出结果: 
[1] qAbsSocket1 bind port:7777 to addr:172.20.1.177 ok. 
[2] qAbsSocket2 bind port:7778 to addr:172.20.1.177 ok. 
Press any key to continue …

【结论3】 
不同的Socket可以将不同的端口绑定到相同的IP地址上。

【源码4】

#include 
#include 

int main(int argc, char *argv[])
{

    QAbstractSocket *qAbsSocket1 = new QAbstractSocket(QAbstractSocket::UdpSocket, NULL);
    QAbstractSocket *qAbsSocket2 = new QAbstractSocket(QAbstractSocket::UdpSocket, NULL);

    QHostAddress addr1("172.20.1.177");
    QHostAddress addr2("192.168.199.177");

    int port1 = 7777;
    int port2 = 7778;

    bool bindOK;

    bindOK = qAbsSocket1->bind(addr1, port1);
    printf("[1] qAbsSocket1 bind port:%d to addr:%s %s.\n", port1, addr1.toString().toLocal8Bit().data(), bindOK?"ok":"fail");
    bindOK = qAbsSocket2->bind(addr2, port1);
    printf("[2] qAbsSocket2 bind port:%d to addr:%s %s.\n", port1, addr2.toString().toLocal8Bit().data(), bindOK ? "ok" : "fail");

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

输出结果 
[1] qAbsSocket1 bind port:7777 to addr:172.20.1.177 ok. 
[2] qAbsSocket2 bind port:7777 to addr:192.168.199.177 ok. 
Press any key to continue …

【结论4】 
不同的Socket可以将相同的端口绑定到不同的IP地址上。

【结论汇总】 
1. 同一个Socket只可以将1个端口绑定到1个地址上。 
2. 即使不同的Socket也不能重复绑定相同的地址和端口。 
3. 不同的Socket可以将不同的端口绑定到相同的IP地址上。 
4. 不同的Socket可以将相同的端口绑定到不同的IP地址上。

你可能感兴趣的:(Linux)