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

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 2008-12-18 23:33
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向