Java Sockets for communication with IRC server

2019-03-03 06:42发布

问题:

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:

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).