Reputation: 2571
I'm trying to use ENet library, it's for programming with reliable UDP socket. On the Feature
tab of the official site it says:
ENet will send and deliver packets regardless of size. Large packets are fragmented into many smaller packets of suitable size, and reassembled on the foreign host to recover the original packet for delivery. The process is entirely transparent to the developer.
I tested with a simple server/client, it's ok to send 1k data to server, but when data grow up to 2k, it seems not received by the server.
My code:
// 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();
}
//client
#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();
}
Any wrong with the code?
Upvotes: 1
Views: 1672
Reputation: 2571
I found enet_peer_disconnect_later
, this seems the API that works. I don't know why the official example use enet_peer_disconnect
..
Upvotes: 2