sfisksk
wuguangbiao518
2008-12-14 09:12
采纳率: 0%
浏览 315
已采纳

socket readline()阻塞了

package mytest;

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) {
BufferedReader in = null;
PrintWriter out = null;
Socket socket = null;
ServerSocket sSocket = null;
String IP = null;
String hostName = null;

    try {
        sSocket = new ServerSocket(1000);
        System.out.println("the connect is doing");
        while (true) {
            socket = sSocket.accept();
            System.out
                    .println("the connect is successed from server to client!");
            IP = socket.getInetAddress().getHostAddress();
            hostName = ":" + socket.getLocalPort();
            System.out.println("A Client come in! IP:" + IP + hostName);
            in = new BufferedReader(new InputStreamReader(System.in));
            String str = in.readLine();[color=red][b][b]//这里会产生阻塞,我想不管我终端有没有输入,后面的代码都要运行,这应该怎么做?[/b][/b][/color]
            out = new PrintWriter(socket.getOutputStream(), true);
            out.println("Server Said:" + str);
            in = new BufferedReader(new InputStreamReader(socket
                    .getInputStream()));
            str = in.readLine();
            System.out.println("Receive message from Client :"
                    + str);
        }
    } catch (IOException e) {
        System.out.println("error");
    } finally {
        try {
        in.close();
        out.close();
        socket.close();
        }catch (IOException e) {
            System.out.println("error");
        }
    }
}

}

package mytest;

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

public class Client {
public static void main(String[] args) {
BufferedReader in = null;
PrintWriter out = null;
Socket socket = null;

    try {
        socket = new Socket("127.0.0.1", 1000);
        System.out.println("connect successed!");
        while (true) {
            in = new BufferedReader(new InputStreamReader(System.in));
            String str = in.readLine();
            out = new PrintWriter(socket.getOutputStream(), true);
            out.println("the Client said:" + str);
            in = new BufferedReader(new InputStreamReader(socket
                    .getInputStream()));
            str = in.readLine();
            System.out.println("from server the message is :" + str);

        }
    } catch (IOException e) {
        System.out.println("error");
        System.out.println(e.getMessage());
    } finally {
        try {
            in.close();
            out.close();
            socket.close();
        } catch (IOException e) {
            System.out.println("error");
        }
    }
}

}

[b]问题补充:[/b]
采用nio怎样做???
我没有用过??

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • wwwghost
    wwwghost 2008-12-18 23:33
    已采纳

    两种解决方案:
    1.专门采用一个线程进行读取流;
    2.采用NIO非阻塞方式。
    这样可以解决问题,具体的实现请参看Java Tutorial.

    点赞 评论

相关推荐