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

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

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

#include <QAbstractSocket>
#include <QHostAddress>

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] 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 <QAbstractSocket>
#include <QHostAddress>

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] 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 <QAbstractSocket>
#include <QHostAddress>

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] 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 <QAbstractSocket>
#include <QHostAddress>

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] 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地址上。

  • 12
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

皓月如我

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值