0

代码:

elaur@colossus[~]$ cat irc.java
import java.net.*;
import java.io.*;

class irc
{
    static Socket server;
    static BufferedReader in;
    static BufferedReader stdin;
    static PrintWriter out;

    public static void main(String args[])
    {
            String user_line;
            try
            {
                    server = new Socket(args[0], 6667);
                    in = new BufferedReader( newInputStreamReader(server.getInputStream()) );
                    stdin = new BufferedReader( newInputStreamReader(System.in) );
                    out = new PrintWriter(server.getOutputStream());
            }
            catch (UnknownHostException e) {}
            catch (IOException e) {}
            irc_in input = new irc_in(server, out, stdin);
            Thread t = new Thread(input);
            t.run();
            while (true)
            {
                    try {
                            System.out.println(in.readLine());
                            System.out.flush();
                            Thread.sleep(2000);
                    } catch (IOException e) {}
                      catch (InterruptedException e) {}
            }
    }
}

class irc_in implements Runnable
{
    static Socket server;
    static PrintWriter out;
    static BufferedReader stdin;

    irc_in(Socket a, PrintWriter b, BufferedReader c)
    {
            server = a;
            out = b;
            stdin = c;
    }

    public void run()
    {
            String user_line;
            while (true)
            {
                    try
                    {
                            Thread.sleep(1000);
                            user_line = stdin.readLine();
                            System.out.println("Got: " + user_line);
                            out.println(user_line);
                            out.flush();
                    }
                    catch (IOException e) {}
                    catch (InterruptedException e) {}
            }
    }
}

我可以发送输入,但我无法在我的屏幕上获得任何输出。有任何想法吗?

4

2 回答 2

3

这可能是问题之一。我相信您没有在调用线程类对象上的 run 方法而不是 start 方法时创建线程:

    t.run();

你应该打电话:

        t.start();

实际创建一个线程,因为调用 run 方法将简单地以顺序方式运行该方法。

于 2013-08-13T16:14:34.080 回答
2

它应该是t.start()不是 t.run()

于 2013-08-13T16:31:15.023 回答