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
1 | int 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 | } |
38 | END_OF_MAIN(); |
39 | |
40 | void ConnectS(){ |
41 | client = enet_host_create (NULL, 1, 5, 5); /* the last two parameters are bandwidth limits, see docs */ |
42 | enet_address_set_host (& address, "192.168.1.101"); |
43 | address.port = 12345; |
44 | |
45 | peer = enet_host_connect (client, & address, 5); |
46 | } |
47 | void SendPacket(){ |
48 | ENetPacket *packet; packet = enet_packet_create("HI", 2, ENET_PACKET_FLAG_RELIABLE); |
49 | enet_peer_send(peer, 0, packet); |
50 | } |
51 | |
52 | void 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
1 | int 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 | } |
43 | END_OF_MAIN(); |
44 | |
45 | void 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 | } |
85 | void 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 | } |
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).
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.)
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...
1 | prompt>p2p-chat-server |
2 | usage: p2p-chat-server port=# |
3 | Press [Enter] to continue... |
4 | prompt>p2p-chat-server port=27015 |
5 | Port: 27015 |
6 | WSAStartup successful! |
7 | Socket created successfully! |
8 | Socket bound to address: 0.0.0.0:27015 |
9 | Listening for connection... |
10 | Connected to client... |
11 | Message received from client: |
12 | This is a test message. |
13 | Connected to client... |
14 | Message received from client: |
15 | This is an unquoted test message. |
16 | Connected to client... |
17 | Message received from client: |
18 | quit |
19 | WSACleanup... |
20 | Press [Enter] to continue... |
1 | prompt>p2p-chat-client |
2 | usage: p2p-chat-client [-v] ip=#.#.#.#:# message |
3 | Press [Enter] to continue... |
4 | prompt>p2p-chat-client -v ip=127.0.0.1:27015 "This is a test message." |
5 | Connected to server: 127.0.0.1:27015 |
6 | Message sent: |
7 | This is a test message. |
8 | Press [Enter] to continue... |
9 | prompt>p2p-chat-client -v ip=127.0.0.1:27015 This is an unquoted test message. |
10 | Connected to server: 127.0.0.1:27015 |
11 | Message sent: |
12 | This is an unquoted test message. |
13 | Press [Enter] to continue... |
14 | prompt>p2p-chat-client -v ip=127.0.0.1:27015 quit |
15 | Connected to server: 127.0.0.1:27015 |
16 | Message sent: |
17 | quit |
18 | Press [Enter] to continue... |
The code is pretty ugly...
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
Client code is
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.
Nice.
Using a Lamborghini where you could have used a bike is not nice. It's overloaded.