TCP Sockets and PThreads (Linux gcc and Mingw gcc for WinXP)
Posted on 2005-03-01
The Short version:
Looking for working examples of code that uses TCP for an integrated multi-client and server that is used for chatting which connects to other servers as well (sort of like a gateway) and treats it as another client so that the whole community can talk to each other. This server itself would not have any GUI, just redirects data from one socket and sends it to all the other sockets (clients and servers alike).
Looking for working examples that will compile in Dev-C (for windows) and/or Fedora Linux via GCC which can easily be ported to the other if it doesn't support both. Note that Dev-C uses a MingW ported GCC compiler so that most linux apps will compile with very little changes as long as it conforms to standards.
The Long version:
I am making a moderation bot for battle.net for a non-graphical linux server. The short version above is just something that would get me started, but is actually much more complex then that as I will explain here...
I have already done this in borland delphi, so I do not need help with the authentication process of logging in or anything, but I do need help with TCP and threads for C++.
I am not ashamed that, despite my long programming career, I still have not grasped C++ in any great detail, and I am looking to use this project as an excuse to strengthen my skills in it.
Basically what I am looking for is something that I can compile in Dev-C and very easily port to Linux by using compliant code. I would even use libraries such as HawkNL to do this which is available in both.
I would like working examples or code snippits. Just giving me links really doesn't help me as I've tried multiple examples that I've found on google.com with very little success.
Anyway, this is my plan...
Set up a server that runs on a linux machine that will connect to another server (as a client) and join a channel for moderation purposes. Would like the ability to connect more than once as well.
This server will have no GUI of any kind resident on the server so that it can be accessed via shell.
In addition, telnet clients can connect to this server to recieve status of what is going on, such as chat messages that are in the channel. There should be the capability of more than one client at a time as well. Later upgrades may even allow connected clients to talk to each other in a chat, but that is not a high priority.
Example output to the clients could be something like this:
Enter Username: Rob
Enter Password: #######
[PHP OnLogin Response] Welcome Rob, you have access 100.
Current bots are: bot1 in woo, bot2 in woo, bot3 in blah.
#monitor bot1 as 1 <input>
[bot1] Ralph has left the channel.
#monitor bot2 as 2 <input>
[bot1] Bob says: hello
[bot2] Bob says: hello
#2 hello bob! This is rob.<input>
[bot2] says [as Rob]: hello bob! This is rob.
[bot1] Bot2 says: hello bob!
[Bot1] Bob whispers : !join blah
[Bot1] PHP OnWhisper Response: /join blah
[Bot1] Joins channel: blah
[Bot1] Current Users: Willy, Rob, Etc.
[Bot2] Bot1 has left the channel.
[Bot1] Willy says: Hello bot!
#1 Hello Willy! <input>
[Bot1] says [as Rob]: Hello Willy!
[Bot1] says [as Joe]: Hi!
[Message from Joe]: Hey, your on bots too!
#Joe Yes I am!
[Message to Joe]: Yes I am!
[Users on bots]: Rob, Joe, Steve.
Note: that in the BNET channel, users off the bot would just see the bots talking to itself if it isn't a direct message. :)
So, basically to accomplish this, I think I need to use threads. PThreads is supported both by Linux and Windows via MingW's gcc compiler (and a DLL), so I think I was to go that route so that I can develop on my windows box and then port it over to the linux server later with minimal changes.
For the bots connecting to the BNET server (as a client), would need to maintain a client list, and all input recieved from BNET to be relayed to the monitoring clients of the bot server.
To complicate things even more, I want to do some automatic moderation and additional services via PHP scripts that is called after each event such as when a user joins or leaves channel, talks or whispers in the channel, or anything else that can happen. With this, the PHP script can determine if that user has access via a DB to commands that is also parses. The scripts themselves are a not a problem, just looking to see if there is a better way than shelling/piping PHP, or calling the script as if it was a webpage. This is something I can figure out on my own, but I am putting in this question case anyone has any suggestions.
Any help would be appreciated!