簡體   English   中英

用於與IRC服務器通信的Java套接字

[英]Java Sockets for communication with IRC server

我正在學習Java,並想創建自己的IRC客戶端。 我有兩個線程,但問題是我並不總是從服務器得到響應(看不到PING)並且它滯后於我的消息被傳遞。

我以為這與線程沒有睡眠有關,但事實並非如此。

當我連接到服務器時,我發送以下命令來標識自己和privatemsg自身:

USER me * 8 : hi
NICK mynick 

我也不確定我的線程使用是否正確。

我用過的代碼:

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個消息似乎會“掛起”直到你獲得第四個消息)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM