Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » ENet Chat Sample

This thread is locked; no one can reply to it. rss feed Print
ENet Chat Sample
Nakhash
Member #8,267
January 2007
avatar

Does anyone know if there is an incrediably simple chat program for Enet in Dev-C++?
I've been piecing together blocks of code and they don't seem to do anything.

If your curious my code is below

All the Server and Client code does is sit there. I'd like it to send a packet saying "HI" to the Server. Or at the very least tell the Server that it is connected to it.

My client code is

1int main (int argc, char ** argv) {
2 init();
3 if (enet_initialize() != 0) {
4 textout_centre_ex(screen, font, "Init Error.", SCREEN_W/2,SCREEN_H/3,makecol(0,255,0),makecol(0,0,0));
5 return 1;
6 }
7 
8 client = enet_host_create (NULL /* create a client host */,
9 1 /* only allow 1 outgoing connection */,
10 57600 / 8 /* 56K modem with 56 Kbps downstream bandwidth */,
11 14400 / 8 /* 56K modem with 14 Kbps upstream bandwidth */);
12 
13 if (client == NULL)
14 {
15 fprintf (stderr,
16 "An error occurred while trying to create an ENet client host.\n");
17 exit (EXIT_FAILURE);
18 }
19 ConnectS();
20///////
21 
22 
23 
24/////
25 while(!key[KEY_ESC])
26 {
27 rect(screen,0,0,1000,1000,makecol(0,0,0));
28 PaintStr("Client Started",100,90,makecol(0,255,0),"");
29 PaintStr("Press Esc to End Program",100,100,makecol(0,255,0),"");
30 SendPacket();
31 PaintStr("Server says: %s",50,150,makecol(0,255,0),InText);
32 PaintStr("Client says: %s",50,170,makecol(0,255,0),OutText);
33 }
34 
35 return 0;
36 
37}
38END_OF_MAIN();
39 
40void ConnectS(){
41client = enet_host_create (NULL, 1, 5, 5); /* the last two parameters are bandwidth limits, see docs */
42enet_address_set_host (& address, "192.168.1.101");
43address.port = 12345;
44 
45peer = enet_host_connect (client, & address, 5);
46}
47void SendPacket(){
48ENetPacket *packet; packet = enet_packet_create("HI", 2, ENET_PACKET_FLAG_RELIABLE);
49enet_peer_send(peer, 0, packet);
50}
51 
52void init() {
53 int depth, res;
54 allegro_init();
55 depth = desktop_color_depth();
56 if (depth == 0) depth = 32;
57 set_color_depth(depth);
58 res = set_gfx_mode(GFX_AUTODETECT | GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0);
59 if (res != 0) {
60 allegro_message(allegro_error);
61 exit(-1);
62 }
63 //install_timer();
64 install_keyboard();
65 /* add other initializations here */
66}

Any my Server Program code is

1int main (int argc, char ** argv) {
2 init();
3 
4 
5 if (enet_initialize() != 0) {
6 textout_centre_ex(screen, font, "Init Error.", SCREEN_W/2,SCREEN_H/3,makecol(0,255,0),makecol(0,0,0));
7 return 1;
8 }
9 
10 /* Bind the server to the default localhost. */
11 /* A specific host address can be specified by */
12 /* enet_address_set_host (& address, "x.x.x.x"); */
13 
14 
15 server = enet_host_create(&address, 5, 5, 0); /* the last two parameters are bandwidth limits, see docs */
16 enet_address_set_host (& address, "192.168.1.101");
17 address.port = 1234;
18 
19 if (server == NULL)
20 {
21 fprintf (stderr,
22 "An error occurred while trying to create an ENet server host.\n");
23 exit (EXIT_FAILURE);
24 }
25///////
26 
27 
28 
29/////
30 while(!key[KEY_ESC])
31 {
32 rect(screen,0,0,1000,1000,makecol(0,0,0));
33 PaintStr("Server Started",100,90,makecol(0,255,0),"");
34 PaintStr("Press Esc to End Program",100,100,makecol(0,255,0),"");
35 Commun();
36 //PaintStr("Server says: %s",50,150,makecol(0,155,100),OutText);
37 //PaintStr("Client says: %s",50,170,makecol(0,155,100),InText);
38 }
39 
40 return 0;
41 
42}
43END_OF_MAIN();
44 
45void Commun(){
46
47 /* Wait up to 1000 milliseconds for an event. */
48 while (enet_host_service (server, & event, 1000) > 0)
49 {
50 switch (event.type)
51 {
52 case ENET_EVENT_TYPE_CONNECT:
53 textprintf_ex(screen, font, 50, 50, makecol(100,255,0),-1,"A new client connected from %x:%u.\n",
54 event.peer -> address.host,
55 event.peer -> address.port);
56 
57 /* Store any relevant client information here. */
58 //event.peer -> data = "Client information";
59 
60 break;
61 
62 case ENET_EVENT_TYPE_RECEIVE:
63 textprintf_ex(screen, font, 50, 60, makecol(100,0,0),-1,"A packet of length %u containing %s was received from %s on channel %u.\n",
64 event.packet -> dataLength,
65 event.packet -> data,
66 event.peer -> data,
67 event.channelID);
68 
69
70 /* Clean up the packet now that we're done using it. */
71 enet_packet_destroy (event.packet);
72
73 break;
74
75 case ENET_EVENT_TYPE_DISCONNECT:
76 textprintf_ex(screen, font, 50, 70, makecol(100,0,0),-1,"%s disconected.\n", event.peer -> data);
77 
78 /* Reset the peer's client information. */
79 
80 event.peer -> data = NULL;
81 }
82 }
83
84}
85void init() {
86 int depth, res;
87 allegro_init();
88 depth = desktop_color_depth();
89 if (depth == 0) depth = 32;
90 set_color_depth(depth);
91 res = set_gfx_mode(GFX_AUTODETECT | GFX_AUTODETECT_WINDOWED, 800, 600, 0, 0);
92 if (res != 0) {
93 allegro_message(allegro_error);
94 exit(-1);
95 }
96 //install_timer();
97 install_keyboard();
98 /* add other initializations here */
99}

Nakhash

If you like Dragon Warrior, then try out my trilogy at http://dragonwarriorreborn.com

ngiacomelli
Member #5,114
October 2004

I've never used ENet and cannot help you, but you're more likely to get a response if you wrap your code up in [CODE] tags (where code is lowercase).

Matthew Leverton
Supreme Loser
January 1999
avatar

I don't have the time right now to look at your code, but you may want to check out my Rebels and Lords game which uses enet for in game chat along with net play. (Note that the proxy component isn't included nor is it running at the moment, so you won't be able to play with it.)

bamccaig
Member #7,536
July 2006
avatar

I don't even know what ENet is, but I wrote a very simple client and server set up a few months ago with raw sockets... IIRC, I wrote it to be cross-platform, but only ever tested it in Windows... Even if the socket code is cross-platform there is probably issues with data type sizes across platforms. You can find a copy (I'm not sure if it's the most up to date) here. It worked during my tests, at least. As always, use at own risk. :P

usage: p2p-chat-server port=#
usage: p2p-chat-client [-v] ip=#.#.#.#:# <your message here>

Basically the server would start up and wait for a client to connect/send a message. Then the server would write the client's message to stdout. The client would send a single message each time it connected. The client could terminate the server by sending the message "quit".

Calling them p2p-chat is a little optimistic. It's really just a very simple message passing client/server.

Sample output...

1prompt>p2p-chat-server
2usage: p2p-chat-server port=#
3Press [Enter] to continue...
4prompt>p2p-chat-server port=27015
5Port: 27015
6WSAStartup successful!
7Socket created successfully!
8Socket bound to address: 0.0.0.0:27015
9Listening for connection...
10Connected to client...
11Message received from client:
12 This is a test message.
13Connected to client...
14Message received from client:
15 This is an unquoted test message.
16Connected to client...
17Message received from client:
18 quit
19WSACleanup...
20Press [Enter] to continue...

1prompt>p2p-chat-client
2usage: p2p-chat-client [-v] ip=#.#.#.#:# message
3Press [Enter] to continue...
4prompt>p2p-chat-client -v ip=127.0.0.1:27015 "This is a test message."
5Connected to server: 127.0.0.1:27015
6Message sent:
7 This is a test message.
8Press [Enter] to continue...
9prompt>p2p-chat-client -v ip=127.0.0.1:27015 This is an unquoted test message.
10Connected to server: 127.0.0.1:27015
11Message sent:
12 This is an unquoted test message.
13Press [Enter] to continue...
14prompt>p2p-chat-client -v ip=127.0.0.1:27015 quit
15Connected to server: 127.0.0.1:27015
16Message sent:
17 quit
18Press [Enter] to continue...

The code is pretty ugly... :-X

Nakhash
Member #8,267
January 2007
avatar

Well I threw HawkNL away, apparently I did something to the code, and now I have no idea how to put it back to normal.

I guess what I'm asking is if anyone has a simple client/server program in Enet that shows the client connecting to the server, and both the client and the server sending a number back and forth would be incredible. I'm looking at creating an online game that is similar to my Dragon Warrior Reborn Trilogy. I'm not sure how I could repay anyone for there help. Maybe I could name a character in my game after you or something. :)

If the program ran in Dev-C++ then I'd have no issue at all compiling it.

I know I'm asking a lot, but I don't know where else to turn.:-/

[Edit]

Ok I figured it out. Below is the code that I used.

Server Code is

#SelectExpand
1#include <allegro.h> 2#include <winalleg.h> 3#include <stdlib.h> 4#include <stdio.h> 5 6#include <winsock2.h> 7#include <enet.h> 8 9 10#define WIN32_LEAN_AND_MEAN 11#define ALLEGRO_NO_MAGIC_MAIN 12#define MAX_CLIENTS 3 13 14#include <string.h> 15using namespace std; 16 17void ServerLoop(); 18void SendPacket(int Channel, char pack[11]); 19void InitNetwork(); 20 21 22 ENetEvent event; 23 ENetHost * host=0; 24 ENetPeer * peer=0; 25 ENetAddress address; 26 27//--- main 28//--------------------------------------------------------- 29 30void main () { 31 32InitNetwork(); 33 34int run=1; 35 while (run==1){ 36 ServerLoop(); 37 38 } 39} 40END_OF_MAIN(); 41void ServerLoop(){ 42 43 // processing incoming events: 44 while (enet_host_service (host, &event, 1000) > 0) { 45 printf("Checking Incoming"); 46 switch (event.type) { 47 case ENET_EVENT_TYPE_CONNECT: { 48 printf(" A new connected has been established to %u:%u\n", 49 event.peer -> address.host,event.peer-> address.port); 50 fflush(stdout); 51 52 char buf[64]; 53 sprintf(buf,"%u:%u",event.peer -> address.host, event.peer -> address.port); 54 int buflen=strlen(buf); 55 event.peer -> data=malloc(buflen+1); 56 strncpy((char*)event.peer -> data,buf,buflen); 57 peer=event.peer; 58 break; 59 } 60 case ENET_EVENT_TYPE_RECEIVE: 61 62 printf("%s says %s on channel %u\n", 63 (char*)event.peer -> data,event.packet -> data,event.channelID); 64 fflush(stdout); 65 66 enet_packet_destroy (event.packet); // clean up the packet now that we're done using it 67 68 SendPacket(0,"Data Received") ; 69 break; 70 71 case ENET_EVENT_TYPE_DISCONNECT: 72 printf(" host disconnected.\n"); 73 fflush(stdout); 74 free(event.peer -> data); 75 event.peer -> data = 0; // reset the peer's client information. 76 peer=0; 77 default: 78 break; 79 } 80 } 81} 82 83void SendPacket(int Channel, char pack[11]) 84{ 85 /* Create a reliable packet of size 7 containing "packet\0" */ 86 ENetPacket * packet = enet_packet_create (pack, 87 strlen (pack) + 1, 88 ENET_PACKET_FLAG_RELIABLE); 89 90 91 /* Send the packet to the peer over channel id 0. */ 92 /* One could also broadcast the packet by */ 93 /* enet_host_broadcast (host, 0, packet); */ 94 enet_peer_send (peer, Channel, packet); 95 /* One could just use enet_host_service() instead. */ 96 enet_host_flush (host); 97 98} 99void InitNetwork(){ 100 if (enet_initialize () != 0) 101 { 102 fprintf (stderr, "An error occurred while initializing ENet.\n"); 103 } 104 atexit (enet_deinitialize); 105 106 printf("Initialized.\n"); 107 108 109 /* Bind the server to the default localhost. */ 110 /* A specific host address can be specified by */ 111 /* enet_address_set_host (& address, "x.x.x.x"); */ 112 113 address.port = 1234; 114 115 printf("I am server...\n"); 116 fflush(stdout); 117 118 address.host = ENET_HOST_ANY; 119 host = enet_host_create (&address, // the address to bind the server host to 120 1, // allow only 1 client and/or outgoing connections 121 0, // assume any amount of incoming bandwidth 122 0); // assume any amount of outgoing bandwidth 123 if (!host) { 124 fprintf(stderr,"An error occurred while trying to create an ENet server host.\n"); 125 exit (EXIT_FAILURE); 126 } 127 printf("Host Created at %s.\n",address.host); 128 129 130}

Client code is

#SelectExpand
1 2#include <allegro.h> 3#include <winalleg.h> 4 5#include <stdlib.h> 6#include <stdio.h> 7 8#include <winsock2.h> 9#include <enet.h> 10 11 12#define WIN32_LEAN_AND_MEAN 13#define ALLEGRO_NO_MAGIC_MAIN 14#define MAX_CLIENTS 3 15 16#include <string.h> 17using namespace std; 18 19void ClientLoop(); 20void SendPacket(int Channel, char pack[11]); 21void InitNetwork(); 22 23 24 ENetEvent event; 25 ENetHost * host=0; 26 ENetPeer * peer=0; 27 ENetAddress address; 28 29//--- main 30//--------------------------------------------------------- 31 32void main () { 33 34InitNetwork(); 35 36int run=1; 37 printf("Checking Incoming\n"); 38 while (run==1){ 39 ClientLoop(); 40 SendPacket(0,"Hello"); 41 } 42} 43END_OF_MAIN(); 44 45void ClientLoop(){ 46 47 // processing incoming events: 48 while (enet_host_service (host, &event, 1000) > 0) { 49 switch (event.type) { 50 case ENET_EVENT_TYPE_CONNECT: { 51 printf(" A new connected has been established to %u:%u\n", 52 event.peer -> address.host,event.peer-> address.port); 53 fflush(stdout); 54 55 char buf[64]; 56 sprintf(buf,"%u:%u",event.peer -> address.host, event.peer -> address.port); 57 int buflen=strlen(buf); 58 event.peer -> data=malloc(buflen+1); 59 strncpy((char*)event.peer -> data,buf,buflen); 60 peer=event.peer; 61 break; 62 } 63 case ENET_EVENT_TYPE_RECEIVE: 64 65 printf("%s says %s on channel %u\n", 66 (char*)event.peer -> data,event.packet -> data,event.channelID); 67 fflush(stdout); 68 69 enet_packet_destroy (event.packet); // clean up the packet now that we're done using it 70 break; 71 72 case ENET_EVENT_TYPE_DISCONNECT: 73 printf(" host disconnected.\n"); 74 fflush(stdout); 75 free(event.peer -> data); 76 event.peer -> data = 0; // reset the peer's client information. 77 peer=0; 78 default: 79 break; 80 } 81 } 82} 83 84void SendPacket(int Channel, char pack[11]) 85{ 86 /* Create a reliable packet of size 7 containing "packet\0" */ 87 ENetPacket * packet = enet_packet_create (pack, 88 strlen (pack) + 1, 89 ENET_PACKET_FLAG_RELIABLE); 90 91 92 /* Send the packet to the peer over channel id 0. */ 93 /* One could also broadcast the packet by */ 94 /* enet_host_broadcast (host, 0, packet); */ 95 enet_peer_send (peer, Channel, packet); 96 /* One could just use enet_host_service() instead. */ 97 enet_host_flush (host); 98 99} 100void InitNetwork(){ 101 102if (enet_initialize () != 0) 103{ 104 fprintf (stderr, "An error occurred while initializing ENet.\n"); 105} 106atexit (enet_deinitialize); 107 108 printf("Initialized.\n"); 109 110 address.port = 1234; 111 112 printf("I am client...\n"); 113 fflush(stdout); 114 115 host = enet_host_create (0, // create a client host 116 1, // allow only 1 outgoing connection 117 0, // use 57600 / 8 for 56K modem with 56 Kbps downstream bandwidth 118 0);// use 14400 / 8 for 56K modem with 14 Kbps upstream bandwidth 119 120 if (!host) { 121 fprintf(stderr,"An error occurred while trying to create an ENet client host.\n"); 122 exit (EXIT_FAILURE); 123 } 124 125 // connect to server: 126 enet_address_set_host (&address, ""); 127 peer = enet_host_connect (host, &address, 2); 128 peer->data=0; // use this as mark that connection is not yet acknowledged 129 if (!peer) { 130 fprintf(stderr,"No available peers for initiating an ENet connection.\n"); 131 exit (EXIT_FAILURE); 132 } 133 134}

To install Enet for Dev-C++ you should go to Tools in the Menu and go to check for updates then change the site to Devpaks.org scroll down to networking and download Enet.

After that create two new empty projects (1 for client, 1 for server) then in both projects go into Project in the Menu and Options then go to parameters then in the linker box type

--no-export-all-symbols --add-stdcall-alias
enet.a
../../../../Dev-Cpp/lib/libws2_32.a
../../../../Dev-Cpp/lib/libwinmm.a
../../../../Dev-Cpp/lib/libwsock32.a
-lalleg

after that compile and your good to go. I decided to post this short tutorial with code on how I was able to use Enet, since I could not find any code that was simplified.

Nakhash

If you like Dragon Warrior, then try out my trilogy at http://dragonwarriorreborn.com

bamccaig
Member #7,536
July 2006
avatar

GullRaDriel
Member #3,861
September 2003
avatar

Using a Lamborghini where you could have used a bike is not nice. It's overloaded.

"Code is like shit - it only smells if it is not yours"
Allegro Wiki, full of examples and articles !!

Go to: