Skip to content

修复启用KCP时可能存在服务器无法正常给客户端发送数据问题 #753

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2025

Conversation

lfeng1420
Copy link
Contributor

@lfeng1420 lfeng1420 commented Jul 22, 2025

当服务器收到另一个客户端的消息时,由于已经覆盖了io的peeraddr,服务器无法再给覆盖前的客户端发送消息(与 #707 应该是一致的)。
本地改动了下evpp目录下的UdpServer_test.cpp和UdpClient_test.cpp进行了验证,可以正常通信。

/*
 * UdpServer_test.cpp
 *
 * @build   make evpp
 * @server  bin/UdpServer_test 1234
 * @client  bin/UdpClient_test 1234
 *
 */

#include <iostream>
#include <map>
#include <string>
#include "hstring.h"

#include "UdpServer.h"

using namespace hv;

int main(int argc, char* argv[]) {
    if (argc < 2) {
        printf("Usage: %s port\n", argv[0]);
        return -10;
    }
    int port = atoi(argv[1]);

    std::map<std::string, sockaddr_u> addrMap;
    UdpServer srv;
    kcp_setting_t stSetting;
    kcp_setting_init_with_normal_mode(&stSetting);
    srv.setKcp(&stSetting);
    int bindfd = srv.createsocket(port);
    if (bindfd < 0) {
        return -20;
    }
    printf("server bind on port %d, bindfd=%d ...\n", port, bindfd);
    srv.onMessage = [&](const SocketChannelPtr& channel, Buffer* buf) {
        std::string addr = channel->peeraddr();
        addrMap[addr] = *(sockaddr_u*)hio_peeraddr(channel->io());

        // echo
        printf("< %s %.*s\n", addr.c_str(), (int)buf->size(), (char*)buf->data());
        channel->write(buf);
    };
    srv.start();

    std::string str;
    while (std::getline(std::cin, str)) {
        if (str == "close") {
            srv.closesocket();
        } else if (str == "start") {
            srv.start();
        } else if (str == "stop") {
            srv.stop();
            break;
        } else {
            hv::StringList stringList = split(str, ' ');
            if (stringList.size() == 2) {
                auto addrIt = addrMap.find(stringList[0]);
                if (addrIt != addrMap.end()) {
                    printf("> send %s to %s\n", stringList[1].c_str(), stringList[0].c_str());
                    srv.sendto(stringList[1], (sockaddr*)&addrIt->second);
                }
            }
            else {
                srv.sendto(str);
            }
        }
    }

    return 0;
}
/*
 * UdpClient_test.cpp
 *
 * @build   make evpp
 * @server  bin/UdpServer_test 1234
 * @client  bin/UdpClient_test 1234
 *
 */

#include <iostream>

#include "UdpClient.h"
#include "htime.h"

using namespace hv;

int main(int argc, char* argv[]) {
    if (argc < 2) {
        printf("Usage: %s remote_port [remote_host]\n", argv[0]);
        return -10;
    }
    int remote_port = atoi(argv[1]);
    const char* remote_host = "127.0.0.1";
    if (argc > 2) {
        remote_host = argv[2];
    }

    UdpClient cli;
    kcp_setting_t stSetting;
    kcp_setting_init_with_normal_mode(&stSetting);
    cli.setKcp(&stSetting);
    int sockfd = cli.createsocket(remote_port, remote_host);
    if (sockfd < 0) {
        return -20;
    }
    printf("client sendto port %d, sockfd=%d ...\n", remote_port, sockfd);
    cli.onMessage = [](const SocketChannelPtr& channel, Buffer* buf) {
        printf("< %.*s\n", (int)buf->size(), (char*)buf->data());
    };
    cli.start();

    std::string str;
    while (std::getline(std::cin, str)) {
        if (str == "close") {
            cli.closesocket();
        } else if (str == "start") {
            cli.start();
        } else if (str == "stop") {
            cli.stop();
            break;
        } else {
            cli.sendto(str);
        }
    }

    return 0;
}

@lfeng1420 lfeng1420 changed the title 修复启用KCP时可能存在服务器无法正常给客户端发送消息问题 修复启用KCP时可能存在服务器无法正常给客户端发送数据问题 Jul 22, 2025
@ithewei ithewei merged commit 1ef4424 into ithewei:master Jul 24, 2025
10 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants