[ENet-discuss] client-server doesn't work
Shawn Yarbrough
shawnyar217 at yahoo.com
Thu Dec 13 05:44:32 PST 2007
Hi,
This is my first try with ENet, enet-1.1 stable release, on Linux.
My client-server ENet test programs aren't working as expected. The client connects to the server and sees a connection event, but the server never sees the connection event.
I've included sample runs and code below. If I'm doing something boneheaded, I was hoping somebody could point out my mistake.
Also I was looking thru the ENet source code trying to understand it. It looks like the UNIX accept() function is never called, or actually it is called by enet_socket_accept(), but then enet_socket_accept() is never called. I assume I'm missing something. I'm not supposed to call the ENet socket functions myself, am I? The tutorial doesn't mention them at all.
Regards,
Shawn Yarbrough
$ ./enet_server
listening on: [0.0.0.0:1234]
accepting
accepting
(NOTE: here is where ./enet_client was run!)
accepting
accepting
accepting
accepting
event:
unexpected event type: 0
accepting
$ ./enet_client
connecting to: [127.0.0.1:1234]
connecting
connected to: [127.0.0.1:1234]
// enet_client.cpp
#include <iostream>
#include <sstream>
#include <unistd.h>
#include "enet/enet.h"
bool enet_initialized = false;
::ENetHost *enethost;
void
check_enet_initialized()
{
// ENet initialization.
if( !enet_initialized )
{
if( ::enet_initialize() != 0 )
throw "failed to initialize ENet";
enet_initialized = true;
atexit( ::enet_deinitialize );
}
}
void
connect_enet()
{
check_enet_initialized();
// Initiate the ENet connection.
enethost = ::enet_host_create( NULL, // don't bind as a server
2, // one connection, only
0, // default incoming bandwidth
0 ); // default outgoing bandwidth
if( enethost == NULL )
throw "failed to open ENet connection";
::ENetAddress remote_address;
::enet_address_set_host( &remote_address, "127.0.0.1" );
remote_address.port = 1234;
::ENetPeer *enetpeer;
enetpeer = ::enet_host_connect( enethost, &remote_address, 2 );
if( enetpeer == NULL )
{
::enet_host_destroy( enethost );
throw "no available ENet peers";
}
char buf[256];
::enet_address_get_host_ip( &remote_address, buf, sizeof(buf) );
std::cout << "connecting to: "
<< "[" << buf << ":" << remote_address.port << "]"
<< std::endl;
////
ENetEvent event;
doconnect:
std::cout << "connecting"
<< std::endl;
while( ::enet_host_service( enethost, &event, 1000 ) > 0 )
{
switch( event.type )
{
case ENET_EVENT_TYPE_CONNECT:
{
char buf[256];
::enet_address_get_host_ip( &event.peer->address, buf, sizeof(buf) );
std::stringstream ss;
ss << buf
<< ":"
<< event.peer->address.port;
std::cout << "connected to: "
<< "[" << ss.str() << "]"
<< std::endl;
event.peer->data = new std::string(ss.str());
goto connected;
}
case ENET_EVENT_TYPE_DISCONNECT:
{
throw "unexpected disconnection received on connecting socket";
}
case ENET_EVENT_TYPE_RECEIVE:
{
throw "unexpected data packet received on connecting socket";
}
}
}
goto doconnect;
////
connected:;
}
int
main( int argc, char *argv[] )
{
connect_enet();
return 0;
}
// enet_server.cpp
#include <iostream>
#include <sstream>
#include <unistd.h>
#include "enet/enet.h"
bool enet_initialized = false;
::ENetHost *enethost;
void
check_enet_initialized()
{
// ENet initialization.
if( !enet_initialized )
{
if( ::enet_initialize() != 0 )
throw "failed to initialize ENet";
enet_initialized = true;
atexit( ::enet_deinitialize );
}
}
void
listen_enet()
{
check_enet_initialized();
// Initiate the ENet connection.
::ENetAddress local_address;
local_address.host = ENET_HOST_ANY;
local_address.port = 1234;
enethost = ::enet_host_create( &local_address, // bind as a server
32, // max number of connections allowed
0, // default incoming bandwidth
0 ); // default outgoing bandwidth
if( enethost == NULL )
throw "failed to open ENet connection";
char buf[256];
::enet_address_get_host_ip( &local_address, buf, sizeof(buf) );
std::cout << "listening on: "
<< "[" << buf << ":" << local_address.port << "]"
<< std::endl;
}
void
accept_enet()
{
ENetEvent event;
doaccept:
std::cout << "accepting"
<< std::endl;
while( ::enet_host_service( enethost, &event, 10000 ) > 0 )
{
std::cout << "event:" << std::endl;
switch( event.type )
{
case ENET_EVENT_TYPE_CONNECT:
{
char buf[256];
::enet_address_get_host_ip( &event.peer->address, buf, sizeof(buf) );
std::stringstream ss;
ss << buf
<< ":"
<< event.peer->address.port;
std::cout << "connection from: "
<< "[" << ss.str() << "]"
<< std::endl;
event.peer->data = new std::string(ss.str());
return;
}
case ENET_EVENT_TYPE_DISCONNECT:
{
throw "unexpected disconnection received on listen socket";
}
case ENET_EVENT_TYPE_RECEIVE:
{
throw "unexpected data packet received on listen socket";
}
default: std::cout << "unexpected event type: " << event.type << std::endl;
}
}
goto doaccept;
}
int
main( int argc, char *argv[] )
{
listen_enet();
accept_enet();
return 0;
}
____________________________________________________________________________________
Be a better friend, newshound, and
know-it-all with Yahoo! Mobile. Try it now. http://mobile.yahoo.com/;_ylt=Ahu06i62sR8HDtDypao8Wcj9tAcJ
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.cubik.org/pipermail/enet-discuss/attachments/20071213/769ab5bd/attachment-0001.htm
More information about the ENet-discuss
mailing list