用于与IRC服务器通信的Java套接字 [英] Java Sockets for communication with IRC server

查看:115
本文介绍了用于与IRC服务器通信的Java套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Java并希望创建自己的IRC客户端。我有两个线程,但问题是我并不总是从服务器得到响应(看不到PING)而且我的消息传递滞后。

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.

当我连接到服务器时,我发送以下命令来识别我自己和privatemsg self:

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.

我使用的代码:

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!");
        }
    }
}
}


推荐答案

您在ServerListener中调用 in.readLine()两次。因为你每个循环消耗2个消息,所以在得到偶数消息之前不会看到任何输出(因此第3个消息似乎会挂起直到你获得第四个消息)。

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

这篇关于用于与IRC服务器通信的Java套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆