UDP 服务器每 5 秒重启一次

UDP Server Restart every 5 seconds

我第一次用这种语言写作,只能写这个 UDP 服务器和客户端。我需要帮助;我想让服务器等待响应并显示它。如果在 5 秒内。没收到回复,程序开始执行again.If task你不懂,可以翻译task(五):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();

        }
    }

任务翻译 google translate:

  1. Write a program that asks the user for an IP address, port number and text string,
  2. sends a string to the specified address using the UDP protocol,
  3. waits for a response, and displays it on the screen.
    • If within 5 sec. the answer is not received, the program starts its execution again.

您的程序完全缺少第 1 部分和第 2 部分,因此您必须添加它们。

对于第 3 部分,移动当前的 while 循环,使其涵盖 main:

中的所有内容
int main() // note: main must be declared to return 'int'
{
    while(true) { // new start of the while loop, first in main

            // ... your current code until the below line ...

            cout << "Message recv from " << clientIp << " : " << buf << endl;

            // exit program if you've received a response
            closesocket(in);
            break; // break out of while loop
        }
        else {
            cout << "Timeout" << endl;
        }

        closesocket(in);
        WSACleanup();

    } // end of while loop

    WSACleanup();
} // end of main

每次尝试都需要重新初始化 Winsock2,而且它在多个地方也有 closesocket。我会为 WSAStartup/WSACleanupsocket/closesocket 添加 类 来管理他们获得的资源。

#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();

        }

    }
}