Advertisement

Simple enet program fails when client is iOS works fine when testing from desktop

Started by August 16, 2016 07:15 PM
1 comment, last by _WeirdCat_ 8 years, 3 months ago

Hello all

i
I have strange ( or not ) connection error
When i try to connect the server which is running from my xCode . and Iphone app is running from xcode also .
The line :

if (enet_host_service(client, &event, 1000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT)

Dosn't pass and jumps to :

enet_peer_reset(peer);

BUT when i run this code both from my desktop as simple client/server app , every thing is working just fine .
here is the code which i take from some internet site .
notice i set the HOST to be my Mac : echo $HOSTNAME
mybe this is the problem and the iPhone can't connect my Mac .. i dont know
how can i check this ?
i also found this old post which somehow related
http://www.gamedev.net/topic/601017-resolved-simple-enet-program-fails/

i tried to change the connection address to every thing i could think about
localhost , hostname , ip's from
ifconfig -a

f


config -a
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> mtu 16384
options=3<RXCSUM,TXCSUM>
inet6 ::1 prefixlen 128 
inet 127.0.0.1 netmask 0xff000000 
inet6 fe80::1%lo0 prefixlen 64 scopeid 0x1 
nd6 options=1<PERFORMNUD>
gif0: flags=8010<POINTOPOINT,MULTICAST> mtu 1280
stf0: flags=0<> mtu 1280
en0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
options=10b<RXCSUM,TXCSUM,VLAN_HWTAGGING,AV>
ether a8:20:66:11:88:28 
inet6 fe80::aa20:66ff:fe11:8828%en0 prefixlen 64 scopeid 0x4 
inet 192.168.1.25 netmask 0xffffff00 broadcast 192.168.1.255
nd6 options=1<PERFORMNUD>
media: autoselect (1000baseT <full-duplex,flow-control,energy-efficient-ethernet>)
status: active
en1: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
ether 20:c9:d0:8f:6b:1f 
nd6 options=1<PERFORMNUD>
media: autoselect (<unknown type>)
status: inactive
en2: flags=963<UP,BROADCAST,SMART,RUNNING,PROMISC,SIMPLEX> mtu 1500
options=60<TSO4,TSO6>
ether 32:00:19:4f:1e:60 
media: autoselect <full-duplex>
status: inactive
fw0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 4078
lladdr 00:3e:e1:ff:fe:94:f1:e6 
nd6 options=1<PERFORMNUD>
media: autoselect <full-duplex>
status: inactive
p2p0: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> mtu 2304
ether 02:c9:d0:8f:6b:1f 
media: autoselect
status: inactive
awdl0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> mtu 1484
ether 12:e7:cd:9c:c3:d2 
inet6 fe80::10e7:cdff:fe9c:c3d2%awdl0 prefixlen 64 scopeid 0x9 
nd6 options=1<PERFORMNUD>
media: autoselect
status: active
bridge0: flags=8863<UP,BROADCAST,SMART,RUNNING,SIMPLEX,MULTICAST> mtu 1500
options=63<RXCSUM,TXCSUM,TSO4,TSO6>
ether aa:20:66:11:a0:00 
Configuration:
id 0:0:0:0:0:0 priority 0 hellotime 0 fwddelay 0
maxage 0 holdcnt 0 proto stp maxaddr 100 timeout 1200
root id 0:0:0:0:0:0 priority 0 ifcost 0 port 0
ipfilter disabled flags 0x2
member: en2 flags=3<LEARNING,DISCOVER>
        ifmaxaddr 0 port 6 priority 0 path cost 0
nd6 options=1<PERFORMNUD>
media: <unknown type>
status: inactive

Here is the source codes

Client desktop :

----------------------------------------------------------


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "enet/enet.h"
#define BUFFERSIZE 1024
#define PORT 9991

ENetAddress  address;
ENetHost   *server;
ENetEvent  event;
ENetPacket      *packet;

char    buffer[BUFFERSIZE];

int  main(int argc, char ** argv) {
    int  i;
    
    if (enet_initialize() != 0) {
        printf("Could not initialize enet.");
        return 0;
    }
    
    address.host = ENET_HOST_ANY;
    address.port = PORT;
    
    server = enet_host_create(&address, 100, 2, 0, 0);
    
    if (server == NULL) {
        printf("Could not start server.\n");
        return 0;
    }
    else
    {
        printf("Server Started host: %d on port:%d.\n",address.host, address.port);

    }
    
    while (1) {
        while (enet_host_service(server, &event, 1000) > 0) {
            switch (event.type) {
                    
                case ENET_EVENT_TYPE_CONNECT:
                    break;
                    
                case ENET_EVENT_TYPE_RECEIVE:
                    if (event.peer->data == NULL) {
                        event.peer->data =
                        malloc(strlen((char*) event.packet->data)+1);
                        strcpy((char*) event.peer->data, (char*)
                               event.packet->data);
                        
                        sprintf(buffer, "%s has connected\n",
                                (char*) event.packet->data);
                        packet = enet_packet_create(buffer,
                                                    strlen(buffer)+1, 0);
                        enet_host_broadcast(server, 1, packet);
                        enet_host_flush(server);
                    } else {
                        for (i=0; i<server->peerCount; i++) {
                            if (&server->peers[i] != event.peer) {
                                sprintf(buffer, "%s: %s",
                                        (char*) event.peer->data, (char*)
                                        event.packet->data);
                                packet = enet_packet_create(buffer,
                                                            strlen(buffer)+1, 0);
                                enet_peer_send(&server->peers[i], 0,
                                               packet);
                                enet_host_flush(server);
                            } else {
                                
                            }
                        }
                    }
                    break;
                    
                case ENET_EVENT_TYPE_DISCONNECT:
                    sprintf(buffer, "%s has disconnected.", (char*)
                            event.peer->data);
                    packet = enet_packet_create(buffer, strlen(buffer)+1, 0);
                    enet_host_broadcast(server, 1, packet);
                    free(event.peer->data);
                    event.peer->data = NULL;
                    break;
                    
                default:
                    printf("Tick tock.\n");
                    break;
            }
            
        }
    }
    
    enet_host_destroy(server);
    enet_deinitialize();
    return 0;
}

Client Iphone device ( part of cpp game engine ) compiles fine .

---------------------------------------------------------------------------------------------


char  buffer[BUFFERSIZE];
    
    ENetHost  *client;
    ENetAddress  address;
    ENetEvent  event;
    ENetPeer  *peer;
    ENetPacket  *packet;
    int connected=0;
    if (enet_initialize() != 0) {
        log("Could not initialize enet.\n");
        return false;
    }
    client = enet_host_create(NULL, 1, 2, 5760/8, 1440/8);
    
    if (client == NULL) {
        log("Could not create client.\n");
        return false;
    }
    
    enet_address_set_host(&address, HOST);
    address.port = PORT;
    
    peer = enet_host_connect(client, &address, 2, 0);
    
    if (peer == NULL) {
        log("Could not connect to server\n");
        return false;
    }
    
    if (enet_host_service(client, &event, 1000) > 0 &&
        event.type == ENET_EVENT_TYPE_CONNECT) {
        
        log("Connection to %s succeeded.\n", HOST);
        connected++;
        
        strncpy(buffer, "Meiry Test", BUFFERSIZE);
        packet = enet_packet_create(buffer, strlen(buffer)+1,
                                    ENET_PACKET_FLAG_RELIABLE);
        enet_peer_send(peer, 0, packet);
        
    } else {
        enet_peer_reset(peer);
        log("Could not connect to %s.\n", HOST);
        return false;
    }
    

Server

----------------------------------------------------------------


#include <stdio.h>

#include <string.h>
#include <stdlib.h>
#include "enet/enet.h"
#define BUFFERSIZE 1024
#define PORT 9991

ENetAddress  address;
ENetHost   *server;
ENetEvent  event;
ENetPacket      *packet;

char    buffer[BUFFERSIZE];

int  main(int argc, char ** argv) {
    int  i;
    
    if (enet_initialize() != 0) {
        printf("Could not initialize enet.");
        return 0;
    }
    
    address.host = ENET_HOST_ANY;
    address.port = PORT;
    
    server = enet_host_create(&address, 100, 2, 0, 0);
    
    if (server == NULL) {
        printf("Could not start server.\n");
        return 0;
    }
    else
    {
        printf("Server Started host: %d on port:%d.\n",address.host, address.port);

    }
    
    while (1) {
        while (enet_host_service(server, &event, 1000) > 0) {
            switch (event.type) {
                    
                case ENET_EVENT_TYPE_CONNECT:
                    break;
                    
                case ENET_EVENT_TYPE_RECEIVE:
                    if (event.peer->data == NULL) {
                        event.peer->data =
                        malloc(strlen((char*) event.packet->data)+1);
                        strcpy((char*) event.peer->data, (char*)
                               event.packet->data);
                        
                        sprintf(buffer, "%s has connected\n",
                                (char*) event.packet->data);
                        packet = enet_packet_create(buffer,
                                                    strlen(buffer)+1, 0);
                        enet_host_broadcast(server, 1, packet);
                        enet_host_flush(server);
                    } else {
                        for (i=0; i<server->peerCount; i++) {
                            if (&server->peers[i] != event.peer) {
                                sprintf(buffer, "%s: %s",
                                        (char*) event.peer->data, (char*)
                                        event.packet->data);
                                packet = enet_packet_create(buffer,
                                                            strlen(buffer)+1, 0);
                                enet_peer_send(&server->peers[i], 0,
                                               packet);
                                enet_host_flush(server);
                            } else {
                                
                            }
                        }
                    }
                    break;
                    
                case ENET_EVENT_TYPE_DISCONNECT:
                    sprintf(buffer, "%s has disconnected.", (char*)
                            event.peer->data);
                    packet = enet_packet_create(buffer, strlen(buffer)+1, 0);
                    enet_host_broadcast(server, 1, packet);
                    free(event.peer->data);
                    event.peer->data = NULL;
                    break;
                    
                default:
                    printf("Tick tock.\n");
                    break;
            }
            
        }
    }
    
    enet_host_destroy(server);
    enet_deinitialize();
    return 0;

}

 
Here is the code :
Can you traceroute from your iphone to your test server?
If you run a web server on the test server, can you connect to it from the browser on the iphone?
enum Bool { True, False, FileNotFound };
Advertisement

i would recommend using higher timeout value like 5 seconds or more

/* Wait up to 5 seconds for the connection attempt to succeed. */
if (enet_host_service (client, & event, 5000) > 0 && event.type == ENET_EVENT_TYPE_CONNECT)

This topic is closed to new replies.

Advertisement