SFML udp通信实例

包含的lib库文件,免得一个一个复制名称:

sfml-window-d.lib
sfml-system-d.lib
sfml-audio-d.lib
sfml-graphics-d.lib
sfml-main-d.lib
sfml-network-d.lib
vorbis.lib
vorbisenc.lib
vorbisfile.lib

void runUdpClient(unsigned short port)
{
    // Ask for the server address
    sf::IpAddress server;
    do
    {
        std::cout << "Type the address or name of the server to connect to: ";
        std::cin >> server;
    } while (server == sf::IpAddress::None);

    // Create a socket for communicating with the server
    sf::UdpSocket socket;

    // Send a message to the server
    const char out[] = "Hi, I'm a client";
    if (socket.send(out, sizeof(out), server, port) != sf::Socket::Done)
        return;
    std::cout << "Message sent to the server: \"" << out << "\"" << std::endl;

    // Receive an answer from anyone (but most likely from the server)
    char in[128];
    std::size_t received;
    sf::IpAddress sender;
    unsigned short senderPort;
    if (socket.receive(in, sizeof(in), received, sender, senderPort) != sf::Socket::Done)
    {
        return;
    }
        
    std::cout << "Message received from " << sender << ": \"" << in << "\"" << std::endl;
}

using namespace sf;
void runUdpPackageClient()
{
    // Ask for the server address
    IpAddress adress("localhost");

    // Create a socket for communicating with the server
    sf::UdpSocket socket;

    Packet sendpacket;
    sendpacket << "ceshishuju";
    socket.send(sendpacket, adress, 1028);

    IpAddress remoteAddress;
    unsigned short remotePort;
    Packet packet;
    if (socket.receive(packet, remoteAddress, remotePort) != sf::Socket::Done)
    {
        return;
    }
    int yyyy = 666;
}

测试使用SocketTool.exe工具即可

相关推荐

  1. SFML udp通信实例

    2024-03-25 08:12:05       39 阅读
  2. Spring WebSocket实现实时通信

    2024-03-25 08:12:05       56 阅读
  3. Python实现WebSocket通信

    2024-03-25 08:12:05       32 阅读
  4. QT实现WebSocket通信

    2024-03-25 08:12:05       28 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-25 08:12:05       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-25 08:12:05       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-25 08:12:05       82 阅读
  4. Python语言-面向对象

    2024-03-25 08:12:05       91 阅读

热门阅读

  1. IOS面试题编程机制 51-55

    2024-03-25 08:12:05       40 阅读
  2. IOS面试题编程机制 56-60

    2024-03-25 08:12:05       33 阅读
  3. 计算机网络 1.2网络功能及应用

    2024-03-25 08:12:05       32 阅读
  4. 每日一练:LeeCode-561、 数组拆分【数组+排序】

    2024-03-25 08:12:05       39 阅读
  5. day3-QT

    day3-QT

    2024-03-25 08:12:05      35 阅读
  6. Python爬虫之正则表达式与httpx的使用与案例

    2024-03-25 08:12:05       36 阅读
  7. 深度解析单例模式

    2024-03-25 08:12:05       40 阅读
  8. NPM 国内镜像

    2024-03-25 08:12:05       34 阅读