Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7053191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:28:30+00:00 2026-05-28T03:28:30+00:00

I don’t know exactly how threads work, but I guess i’m doing it right.

  • 0

I don’t know exactly how threads work, but I guess i’m doing it right.
I’m coding an IRC bot just to help me to administer some computers.

When the bot receives the command -new, it runs another bot with the given parameters.
The bot connects to the network properly but the last one that received the command to run another one, stops responding the administrator commands.

Here is the bot class:

public class IRCBot extends Thread {

    private final String NETWORK;
    private final int PORT;
    private String nickname;
    private String defaultChannel;
    private String admin;
    private Socket connection;
    private BufferedReader input;
    private BufferedWriter output;
    private final String VERSION = "1.00";

    public IRCBot(String NETWORK, int PORT, String nickname, String defaultChannel, String admin) {
        this.NETWORK = NETWORK;
        this.PORT = PORT;
        this.nickname = nickname;
        this.defaultChannel = defaultChannel;
        this.admin = admin;
    }

    private void connect() throws Exception {
        connection = new Socket(NETWORK, PORT);
        input = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        output = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream()));
    }

    private void disconnect() throws Exception {
        connection.close();
    }

    private void sendLoginData() throws Exception {
        output.write("NICK " + nickname + "\n");
        output.write("USER " + nickname + " codemonkey.com CM: " + nickname + "\n");
        output.flush();
    }

    private void pingPong(String[] data) throws Exception {
        if (data[0].equals("PING")) {
            System.out.println("--> PONG " + data[1]);
            output.write("PONG " + data[1] + "\n");
            output.flush();
        }
    }

    private void joinChannel(String channel) throws Exception {
        output.write("JOIN " + channel + "\n");
        output.flush();
    }

    private void sendMessage(String to, String message) throws Exception {
        System.out.println("--> PRIVMSG " + to + " :" + message + "\n");
        output.write("PRIVMSG " + to + " :" + message + "\n");
        output.flush();
    }

    private void verifyMOTD(String[] data) throws Exception {
        if (data.length >= 2) {
            /**
             * 376 is the protocol number (end of MOTD)
             */
            if (data[1].equals("376")) {
                joinChannel(defaultChannel);
                sendMessage(defaultChannel, "wazup monkeys!");
            }
        }
    }

    private boolean isCommand(String[] data) {
        if (data.length >= 4) {
            if (data[1].equals("PRIVMSG")) {
                String[] split = data[0].split("!");
                if (split[0].substring(1).equals(admin)) {
                    if (data[3].substring(1, 2).equals("-")) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    private void verifyCommand(String[] data) throws Exception {
        if (isCommand(data)) {
            String from = data[2];
            String command = data[3].substring(2);
            switch (command) {
                case "admin":
                    sendMessage(from, admin);
                    break;
                case "version":
                    sendMessage(from, VERSION);
                    break;
                case "avapro":
                    sendMessage(from, String.valueOf(Runtime.getRuntime().availableProcessors()));
                    break;
                case "freememory":
                    sendMessage(from, String.valueOf(Runtime.getRuntime().freeMemory()));
                    break;
                case "totalmemory":
                    sendMessage(from, String.valueOf(Runtime.getRuntime().totalMemory()));
                    break;
                case "new":
                    if (data.length < 9) {
                        throw new WrongParametersException(from, "-new (network) (port) (nickname) (defaultChannel) (admin)");
                    }
                    IRCBot newBot = new IRCBot(data[4],Integer.parseInt(data[5]),data[6],data[7],data[8]);
                    Thread threadNewBot = new Thread(newBot);
                    threadNewBot.run();
                    break;
                default:
                    sendMessage(from, "exception -> uknown function: \"" + command + "\"");
                    break;
            }
        }
    }

    public void run() {
        try {
            connect();
            sendLoginData();

            while (true) {
                String data = null;
                while ((data = input.readLine()) != null) {
                    System.out.println("<-- " + data);
                    String[] dataSplitted = data.split(" ");
                    pingPong(dataSplitted);
                    verifyMOTD(dataSplitted);
                    try {
                        verifyCommand(dataSplitted);
                    } catch (WrongParametersException ex) {
                        sendMessage(ex.from(), ex.toString());
                    }
                }
            }
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
}

And here the main class:

public class Main {
    public static void main(String[] args) {
        IRCBot bot = new IRCBot("irc.quakenet.org",6667,"cod3monk3y","#codemonkey","codemonkey");
        Thread botThread = new Thread(bot);
        botThread.run();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:28:30+00:00Added an answer on May 28, 2026 at 3:28 am

    You invoke run() on your Thread instead of start().

    run() just calls the method which is performing your code on the same thread it was called.

    start() actually starts a new thread and perform the code on it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't know if I worded the question right, but basically what I want to
Don't know whats exactly going on, but it's definitely killing my time for nothing.
Don't know if this is the right place to ask this, but I will
Don't know whether I'm having a "thick day" - but I just wondered what
Don't really know how to formulate the title, but it should be pretty obvious
Don't know how to google for such, but is there a way to query
(Don't know if this is strictly on-topic, but I don't see any better Stack
Don't know how to explain it better but i'm trying to get a response
Don't know if anyone can help me with this or if it's even possible.
I don't know if this question is trivial or not. But after a couple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.