Qt network programming based on C + + -- network conference program based on IP Multicast

Posted by AndrewBacca on Thu, 20 Jan 2022 20:38:55 +0100

catalogue

1, Experimental topic

2, Experimental purpose

3, Overall design

1. Experimental principle

2. Design steps

4, Detailed design

1. Procedure flow chart

5, Experimental results and analysis

6, Summary and experience

1, Experimental topic

Network conference program based on IP Multicast

2, Experimental purpose

Referring to the LAN IP multicast program in Appendix 3, design a network conference program with graphical interface (just realize the text multicast mode).

3, Overall design

1. Experimental principle

IP Multicast (also known as multiple access broadcast or multicast) technology is a TCP/IP network technology that allows one or more hosts (multicast source) to send a single packet to multiple hosts (once and at the same time). As a point to multipoint communication, multicast is one of the effective methods to save network bandwidth. In network applications, when the signal of one node needs to be transmitted to multiple nodes, whether using repeated point-to-point communication or broadcasting, it will seriously waste network bandwidth. Only multicast is the best choice. Multicast enables one or more multicast sources to send data packets only to a specific multicast group, and only the host joining the multicast group can receive data packets.

2. Design steps

Receiving end steps:

(1) Create a sock_ Socket of Dgram type.
(2) Bind the Socket to a local port to receive multicast data sent by the server.
(3) Join a multicast group.
① A WSAJoinLeaf is introduced into WinSock2,
② To join a multicast group on WinSock1 platform, you need to call the setsockopt function and set the IP address at the same time_ ADD_ The membership option specifies the address structure of the group you want to join.
(4) Receive multicast data.
(5) Exit multicast group, IP_DROP_MEMBERSHIP option.

Implementation steps of sender:
(1) Create a sock_ Socket of Dgram type.
(2) . send multicast data.

4, Detailed design

1. Procedure flow chart

2. Experimental code (part)

void UDP::on_sed_clicked()
{
    /...Create, bind, and other events.../
    SOCKADDR_IN addrServer;
    addrServer.sin_family = AF_INET;
    addrServer.sin_port = htons(MCASTPORT);//Bound host port number
    addrServer.sin_addr.S_un.S_addr = inet_addr(MCASTADDR);//Converts the port number to a binary number
    int nLength = sizeof(addrServer);
    char* szSendMsg;
    QString str = mui->send->toPlainText();
    QByteArray ba = str.toLocal8Bit();
    szSendMsg = ba.data();
    sendto(socketClient, szSendMsg, strlen(szSendMsg), 0, (SOCKADDR*)&addrServer, nLength);
}
void WorkThread::run()
{
    /...Create, bind, and other events.../
    setsockopt(RecvSocket,SOL_SOCKET,SO_REUSEADDR,(char*)&b,sizeof(b));
    struct ip_mreq stMreq;
    stMreq.imr_multiaddr.s_addr = inet_addr("239.1.100.1");
    stMreq.imr_interface.s_addr = htonl(INADDR_ANY);
    setsockopt(RecvSocket, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char*)&stMreq, sizeof(stMreq));

    while (1)
    {
        memset(RecvBuf, 0, sizeof(RecvBuf));
        iResuit = recvfrom(RecvSocket, RecvBuf, BufLen, 0, (sockaddr*)&SenderAddr, &SenderAddrSize);
        if (iResuit == SOCKET_ERROR)
        {
            printf("recvfrom failed with:%d\n", WSAGetLastError());//Wsagetlastererror() gets the corresponding error code
        }
        else
        {
            QString srt = QString::fromLocal8Bit(RecvBuf);
            mui->recv->append(srt);
        }
    }

    setsockopt(RecvSocket, IPPROTO_IP, IP_DROP_MEMBERSHIP, (char*)&stMreq, sizeof(stMreq));
    if (closesocket(RecvSocket) == SOCKET_ERROR)
    {
        printf("close failed");
        return ;
    }
    WSACleanup();
    return ;
}

The encoding also needs to be converted to GBK.

5, Experimental results and analysis

Click the join button to trigger an event to join the multicast group. Click send to send it to all members joining the multicast group.

6, Summary and experience

This example discusses IP multicast communication. The example program should be composed of Sender and Receiver. One part is responsible for sending and the other end is responsible for receiving. However, because the function is too simple, it is integrated here. Any multicast group member can send data to or receive data from the multicast group.

During the experiment, the problem of port occupation occurs. When a program starts to occupy the port, other programs can't occupy it again. Solution: set SO_REUSEADDR. It allows complete and repeated binding: when an IP address and port are bound to a socket, it also allows this IP address and port to be bound to another socket. This feature is only available on systems that support multicast, and only for UDP sockets.

Topics: C++ network Qt