|
ENet Chat Sample |
Nakhash
Member #8,267
January 2007
|
Does anyone know if there is an incrediably simple chat program for Enet in Dev-C++? 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
Any my Server Program code is
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
|
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
|
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. 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...
The code is pretty ugly... -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
Nakhash
Member #8,267
January 2007
|
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 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 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
|
Nice. -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
GullRaDriel
Member #3,861
September 2003
|
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" |
|