将BufferedReader作为类的构造函数的参数传递

时间:2014-03-26 15:34:54

标签: sockets networking bufferedreader socketexception

我已经建立了一个网络,并且我已经将读取和写入流设置为套接字,如下所示:

//Set up socket reads and writes
final BufferedReader in = new BufferedReader(
    new InputStreamReader(client.getInputStream()));

final PrintWriter out = new PrintWriter(
    client.getOutputStream(), true);

我想将两个变量'in'和'out'作为另一个类'构造函数的参数传递。这就是它在其他课程中的表现

BufferedReader in;
PrintWriter out;

public ClientThread(BufferedReader in, PrintWriter out) {
    this.in = in;
    this.out = out;

}

然后,我想使用这些类变量来写入相同套接字的输出流(类实现Runnable):

public void run() {
    while (true) {
        try {
            String userCommand = in.readLine();
        } catch (IOException e) {
            // Die if something goes wrong.
            System.err.println(e.toString());
            System.exit(1);
        }
    }
}

然而,每当代码到达这一点时,我都会得到一个SocketException:

java.net.SocketException: Socket closed

我该如何解决这个问题?我想将服务器和套接字的设置与客户端给出的任何命令的处理分开。


编辑:这是BufferedRead从

获取输入的内容
//create server socket
ServerSocket server = new ServerSocket(portNum);

// Accept a client if it appears
Socket client = server.accept();

编辑2:我使用了这三个文件:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;


public class Client {

    public static void main(String[] args) throws UnknownHostException, IOException {
        //Change the socket if it doesn't work
        Socket sock = new Socket("localhost", 5920);

        //keyboard
        final BufferedReader stdin = new BufferedReader(
                new InputStreamReader(System.in));

        //input from socket
        final BufferedReader in = new BufferedReader(
                new InputStreamReader(sock.getInputStream()));

        //writer to socket
        final PrintWriter out = new PrintWriter(
                sock.getOutputStream(), true);

        //new thread for incoming messages
        (new Thread(){

            @Override
            public void run() {
                String serverMessage;

                try {
                    while ((serverMessage = in.readLine()) != null) {
                        System.out.println(serverMessage);

                    }
                } catch (IOException e) {
                    System.err.println("Something went wrong whilst trying "
                            + "to retrieve a message from the server");
                    System.exit(-1);
                }
            }
        }).start();

        //new thread for outgoing messages
        (new Thread(){

            @Override
            public void run() {
                String clientMessage;

                try {
                    while ((clientMessage = stdin.readLine()) != null) {
                        out.println(clientMessage);

                    }
                } catch (IOException e) {
                    System.err.println("Something went wrong whilst trying "
                            + "to send a message to the server.");
                    System.exit(-1);
                }

            }

        }).start();

    }

}

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;


public class Server {

    public static void main(String[] args) throws IOException {
        ServerSocket server = new ServerSocket(5920);

        Socket client = server.accept();

        //Set up socket reads and writes
        final BufferedReader in = new BufferedReader(
                new InputStreamReader(client.getInputStream()));

        final PrintWriter out = new PrintWriter(
                client.getOutputStream(), true);

        new Thread(new ClassWithParam(in, out)).start();

    }

}

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;


public class ClassWithParam implements Runnable {

    BufferedReader in;
    PrintWriter out;

    public ClassWithParam(BufferedReader in, PrintWriter out) {
        this.in = in;
        this.out = out;

    }

    @Override
    public void run() {
        while (true) {
            try {
                System.out.println("HERE");
                String userCommand = in.readLine();
                System.out.println("HERE2");
            } catch (IOException e) {
                // Die if something goes wrong.
                System.err.println(e.toString());
                System.exit(1);
            }
        }

    }

}

现在它有效。不知道发生了什么。将继续撞到墙上。感谢。

1 个答案:

答案 0 :(得分:0)

由于某种原因,现在没有问题。我现在使用的代码(我重新创建)在描述中。