如何在java中建立线程套接字连接?

时间:2011-09-19 09:05:09

标签: java multithreading sockets

我很遗憾地提出这样一个基本问题,但我是线程和套接字编程的新手。我正在尝试编写一个多线程套接字程序,但它总是给出错误。当我调试它时,我找不到为什么它会给出那个愚蠢的错误。你能在我错的地方帮助我吗?

MY CLIENT CLASS:

public class Client {

    public void run() throws IOException{
        Socket s = new Socket("localhost",9999);
        PrintStream ps = new PrintStream(s.getOutputStream());
        ps.println(readFromConsole());
    }

    public String readFromConsole(){
        String result ;
        Scanner in = new Scanner(System.in);
        result = in.nextLine();
        return result;
    }
    public static void main(String[] args) throws Exception {
        Client c = new Client();
        c.run();
    }
}

我的服务员类:

public class Server {
    ServerSocket ss;
    public void run() throws IOException{
        ss = new ServerSocket(9999);
        while(true){
            TestForThread t;
            try {
                t = new TestForThread(ss.accept());
                Thread testThread = new Thread(t);
                testThread.start();
            } catch(IOException e){
                e.printStackTrace();
            }
        }
        //BufferedReader br =
        //new BufferedReader(new InputStreamReader(sForAccept.getInputStream()));
        //String temp = br.readLine();
        //System.out.println(runCommand(temp));
    }
    protected void finalize() {
        // Objects created in run method are finalized when
        // program terminates and thread exits
        try {
            ss.close();
        } catch (IOException e) {
            System.out.println("Could not close socket");
            System.exit(-1);
        }
    }

    public static void main(String[] args) throws Exception{
        Server s = new Server();
        s.run();
    }
}

MY THREAD CLASS ::

public class TestForThread implements Runnable {

    private Socket client;
    public TestForThread(Socket c){
        client = c;
    }

    public void run() {
        String line;
        BufferedReader in = null;
        PrintWriter out = null;
        try {
            in = new BufferedReader(new InputStreamReader(client
                .getInputStream()));
            out = new PrintWriter(client.getOutputStream(), true);
        } catch (IOException e) {
            System.out.println("in or out failed");
            System.exit(-1);
        }

        while (true) {
            try {
                line = in.readLine();
                String commandResult = runCommand(line);
                System.out.println(commandResult);
            } catch (IOException e) {
                System.out.println("Read failed");
                System.exit(-1);
            }
        }
    }

    public String runCommand(String command) throws IOException {
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(command); // you might need the full path
        InputStream is = process.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);
        String line;
        String answer = "";
        while ((line = br.readLine()) != null) {
            System.out.println(line);
            answer = answer + line + "\n";
        }
        return answer;
    }
}

非常感谢。

1 个答案:

答案 0 :(得分:1)

在您的代码中,您使用BufferedReader从Socket包装InputStream,并调用readLine()以获取客户端发送的字符串。根据{{​​3}}(), readLine()将等待'\ n'或'\ r'。问题在于Client.readFromConsole(),因为它不返回包含行终止字符的字符串。

在Client.readFromConsole()方法返回的字符串中添加'\ n'或'\ r',如下所示:

public String readFromConsole() {
    String result ;
    Scanner in = new Scanner(System.in);
    result = in.nextLine();
    return result+System.getProperty("line.separator");
}

服务器不会在第一次请求后停止运行。