Previous Section  < Day Day Up >  Next Section

Hack 34 A Simple Java IRC Client

figs/moderate.gif figs/hack34.gif

If you want to make an IRC client that can run on a variety of platforms, Java provides one possible solution.

To illustrate how easy it is to connect to IRC with other languages, you can apply the protocol information learned from the earlier Telnet experiments [Hack #31] and port the Perl example to Java. Using a Java program to connect to IRC is perhaps the most flexible solution, as it can run on any machine that has a Java Virtual Machine installed and can also run inside a web browser as an Applet.

5.5.1 The Code

Save the following code to a file called



public class HackBot {

    public static void main(String[] args) throws Exception {

        // The server to connect to and our details.

        String server = "";

        String nick = "simple_bot";

        String login = "simple_bot";

        // The channel which the bot will join.

        String channel = "#irchacks";


        // Connect directly to the IRC server.

        Socket socket = new Socket(server, 6667);

        BufferedWriter writer = new BufferedWriter(

                new OutputStreamWriter(socket.getOutputStream( )));

        BufferedReader reader = new BufferedReader(

                new InputStreamReader(socket.getInputStream( )));


        // Log on to the server.

        writer.write("NICK " + nick + "\r\n");

        writer.write("USER " + login + " 8 * : Java IRC Hacks Bot\r\n");

        writer.flush( );


        // Read lines from the server until it tells us we have connected.

        String line = null;

        while ((line = reader.readLine( )) != null) {

            if (line.indexOf("004") >= 0) {

                // We are now logged in.



            else if (line.indexOf("433") >= 0) {

                System.out.println("Nickname is already in use.");





        // Join the channel.

        writer.write("JOIN " + channel + "\r\n");

        writer.flush( );


        // Keep reading lines from the server.

        while ((line = reader.readLine( )) != null) {

            if (line.toLowerCase( ).startsWith("PING ")) {

                // We must respond to PINGs to avoid being disconnected.

                writer.write("PONG " + line.substring(5) + "\r\n");

                writer.write("PRIVMSG " + channel + " :I got pinged!\r\n");

                writer.flush( );


            else {

                // Print the raw line received by the bot.






5.5.2 Running the Hack

You must first compile the hack with the javac command. If you have the Java SDK installed and set up correctly, you can do this from the command prompt:

% javac

This will compile the source code into platform-independent bytecode and create the file HackBot.class. You can now run the HackBot from the same command prompt:

% java HackBot

The HackBot will now leap into life.

5.5.3 Results

When you run the Java HackBot, it should connect to the IRC server you specified. Any exceptions will be allowed to propagate from the main method, so if something goes wrong, you will find out what it was. When it comes to adding a few more features to this IRC bot, you may like to consider dealing with these exceptions more neatly. As with the simple Perl bot, the HackBot should display the raw lines it receives from the server after it has connected successfully.

Each time this bot receives a PING from the server, it will respond with a PONG and use the PRIVMSG command to tell the channel that it got pinged. The format of this message will be something like this:

PRIVMSG #irchacks :I got pinged!

The PRIVMSG command is to send messages to individual users as well as channels. All you have to do is write your nickname instead of the channel name:

PRIVMSG YourNick :I got pinged!

This hack forms the basis of a bot that is able to connect to an IRC server and stay connected. With a little experimentation with the IRC protocol [Hack #78], you can add extra features to this bot and make it do something useful.

    Previous Section  < Day Day Up >  Next Section