So, you have decided to make a program that connects to IRC! But do you know what an IRC client needs to be able to connect to a random host? Fortunately, this article will guide you through the process.
First Of All: Learn the basics:
You will need to know how a little about the IRC specification protocols in order to know how to use them to make a connection.A little help about the telnet protocol is also handy.
What is telnet?
The telnet protocol is a text protocol that lets you communicate with any server and give commands that the server supports. Most (if not all) servers on the internet, and any other ‘net’. You can use the telnet protocol via the command line. You must first specify a port in which to communicate to your server. This may require some port forwarding, Visit http://portforward.com/ for help on how to do that.
Telnet can transfer any type of data. IRC servers tranfer text mostly, so text manipulaiton of data is all that is needed in order to communicate.
Many IRC servers comprise a network. Each network has many servers you can connect to. Connecting to those servers requires telnet protocol, since in your program you will need to send commands using that protocol.
Establishing a Connection Using The Command Line
Open the DOS command prompt (or the Terminal in OS X) and enter the ‘telnet’ command using this syntax:
telnet <hostname> <port> .
Put whatever you want inside the places required, but I have chosen to connect to the IRC server efnet.teleglobe.net using port 6667. So I enter:
and I see a big chunk of text describing my connection to the server. Right now, I can enter anything I want in order to communicate with the server. I may have established a connection to the server, but the server will close the connection in a few seconds, because it doesn’t know anything about my identity! In order to make that connection stable, I must give a nickname for the server, and a User Name, using the NICK and USER commands respectively.
A little parenthesis here: How are you supposed to know all this? You aren’t. You can learn all the important details inside the IRC protocol specification article, here. Be sure to bookmark this page, as it will be needed lots of times.
As the server waits for commands, give the following commands:
and the server will send you messages to verify the stability of your connection to it. You are connected! You can now join a channel, and talk to other people using your DOS command prompt!
Connecting To A Server Using C/C++ Programming Language: Technologies
Making a program to connect to an IRC server is little harder, because you must write the communication implementation all by yourself. There are many technologies that allow your computer to communicate with the outer world, and in each platform, those technologies differ dramatically. However, one technology is cross-platform, and that is BSD sockets.
BSD sockets are a communication API that relies on abstract entities called “sockets” to establish a network connection. Sockets work for the majority of the networking needs, providing communication between programs on the same computers, on different computers, ondifferent networks. They are used in many programs, like games, or internet browsers. The simplicity of their implementation makes them preferable for the majority of applications that need a fast and stable API to implement networking to their applications
So, you will use BSD sockets. The purpose of this article is not the teaching of BSD sockets, so you will need to find another resource for that. A tutorial that guides you through everything you will need to use this article can be found here.
Read the basics of this article, understand the concept of the BSD sockets, then continue reading this.
The sample code provided in here connects to efnet.teleglobe.net, and establishes a connection with a default nickname and a password. EDIT 8/8/2011: LINK FIXED!
All headers are the same on any system. This source was written on Xcode on OS X, but it was tested on Windows XP, too. As you can see, you must first create the socket. You will create a stream socket in order to connect to an IRC server. The connect function is equal to the telnet command you used at the command line before. Since the IRC server uses a text parser to figure out what you are sending to it, you will need to provide CR and LF after each line ending, to server know where your command ends.For example, the NICK command mut be given as “NICK soulstormrn” to the server in order to be correctly parsed.
In order to receive commands from the server, you will use the recv() function.
The IRC server needs to maintain a connection. It also needs some information by you in regular time internals. During some time, the server will send you a PING. That means that the server asks you: “Are you there?”. You must reply “yes I am still here” using the PONG command, whose syntax you will find in the beforementioned IRC protocol specification. This is not described here in the source example, since it will be out of the scope of this article.
However,I should mention that there are 2 ways of achieving this: One is implementing methods and classes that are platform specific to the platform you are using. Second is to use the BSD sockets’ send() funciton in conjuction with the C function’s time() function. You must implement a run loop, where it will count the number of seconds passed, and you will send a PONG command to the server during some time interval of your choice. Otherwise, you could wait fot the PING from the server, and at this time you will send a PONG to it.
Since you are building a command line program, there is one last thing I should warn you about: Multithreading needs. When you are building a program that requires networking, not using multithreading is not a good idea. If you don’t your program will freeze every time that it waits a command, or gives a command. That will happen because your program will wait for a responce each time, and it won’t proceed with it’s execution until that responce is taken. And even if it is, you won’t be able to do anything else, because you will have exited the loop where you wait for a responce, and you will need to call that function again, wasting processor resources.
One good idea is to use multithreading for 2 functions. So, you will have 3 threads on your program (unless you need more).
- The main program which is executed
- The send() thread
- The receive() thread
The send() thread is optional. The receive() thread (where you actually wait for a server responce) is required to be on a separate thread from the main program. That is because you will need to have that loop running all the time until your program quits, because you never know what message you may receive from the server or any other person. So, keep the receive() loop running all the time on a separate thread, That way, you won’t waste any resources or time waiting for a response.
Implementing threads is platform specific information and goes out of the scope of this article. So, I am not covering it.