用QT的UDP广播通讯,向同一个局域网的其他主机发送自己的ip地址,用控制台的方式实现,不要窗口

就是连在同一个路由器的所有主机上都跑一下这个QT程序,然后每台主机都向其他主机UDP广播
发送自己的ip地址,这个建立的工程文件是QT Console Application,也就是不要用到
窗口的任何东西,只在控制台这个黑框框里显示结果就行了,麻烦大佬给代码的时候给一下
注释,就给一下cpp里的代码就好,分客户端和服务端,万分感谢。

2个回答

LiveServer::LiveServer(Analysis *p):Analysis(p)
{
udp=new QUdpSocket;
connect(&timer,SIGNAL(timeout()),this,SLOT(broadcastInfo()));
timer.start(30000);
}
void LiveServer::setPort(int port, QString settings, HANDLE d)
{

das=(SysInfo*)d;
udp->bind(IPMSG_DEFAULT_PORT);
connect(udp,SIGNAL(readyRead()),this,SLOT(readReady()));
broadcastInfo();

}
void LiveServer::broadcastInfo()//客户端调用此函数广播
{
SearchCommand com;
initCommand(&com);
char buf[COM_LENGTH];
memset(buf,0,sizeof(buf));
com.commandNo=IPMSG_BR_ENTRY;
com.describe=das->getSettings("describe");
com.hardVersion=das->getSettings("hardVersion");
com.softVersion=das->getSettings("softVersion");
com.sysLocation=das->getSettings("sysLocation");
com.name=das->getSettings("name");
com.product=das->getSettings("product");
com.uuid=das->getSettings("uuid");
com.ipAddr=das->getSettings("ipAddr");
com.mac=das->getSettings("mac");
com.mask=das->getSettings("mask");
com.dns1=das->getSettings("dns1");
com.dns2=das->getSettings("dns2");
com.ipAddrMethod=das->getSettings("dynamic_static_ipmethod");
msgCreater(buf,&com,MSG_LENGTH);
udp->writeDatagram(buf,strlen(buf),QHostAddress::Broadcast,IPMSG_DEFAULT_PORT);

}
void LiveServer::readReady()
{
while (udp->hasPendingDatagrams()) {
QByteArray datagram;
datagram.resize(udp->pendingDatagramSize());
QHostAddress sender;
quint16 senderPort;

    udp->readDatagram(datagram.data(), datagram.size(),
                      &sender, &senderPort);
    SearchCommand com;
    msgParser(datagram,&com);
    isearch_arp_entry(datagram,sender,senderPort);
    if(com.commandNo==IPMSG_BR_ENTRY)
    {
        initCommand(&com);
        char buf[COM_LENGTH];
        memset(buf,0,sizeof(buf));
        com.commandNo=IPMSG_ANSENTRY;
        com.describe=das->getSettings("describe");
        com.hardVersion=das->getSettings("hardVersion");
        com.softVersion=das->getSettings("softVersion");
        com.sysLocation=das->getSettings("sysLocation");
        com.name=das->getSettings("name");
        com.product=das->getSettings("product");
        com.uuid=das->getSettings("uuid");
        com.ipAddr=das->getSettings("ipAddr");
        com.mac=das->getSettings("mac");
        com.mask=das->getSettings("mask");
        com.dns1=das->getSettings("dns1");
        com.dns2=das->getSettings("dns2");
        com.ipAddrMethod=das->getSettings("dynamic_static_ipmethod");

        msgCreater(buf,&com,MSG_LENGTH);
        udp->writeDatagram(buf,strlen(buf),sender,senderPort);

    }

}

}
void LiveServer::initCommand(SearchCommand *com)
{
if (com == NULL)
return;
com->version = 1;
com->packetNo = (unsigned int)time(NULL);
com->commandNo =0 ;

}
int LiveServer::msgParser(QString data, SearchCommand* com)//, struct sockaddr_in *peer)
{

QStringList list=data.split(":");
int count=list.count();
if(count>4)
{
    com->version=list.at(0).toUInt();
    com->packetNo=list.at(1).toUInt();
    com->commandNo=list.at(2).toUInt();
    com->uuid=list.at(3);
}
else
    return 0;
for(int i=4;i<count;i++)
{
    switch(i)
    {
    case 4:
        com->product=list.at(i);
        break;
    case 5:
        com->sysLocation=list.at(i);
        break;
    case 6:
        com->mac=list.at(i);
        break;
    case 7:
        com->ipAddr=list.at(i);
        break;
    case 8:
        com->mask=list.at(i);
        break;
    case 9:
        com->gateway=list.at(i);
        break;
    case 10:
        com->dns1=list.at(i);
        break;
    case 11:
        com->dns2=list.at(i);
        break;
    case 12:
        com->ipAddrMethod=list.at(i);
        break;
    case 13:
        com->name=list.at(i);
        break;
    case 14:
        com->softVersion=list.at(i);
        break;
    case 15:
        com->hardVersion=list.at(i);
        break;
    case 16:
        com->describe=list.at(i);
        break;
    case 17:
        com->additional=list.at(i);
        break;


    }
}

return count;

}

int LiveServer::msgCreater(char* msg, SearchCommand* com, size_t msgLen)
{
int len, tmp;
char dest[COM_LENGTH];

if ((dest==NULL)||(com==NULL))
    return -1;

len = snprintf(dest, sizeof(dest), "%d:%d:%d:%s:%s:%s:%s:%s:%s:%s:%s:%s:%s:%s:%s:%s:%s:%s",
               com->version,
               com->packetNo,
               com->commandNo,
               com->uuid.toUtf8().data(),
               com->product.toUtf8().data(),
               com->sysLocation.toUtf8().data(),
               com->mac.replace(":","").toUtf8().data(),
               com->ipAddr.toUtf8().data(),
               com->mask.toUtf8().data(),
               com->gateway.toUtf8().data(),
               com->dns1.toUtf8().data(),
               com->dns2.toUtf8().data(),
               com->ipAddrMethod.toUtf8().data(),
               com->name.toUtf8().data(),
               com->softVersion.toUtf8().data(),
               com->hardVersion.toUtf8().data(),
               com->describe.toUtf8().data(),
               com->additional.toUtf8().data());
tmp = sizeof(dest) - len - 1;

strncpy(msg, dest, msgLen);

return 0;

}

“分客户端和服务端”,这就要求搞个cs架构了,有点麻烦

qq_38151907
大数据★东东 其实就是一个简单的UDP广播通讯,调用下查看自身IP的函数,不需要搞CS,我只需要很简单的通讯代码做个测试就行。
接近 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐