Search code examples
c++udpwinsock

UDP Server Restart every 5 seconds


I am writing for the first time in this language and could only write this UDP server and client. I need help; I want to server waits for a response and displays it. If within 5 sec. the answer is not received, the program starts its execution again.If you don't understand task, you can translate task(fifth) :https://www.opennet.ru/docs/RUS/socket/node15.html

  #include <iostream>
    #include <WS2tcpip.h>
    #pragma comment (lib, "ws2_32.lib")
    using namespace std;
    void main()
    {
        struct timeval tv;
        fd_set fd;
        WSADATA data;
        WORD version = MAKEWORD(2, 2);
        int wsOk = WSAStartup(version, &data);
        cout << "Server opened" << endl;
        if (wsOk != 0)
        {
            cout << "Can't start Winsock! " << wsOk;
            return;
        }
        SOCKET in = socket(AF_INET, SOCK_DGRAM, 0);
        sockaddr_in serverHint;
        serverHint.sin_addr.S_un.S_addr = ADDR_ANY; 
        serverHint.sin_family = AF_INET; 
        serverHint.sin_port = htons(54000); 
        if (bind(in, (sockaddr*)& serverHint, sizeof(serverHint)) == SOCKET_ERROR)
        {
            cout << "Can't bind socket! " << WSAGetLastError() << endl;
            return;
        }
        sockaddr_in client; 
        int clientLength = sizeof(client); 
        char buf[1024];
        while (true)
        {

            FD_ZERO(&fd);
            FD_SET(in, &fd);
            tv.tv_sec = 5;
            tv.tv_usec = 0;
            if (select(0, &fd, NULL, NULL, &tv) > 0)
            {
                ZeroMemory(&client, clientLength);
                ZeroMemory(buf, 1024);
                int bytesIn = recvfrom(in, buf, 1024, 0, (sockaddr*)& client, &clientLength);
                if (bytesIn == SOCKET_ERROR)
                {
                    cout << "Error receiving from client " << WSAGetLastError() << endl;
                    continue;
                }
                char clientIp[256];
                ZeroMemory(clientIp, 256);
                inet_ntop(AF_INET, &client.sin_addr, clientIp, 256);
                cout << "Message recv from " << clientIp << " : " << buf << endl;

            }
            else  {
                cout << "Timeout" << endl;
                closesocket(in);
                break;

            }
            closesocket(in);
            WSACleanup();

        }
    }

Solution

  • #include <iostream>
    #include <WS2tcpip.h>
    #pragma comment (lib, "ws2_32.lib")
    using namespace std;
    void main()
    {
        struct timeval tv;
        fd_set fd;
        WSADATA data;
        WORD version = MAKEWORD(2, 2);
        int wsOk = WSAStartup(version, &data);
        cout << "5" << endl;
        if (wsOk != 0)
        {
            cout << "Winsock" << wsOk;
            return;
        }
        SOCKET in = socket(AF_INET, SOCK_DGRAM, 0);
        sockaddr_in serverHint;
        serverHint.sin_addr.S_un.S_addr = ADDR_ANY; 
        serverHint.sin_family = AF_INET; 
        serverHint.sin_port = htons(54000); 
        if (bind(in, (sockaddr*)& serverHint, sizeof(serverHint)) == SOCKET_ERROR)
        {
            cout << "hhhh" << WSAGetLastError() << endl;
            return;
        }
        sockaddr_in client; 
        int clientLength = sizeof(client); 
        char buf[1024];
        while (true)
        {
    
            FD_ZERO(&fd);
            FD_SET(in, &fd);
            tv.tv_sec = 5;
            tv.tv_usec = 0;
            if (select(0, &fd, NULL, NULL, &tv) > 0)
            {
                ZeroMemory(&client, clientLength);
                ZeroMemory(buf, 1024);
                int bytesIn = recvfrom(in, buf, 1024, 0, (sockaddr*)& client, &clientLength);
                if (bytesIn == SOCKET_ERROR)
                {
                    cout << "EEEROOR" << WSAGetLastError() << endl;
                    continue;
                }
                char clientIp[256];
                ZeroMemory(clientIp, 256);
                inet_ntop(AF_INET, &client.sin_addr, clientIp, 256);
                cout << "С " << clientIp << " : " << buf << endl;
                closesocket(in);
                WSACleanup();
                exit(0);
            }
            else  {
                cout << "jhkjhkjhjn" << endl;
                closesocket(in);
                main();
    
            }
    
        }
    }