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

Вячеслав Блинников slavmfm at gmail.com
Fri Nov 5 07:50:36 PDT 2010


Thank you very much! It works now!
Is it right calling that function in the separate thread? Like that:

void* ENetHostService(void* client)
{
	ENetHost* castClient	= (ENetHost*)client;
	ENetEvent event;

ENET_HOST_SERVICE:
	enet_host_service(castClient, &event, 1000);
	goto ENET_HOST_SERVICE;
}

But I think I have some lags in messages sending - what is the point
in third param to enet_host_service()? Does not this function returns
(do it's job (message sending in our case)) just in moment when
enet_peer_send() where called? Or it will wait 1000 milliseconds
anyway?

So, am I right in the whole structure (calling enet_host_service()
permanently in different thread) or there are better ways? And is it
alrignt thet I do not use mutexes when call enet_peer_send()?

5 ноября 2010 г. 17:10 пользователь Nuno Silva
<little.coding.fox at gmail.com> написал:
> 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
>
>
> _______________________________________________
> ENet-discuss mailing list
> ENet-discuss at cubik.org
> http://lists.cubik.org/mailman/listinfo/enet-discuss
>
>


More information about the ENet-discuss mailing list