IRC_SockClient

Prerequisites: 

basic knowledge of network programming.

	So we want to create a client yes? Do we even know what a client is? How
is a client created? What does a client do? Well my friends, I plan to go over
each and every question that I once had about them. First off I would like to
point out that for this tutorial, YES we will want to create a client. Now as
far as I know, a client is a general every day application that has some
Internet connectivity. The client we are going to be creating will be a simple
one to connect to, oh lets say, an IRC server. I chose an IRC server because
when I had first learned to use sockets, I had wanted to jump right in and write
an IRC client which would eventually be turned into a bot that meets my
friends/communities needs. I had struggled with most of it at first. As time had
passed and my knowledge of sockets had increased by playing with the code, the
more structured the client had became and the less and less errors I had
recieved. My goal here in this tutorial is to try and teach people so that they
will not have to struggle as much as I had.

	Now my definition of what a client does would be that it serves the
purpouse of what the server requests of it and the user and what the user
requests of it and the server. Sort of like the man in the middle who runs back
and forth between the two relaying messages to each other.

	Some things that we need to look into first would be, what it is that we
aspect of our client. One thing that we will most definitely aspect would be
that it connects. So we will start with the connecting but first there is one
thing we need to do. We need to look into how IRC works and look at how we need
to use its protocols. The IRC server works like shown in there man pages located
here: http://irchelp.org/irchelp/rfc/chapter1.html#c1_1 The protocols how ever
will seem confusing at first. You have commands like you would in a *nix shell,
how ever these commands have to be issued to the server in a spesific order,
otherwise everything will get screwed up and either the server or client will
get confused. The commands we are going to focus on for connecting will be,
NICK, USER, and JOIN. NICK is what we will set the nick of the client to, for
example, NICK TestClient. The USER command is what will register your
information to your nick such as a name and domain and such. For an example, in
my IRC bot the USER command looks something like this, "USER supergate
anything soldierx :supergate". The JOIN command is for joining a channel similar
to the /join in clients such as xchat and for an example we will "JOIN
#soldierx".

	Now onto creating the client. Some people favor Winsock and some people
favor the sockets in *nix, however, I have never used the Winsock library before
so I will be fair to BOTH *nix and windows users and not write the following for
a specific Operating System. Now that we know what it is that the server will
ask of us, we will need to set up our variables. for our variables in this
paper, lets create the variables as char. Here is how we are going to name them
for this paper.
char ClientNick = "NICK ClientTest\r\n";
char ClientUser = "USER ClientTest client soldierx :client\r\n";
char ServerJoin = "JOIN IP of server\r\n";
Now for the ServerJoin, you will want to make that equal to the IP address of the server you wish it to join. I had parsed that into the connect function because my client connects to only one server. In your connection loop it will be best to and a while loop in there and have it print out the information that the server is spitting at it. How I did that was I had created a buffer to send the information into like so:
        int bytes;
        char buffer[BUFSIZ+1];
Due to the server constantly spitting out information to you, before you have your client send its commands to the server, besure to set bytes to 0. The reason for this is because while byte is greater then 0 the buffer will be written into and will be displayed on the screen which in turn, doesn't give your client the chance to send its commands. Once you are connected, the server will most likely PING you everyonce in a while. If your client fails to respond, the server will see it as timed out and will disconnect it. So to combat this, what you need to do is to constantly have your client compare its buffer where the server is sending data to, for the char PING. To respond to a PING you will need to send a PONG with the information that it sends with the PING. My bot is written a bit different do to the fact that I have a constant var equal to the PING information sent to it by the server the bot was meant for. However you shouldn't have a problem by doing something like:
char pong;
if (strcmp(buffer,"PING"){
        pong = buffer;
        send pong;
}
Now hopefully we should have a well written client in either Winsock or *nix network sockets with what knowledge we should already have on network programming. If not we should have some tutorials or books around here on SX that covers the topic of networking sockets. Some things that I recommend looking into are the a few more commands such as MODE. I am going to rap this tutorial up by saying that everyone should expect a part two of this covering adding client functions. I will also create another paper/tutorial that does not just focus on IRC but on a more rounder broader subject of connecting to different servers.
Reference: 

Hand written by myself