ENet c ++库

时间:2016-01-20 17:15:12

标签: c++ enet

我正在尝试使用ENet库,它用于使用可靠的UDP套接字进行编程。在官方网站的Feature标签上,它说:

  

无论大小如何,ENet都会发送和传送数据包。大数据包被分段成许多适当大小的较小数据包,并在外部主机上重新组装以恢复原始数据包以进行传送。该过程对开发人员完全透明。

我使用简单的服务器/客户端测试,可以将1k数据发送到服务器,但是当数据增长到2k时,服务器似乎没有收到。

我的代码:

// server

#include <string>
#include <iostream>
#include <enet/enet.h>
using namespace std;

void main()
{
    // initialize
    if(enet_initialize()) {
        cout << "fail init" << endl;
        return;
    }

    ENetAddress address;
    address.host = ENET_HOST_ANY;
    address.port = 9000;

    ENetHost* server;
    server = enet_host_create(&address,
                  512,
                  2,
                  0,
                  0);
    if(server == NULL) {
        cout << "fail create host" << endl;
        enet_deinitialize();
        return;
    }


    ENetEvent event;
    while(enet_host_service(server, &event, 5000) >= 0) {
        if(event.type == ENET_EVENT_TYPE_CONNECT) { // client connected
            static unsigned int num = 0;
            ENetAddress remote = event.peer->address; // peer address
            char ip[256];
            enet_address_get_host_ip(&remote, ip, 256);
            cout << "ip:" << ip << " connected, id: " << num << endl;
            event.peer->data = (void*)num++;
        } else if(event.type == ENET_EVENT_TYPE_RECEIVE) {    // recv data
            cout << "received data at channel: " << event.channelID << endl;
            cout << "data length: " << event.packet->dataLength << endl;
            cout << "data: " << string((char*)event.packet->data, event.packet->dataLength) << endl;
            enet_packet_destroy(event.packet); // free
            cout << endl;
        } else if(event.type == ENET_EVENT_TYPE_DISCONNECT) {     // disconnect
            cout << "id " << event.peer->data << " disconnected" << endl;
        }
    }

    enet_deinitialize();
}

//客户端

#include <string>
#include <iostream>
#include <enet/enet.h>
using namespace std;

void main()
{
    if(enet_initialize()) {
        cout << "fail init" << endl;
        return;
    }

    ENetHost* client = enet_host_create(NULL,
                        1,
                        2,
                        0,
                        0);
    if(client == NULL) {
        cout << "fail create client" << endl;
        return;
    }


    ENetAddress svraddr;
    enet_address_set_host(&svraddr, "127.0.0.1");
    svraddr.port = 9000;

    ENetPeer* server = enet_host_connect(client, &svraddr, 2, 0);

    if(server == NULL) {
        cout << "fail connect" << endl;
        return;
    }

    ENetEvent event;
    if(enet_host_service (client, &event, 5000) > 0 &&
       event.type == ENET_EVENT_TYPE_CONNECT) {
        cout << "connected successfully" << endl;
    } else {
        enet_peer_reset (server);
        cout << "failed to connect" << endl;
        return;
    }


    string data(1024, 'a'); // <--------------- ok
    string data(2 * 1024, 'a'); // <--------------- fail
    ENetPacket* packet1 = enet_packet_create(data.c_str(), data.length(), ENET_PACKET_FLAG_RELIABLE);
    if(!packet1) {
        cout << "create packet fail" << endl;
        return;
    }
    if(enet_peer_send(server, 0, packet1) < 0) {
        cout << "send fail" << endl;
        return;
    }

    enet_host_flush (client);


    enet_peer_disconnect (server, 0);
    while(enet_host_service (client, &event, 5000) > 0) {
        switch(event.type) {
          case ENET_EVENT_TYPE_RECEIVE:
            enet_packet_destroy (event.packet);
            break;

          case ENET_EVENT_TYPE_DISCONNECT:
            cout << "disconnected" << endl;
            enet_deinitialize();
            return;
        }
    }

    enet_peer_reset(server);
    cout << "force reset" << endl;

    enet_deinitialize();
}

代码有问题吗?

1 个答案:

答案 0 :(得分:1)

我找到enet_peer_disconnect_later,这似乎是有效的API。我不知道为什么官方示例使用enet_peer_disconnect ..