[ENet-discuss] ENet - desconnection in 30 seconds after connection.

Nuno Silva little.coding.fox at gmail.com
Fri Nov 5 07:10:38 PDT 2010


Hey there.
On your while(true) on the client you must also call enet_host_service in
order for the client to keep its connection alive.

2010/11/5 Вячеслав Блинников <slavmfm at gmail.com>

> Hello!
>
> I wrote the base client accordingly the tutorial and the client which
> read inputting through console characters and send it to the server.
> Everything is fine but just first 30 seconds - then happen the
> disconnection (message "Client information disconnected" shown).
> What is wrong with my code?
>
>
>
> Server code:
>
> #include <cstdio>
> #include "enet\enet.h"
> int main(int argc, int** argv)
> {
>        if (enet_initialize () != 0)
>    {
>        printf ("An error occurred while initializing ENet.\n");
>        goto END;
>    }
>        ENetAddress address;
>        address.host = ENET_HOST_ANY;
>        address.port = 1234;
>        ENetHost* server = enet_host_create (   & address, 32, 2, 0, 0);
>    if (server == NULL)
>    {
>        printf("An error occurred while trying to create an ENet
> client host.\n");
>        goto END;
>    }
>        ENetEvent event;
> WAIT_FOR_AN_EVENT:
>        enet_host_service(server, &event, 5);
>        switch (event.type)
>        {
>        case ENET_EVENT_TYPE_CONNECT:
>                printf ("A new client connected from %x:%u.\n", event.peer
> ->
> address.host, event.peer -> address.port);
>                event.peer -> data = "Client information";
>                break;
>
>        case ENET_EVENT_TYPE_RECEIVE:
>                printf ("A packet of length %u was received from %s on
> channel %u.
> Containings:\n  %s", event.packet -> dataLength, event.peer -> data,
> event.channelID, event.packet -> data);
>                enet_packet_destroy (event.packet);
>                break;
>
>        case ENET_EVENT_TYPE_DISCONNECT:
>                printf ("%s disconected.\n", event.peer -> data);
>                event.peer -> data = NULL;
>                break;
>
>        case ENET_EVENT_TYPE_NONE:
>                break;
>        }
>        goto WAIT_FOR_AN_EVENT;
>
>        printf("host halted.\n");
>
> END:
>        getchar();
>        return 0;
> }
>
>
>
> Client code:
>
> #include <cstdio>
> #include "enet\enet.h"
> #include <vector>
> int main(int argc, int** argv)
> {
>        //where reading console data will be stored:
>        std::vector<char> buffer;
>
>        if (enet_initialize () != 0)
>    {
>        printf ("An error occurred while initializing ENet.\n");
>        goto END;
>    }
>        ENetHost* client        = enet_host_create ( NULL, 1, 2, 57600 / 8,
> 14400 / 8);
>        if(client == 0l)
>        {
>                printf("An error occurred while trying to create an ENet
> server host.\n");
>                goto END;
>        }
>        ENetAddress address;
>        enet_address_set_host(&address, "localhost");
>        address.port    = 1234;
>
>        ENetPeer* peer  = enet_host_connect(client, &address, 2, 0);
>        if(peer == 0l)
>        {
>                printf("No available peers for initiating an ENet
> connection.\n");
>                goto END;
>        }
>
>        ENetEvent event;
>        if (enet_host_service (client, & event, 5000) > 0 && event.type ==
> ENET_EVENT_TYPE_CONNECT)
>        {
>                puts ("Connection to localhost:1234 succeeded.");
>        }
>        else
>        {
>                enet_peer_reset (peer);
>
>                puts ("Connection to localhost:1234 failed.");
>                goto END;
>        }
>
>        printf("Input some data which will be sent to server...\n");
>
> INPUT_DATA:
>        buffer.clear();
>        while(true)
>        {
>                char character = getchar();
>                buffer.push_back(character);
>                if(character == '\n')
>                {
>                        break;
>                }
>        }
>        buffer.push_back('\0');
>
>        ENetPacket * packet = enet_packet_create(&buffer[0], buffer.size(),
> ENET_PACKET_FLAG_RELIABLE);
>        enet_peer_send (peer, 0, packet);
>        enet_host_flush(client);
>        goto INPUT_DATA;
>
> END:
>        getchar();
>
>        return 0;
> }
> _______________________________________________
> ENet-discuss mailing list
> ENet-discuss at cubik.org
> http://lists.cubik.org/mailman/listinfo/enet-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cubik.org/pipermail/enet-discuss/attachments/20101105/a1fd4f09/attachment.html>


More information about the ENet-discuss mailing list