Jump to content

  • Log In with Google      Sign In   
  • Create Account

#Actualrip-off

Posted 09 January 2013 - 08:33 AM

post my code first 
please help me to point out where is wrong?
 
client code :
</div>
<div>int send_to_peer(ENETHost* client, ENETPeer *peer)</div>
<div>{</div>
<div>   i</div>
<div>    ENETPacket *packet=ENET_packet_create(NULL,128,ENET_PACKET_FLAG_RELIABLE);</div>
<div>    if(NULL == packet){</div>
<div>        fprintf(stderr,"create packet with error\n");</div>
<div>        exit(EXIT_FAILURE);</div>
<div>    }</div>
<div>    char* d="client say";</div>
<div>    strncpy(&packet->data,d,strlen(d));</div>
<div>    ENET_peer_send(peer,1,packet);</div>
<div>    // ENET_host_broadcast(client,1,packet);</div>
<div>    // send data out</div>
<div>    ENET_host_flush(client);</div>
<div> </div>
<div>    // close connection</div>
<div>    ENET_peer_disconnect(peer,0);</div>
<div> </div>
<div>    // wait for close with success</div>
<div>    ENETEvent event;</div>
<div> </div>
<div>    while(ENET_host_service(client,&event,3000)>0){</div>
<div>        switch(event.type){</div>
<div>        case ENET_EVENT_TYPE_NONE:</div>
<div>            break;</div>
<div>        case ENET_EVENT_TYPE_RECEIVE:</div>
<div>            ENET_packet_destroy(packet);</div>
<div>            break;</div>
<div>        case ENET_EVENT_TYPE_DISCONNECT:</div>
<div>            printf("has disconnected\n");</div>
<div>            ENET_destruct();</div>
<div>            exit(EXIT_SUCCESS);</div>
<div>            break;</div>
<div>        }</div>
<div>    }</div>
<div> </div>
<div>    // force to close socket if unsuccess</div>
<div>    ENET_peer_reset(peer);</div>
<div>    printf("force disconnected\n");</div>
<div> </div>
<div>    ENET_destruct();</div>
<div>}</div>
<div>
 
 
SERVER:
</div>
<div>int start_daemon_services(ENETHost* server)</div>
<div>{</div>
<div>    ENETEvent event;</div>
<div> </div>
<div>    while(1){</div>
<div>        while(ENET_host_service(server,&event,5000)>0){</div>
<div>            switch(event.type){</div>
<div>            case ENET_EVENT_TYPE_NONE:</div>
<div> </div>
<div>                break;</div>
<div>            case ENET_EVENT_TYPE_CONNECT:</div>
<div>               printf("a new client has connected,remote host address is:  %s:%u\n",</div>
<div>                        inet_ntoa(*(struct in_addr*)&event.peer->address.host),</div>
<div>                        event.peer->address.port);</div>
<div>                event.peer->data="incoming client";</div>
<div>                break;</div>
<div>            case ENET_EVENT_TYPE_RECEIVE:</div>
<div>                printf("received package from %s\n on channel %u\tpacket size:%u\tpacket content:%s\n",</div>
<div>                       event.peer->data,</div>
<div>                       event.peer->connectID,</div>
<div>                       event.packet->dataLength,</div>
<div>                       event.packet->data);</div>
<div>                //do with package</div>
<div> </div>
<div>                //destroty it</div>
<div>                ENET_packet_destroy(event.packet);</div>
<div>                break;</div>
<div>            case ENET_EVENT_TYPE_DISCONNECT:</div>
<div>                break;</div>
<div>            default:</div>
<div>                break;</div>
<div>            }</div>
<div>        }</div>
<div>    }</div>
<div>    //reset all peers connected to server</div>
<div>    ENET_host_destroy(server);</div>
<div>}</div>
<div>

#1hsine

Posted 09 January 2013 - 07:51 AM

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);
}
 

PARTNERS