Java对标准流的读写不起作用

Java对标准流的读写不起作用,java,sockets,input,output,irc,Java,Sockets,Input,Output,Irc,代码: 我可以发送输入,但无法在屏幕上获得任何输出。有什么想法吗?这可能是问题之一。 我相信您在线程类对象上调用run方法而不是start方法时并没有创建线程: elaur@colossus[~]$ cat irc.java import java.net.*; import java.io.*; class irc { static Socket server; static BufferedReader in; static BufferedReader stdin

代码:


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

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

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) {}
            }
    }
}
你应致电:

    t.run();

要实际创建线程,因为调用run方法只需按顺序运行该方法。

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

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) {}
            }
    }
}
你应致电:

    t.run();

要真正创建线程,调用run方法只需按顺序运行该方法。

它应该是t.start()而不是t.run()它应该是t.start()而不是t.run()好的,这很有效,但现在我没有收到服务器的响应。我的out.println(user_line)有什么特别需要做的吗;?好的,这很有效,但现在我没有收到服务器的响应。我的out.println(user_line)有什么特别需要做的吗;?