[ENet-discuss] why can't send and receice ?

enetcub enet_club at 126.com
Wed Jan 9 05:49:22 PST 2013


enet-discusshi all:

post my code first 
please help me to point out where is wrong?

i 'v use something net capture tools to watch the data transfer but nothing found 



client code :
------------------------------------
int send_to_peer(ENETHost* client, ENETPeer *peer)
{
   i
    ENETPacket *packet=ENET_packet_create(NULL,128,ENET_PACKET_FLAG_RELIABLE);
    if(NULL == packet){
        fprintf(stderr,"create packet with error\n");
        exit(EXIT_FAILURE);
    }
    char* d="client say";
    strncpy(&packet->data,d,strlen(d));
    ENET_peer_send(peer,1,packet);
    // ENET_host_broadcast(client,1,packet);
    // send data out
    ENET_host_flush(client);


    // close connection
    ENET_peer_disconnect(peer,0);


    // wait for close with success
    ENETEvent event;


    while(ENET_host_service(client,&event,3000)>0){
        switch(event.type){
        case ENET_EVENT_TYPE_NONE:
            break;
        case ENET_EVENT_TYPE_RECEIVE:
            ENET_packet_destroy(packet);
            break;
        case ENET_EVENT_TYPE_DISCONNECT:
            printf("has disconnected\n");
            ENET_destruct();
            exit(EXIT_SUCCESS);
            break;
        }
    }


    // force to close socket if unsuccess
    ENET_peer_reset(peer);
    printf("force disconnected\n");


    ENET_destruct();
}






SERVER:
-------------------------------


int start_daemon_services(ENETHost* server)
{
    ENETEvent event;


    while(1){
        while(ENET_host_service(server,&event,5000)>0){
            switch(event.type){
            case ENET_EVENT_TYPE_NONE:


                break;
            case ENET_EVENT_TYPE_CONNECT:
               printf("a new client has connected,remote host address is:  %s:%u\n",
                        inet_ntoa(*(struct in_addr*)&event.peer->address.host),
                        event.peer->address.port);
                event.peer->data="incoming client";
                break;
            case ENET_EVENT_TYPE_RECEIVE:
                printf("received package from %s\n on channel %u\tpacket size:%u\tpacket content:%s\n",
                       event.peer->data,
                       event.peer->connectID,
                       event.packet->dataLength,
                       event.packet->data);
                //do with package


                //destroty it
                ENET_packet_destroy(event.packet);
                break;
            case ENET_EVENT_TYPE_DISCONNECT:
                break;
            default:
                break;
            }
        }
    }
    //reset all peers connected to server
    ENET_host_destroy(server);
}




enetcub,enet_club at 126.com
2013-01-09 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cubik.org/pipermail/enet-discuss/attachments/20130109/41cb08e6/attachment-0001.html>


More information about the ENet-discuss mailing list