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

  • SEARCH
  • Home
  • 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 6815383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:48:01+00:00 2026-05-26T20:48:01+00:00

I am learning Java and want to create my own IRC client. I have

  • 0

I am learning Java and want to create my own IRC client. I have two threads but the problem is that I do not always get a response from server (can’t see PING) and it lags for my message to be delivered.

I thought it was related to thread not having a sleep, but it turns out it’s not.

When I connect to the server I send the following commands to identify myself and privatemsg self:

USER me * 8 : hi
NICK mynick 

I am also not sure if my usage of threads is correct.

The code I used:

import java.io.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class f_irc {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException {

    Socket ircSocket = null;
    BufferedWriter out = null;
    BufferedReader in = null;
    String host = "irc.freenode.net";
    int port = 6667;

    Boolean proxyEnabled = true;

    try {
        SocketAddress addr = new InetSocketAddress("127.0.0.1", 1080);
        Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr);
        ircSocket = new Socket(proxy);
        InetSocketAddress final_addr = new InetSocketAddress(host, port);
        ircSocket.connect(final_addr);
    }
    catch(Exception e)
    {
        ircSocket = new Socket(host, port);
    }

    Thread listener = new ServerListener(ircSocket);
    listener.start();

    System.out.println("Listener started!");

    Thread sender = new ServerSender(ircSocket);
    sender.start();

    System.out.println("Sender started!");
}
}

class ServerListener extends Thread implements Runnable {

Socket ircSocket;
String serverAnswer = null;
BufferedReader in = null;

ServerListener(Socket irc) throws IOException {
    ircSocket = irc;
    in = new BufferedReader(new InputStreamReader(irc.getInputStream()));
}

@Override
public void run() {
    while(true) {
        System.out.println("Running: ");
        try {
            serverAnswer = in.readLine();
            if (serverAnswer != null) {
                System.out.println("Server talkin: " + in.readLine());
                System.out.println("Server talkin++: " + serverAnswer);
            }
        } catch (IOException ex) {
            System.out.println("cant read linez br0w");
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException ex) {
            Logger.getLogger(ServerSender.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
}

class ServerSender extends Thread {

Socket ircSocket;
String serverCommand = null;
BufferedWriter out = null;
BufferedReader stdIn = null;

ServerSender(Socket irc) throws IOException {
    ircSocket = irc;
    out = new BufferedWriter(new OutputStreamWriter(irc.getOutputStream()));
    stdIn = new BufferedReader(new InputStreamReader(System.in));
}

@Override
public void run() {
    while(true) {
        System.out.println("Running snder: ");
        try {
            serverCommand = stdIn.readLine();
            if (serverCommand != null) {
                out.write(serverCommand + "\n");
                out.flush();
                System.out.println("Sent: " + serverCommand);
            }
        }
        catch(IOException e) {
            System.out.println("Server fed up");
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException ex) {
            System.out.println("Sleep failed!");
        }
    }
}
}
  • 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-26T20:48:02+00:00Added an answer on May 26, 2026 at 8:48 pm

    you are calling in.readLine() twice in your ServerListener. since you are consuming 2 messages per-loop, will not see any output until you get an even number of messages (so the 3rd message will seem to “hang” until you get the fourth).

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

Sidebar

Related Questions

I'm learning Java and I have a problem that I can't understand or solve!
So I'm learning java, and I have a question. It seems that the types
I am learning Java and have a pretty basic problem. I am indexing some
I'm learning Java and I have a problem. I created 6 different classes, each
Im learning Java and having a problem with ArrayList. Firstly I have a class
I am a beginner learning Java. I have two classes, public class myClassA {
I don't have much knowledge on Java EE but am currently learning it. I've
I am learning Java, theres one thing I do not understand.. in the main
I'm learning Java by reading the online Java book, but im finding it hard
Which one should I install when I want to start learning Java? I'm going

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.