• Advertisement
Sign in to follow this  

why i can't send or receive anything ....

This topic is 1841 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

post my code first 
please help me to point out where is wrong?
 
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);
}
Edited by rip-off

Share this post


Link to post
Share on other sites
Advertisement

Can you post any error message(s) or describe the expected and erroneous behaviour that you are observing?

 

Please use [ code ] tags (without spaces) around your source code, it will render it in a nice, syntax highlighted box for ease of viewing.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement