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.

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

报告相同问题?

悬赏问题

  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退