[ENet-discuss] simple program
Leon
kuba at et.pl
Wed Jul 20 13:57:09 PDT 2005
Hi. I've got problem with my program.. Look just at it:
/* cc -o q q.c -lenet */
#include <stdio.h>
#include <enet/enet.h>
#define error(frm, arg...) \
do { fprintf(stderr, "error: " frm "\n", ##arg); exit(1); }
while(0)
#define debug(frm, arg...) \
do { fprintf(stderr, "debug: " frm "\n", ##arg); } while(0)
void qsend(ENetAddress *xaddr, ENetAddress *yaddr)
{
ENetHost *client = enet_host_create (xaddr, 1, 0, 0);
if (client == NULL)
error("enet_host_create(client)");
ENetPeer *peer = enet_host_connect (client, yaddr, 1);
if(peer == NULL)
error("enet_host_connect()");
ENetEvent event;
if (enet_host_service (client, &event, 5000) > 0
&& event.type == ENET_EVENT_TYPE_CONNECT) {
debug ("Connection succeeded.");
} else {
enet_peer_reset (peer);
error ("Connection failed.");
}
int last_byte = 0;
#define QBUFF 32
char buff[QBUFF];
do {
int i;
for (i=0; i<QBUFF; i++) {
int c = getchar();
if(c == EOF) { last_byte = 1; break; }
buff[i] = c;
}
ENetPacket *packet = enet_packet_create (buff, i,
ENET_PACKET_FLAG_RELIABLE);
if (enet_peer_send(peer, 0, packet))
error("enet_peer_send()");
enet_host_flush (client);
usleep(1000);
} while(!last_byte);
/* disconnecting */
enet_peer_disconnect (& client -> peers [0]);
while (enet_host_service (client, & event, 3000) > 0) {
switch (event.type) {
case ENET_EVENT_TYPE_RECEIVE:
enet_packet_destroy (event.packet);
break;
case ENET_EVENT_TYPE_DISCONNECT:
debug ("Disconnection succeeded.");
return;
}
}
enet_peer_reset (& client -> peers [0]);
enet_host_destroy(client);
}
void qrecv(ENetAddress *xaddr, ENetAddress *yaddr)
{
ENetHost *server = enet_host_create(xaddr, 1, 0, 0);
if(server == NULL)
error("enet_host_create()");
ENetEvent event;
while ( enet_host_service(server, &event, 10000) > 0) {
switch (event.type) {
case ENET_EVENT_TYPE_CONNECT:
debug ("A new client connected from %x:%u.\n",
event.peer -> address.host,
event.peer -> address.port);
break;
case ENET_EVENT_TYPE_RECEIVE:
{
int i;
for(i=0; i<event.packet->dataLength;i++)
putchar(event.packet->data[i]);
enet_packet_destroy (event.packet);
break;
}
case ENET_EVENT_TYPE_DISCONNECT:
debug ("%s disconected.\n", event.peer -> data);
event.peer -> data = NULL;
break;
}
}
enet_host_destroy(server);
}
int main(int argc, char **argv)
{
if(argc<2)
error("usage: %s [send, receive]", argv[0]);
if (enet_initialize())
error("enet_initialize()");
ENetAddress xaddr;
ENetAddress yaddr;
xaddr.host = ENET_HOST_ANY;
enet_address_set_host(&yaddr, "localhost");
if(argv[1][0] == 'r') {
/* we want to receive */
xaddr.port = 3000;
yaddr.port = 2000;
qrecv(&xaddr, &yaddr);
} else {
/* we want to send sth */
xaddr.port = 2000;
yaddr.port = 3000;
qsend(&xaddr, &yaddr);
}
enet_deinitialize();
return 0;
}
so i run two programs just like this:
./q receice > file
and
cat some_file | ./q send
and it always stops after about 30Kbytes.
usleep(1000); <-- look here
^^ I've been trying to change this value, but i didn't help.
I've also been trying to chagne QBUFF size :(
What i'm doing wrong?
Thanks!
--
Leon
www.kuba.et.pl
More information about the ENet-discuss
mailing list