首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

关于i/o的一个有关问题

2012-09-29 
关于i/o的一个问题服务器主程序Java codepackage com.cfpl.qimport java.io.IOExceptionimport java.net

关于i/o的一个问题
服务器主程序

Java code
package com.cfpl.q;import java.io.IOException;import java.net.ServerSocket;import java.net.Socket;public class ServerMain {    public static void main(String[] args) throws IOException {        ServerSocket serverSocket = new ServerSocket(5000);        while (true) {            Socket socket = serverSocket.accept();            ServerReadThread sr = new ServerReadThread(socket);            ServerWriterThread sw = new ServerWriterThread(socket);            sr.start();            sw.start();        }    }}

服务器输入的线程
Java code
package com.cfpl.q;import java.io.IOException;import java.io.InputStream;import java.net.Socket;public class ServerReadThread extends Thread {    private Socket socket;    public ServerReadThread(Socket socket) {        this.socket = socket;    }    @Override    public void run() {        try {            InputStream is = socket.getInputStream();                        while (true) {                byte[] bt = new byte[1024];                 int length = is.read(bt);                System.out.println(new String(bt,0,length));                //                BufferedReader br = new BufferedReader(new InputStreamReader(is));//                String s = br.readLine();//                System.out.println(s);            }        } catch (IOException e) {            e.printStackTrace();        }    }}

服务器输出的线程
Java code
package com.cfpl.q;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStream;import java.net.Socket;public class ServerWriterThread extends Thread {    private Socket socket;    public ServerWriterThread(Socket socket) {        this.socket = socket;    }    @Override    public void run() {        try {            OutputStream os = socket.getOutputStream();            while (true) {                BufferedReader br = new BufferedReader(new InputStreamReader(                        System.in));                String s = br.readLine();                os.write(s.getBytes());            }        } catch (IOException e) {            e.printStackTrace();        }    }}


客户端主程序
Java code
package com.cfpl.q;import java.net.Socket;public class ClientMain {    public static void main(String[] args) throws Exception {        Socket socket = new Socket("127.0.0.1",5000);        ClientReadThread cr = new ClientReadThread(socket);        ClientWriterThread cw = new ClientWriterThread(socket);        cr.start();        cw.start();    }}

客户端输入线程
Java code
package com.cfpl.q;import java.io.IOException;import java.io.InputStream;import java.net.Socket;public class ClientReadThread extends Thread {    private Socket socket;    public ClientReadThread(Socket socket) {        this.socket = socket;    }    @Override    public void run() {        try {            InputStream is = socket.getInputStream();            while (true) {                 byte[] bt = new byte[1024];                 int length = is.read(bt);                 System.out.println(new String(bt,0,length));//                BufferedReader br = new BufferedReader(//                        new InputStreamReader(is));//                String s = br.readLine();//                System.out.println(s);            }        } catch (IOException e) {            e.printStackTrace();        }    }} 


客户端输出
Java code
package com.cfpl.q;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStream;import java.net.Socket;public class ClientWriterThread extends Thread{    private Socket socket;    public ClientWriterThread(Socket socket) {        this.socket = socket;    }    @Override    public void run() {        try {            OutputStream os = socket.getOutputStream();            while (true) {                BufferedReader br = new BufferedReader(new InputStreamReader(                        System.in));                String s = br.readLine();                os.write(s.getBytes());            }        } catch (IOException e) {            e.printStackTrace();        }    }}


如上所示,问题出在输入线程里面,我如果用
  byte[] bt = new byte[1024];
  int length = is.read(bt);
System.out.println(new String(bt,0,length));
就没问题,但是如果改成
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String s = br.readLine();
System.out.println(s);
就没法实现功能,请问这是为什么呢?



[解决办法]
探讨

BufferedReader是有缓存的。

[解决办法]
readLine会找换行符的,如果没有的话会阻塞!
[解决办法]
我建议楼主:
既然是字符的传输,那么输出时改用与BufferedReader相对应的BufferedWriter就可以了。代码供参考:

Java code
import java.io.BufferedReader;import java.io.BufferedWriter;                                //引入相应的包。import java.io.IOException;import java.io.InputStreamReader;import java.io.OutputStreamWriter;                            //引入相应的包。import java.io.OutputStream;import java.net.Socket;public class ServerWriterThread extends Thread {    private Socket socket;    public ServerWriterThread(Socket socket) {        this.socket = socket;    }    @Override    public void run() {        try {        BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(        //输入流包装成字符缓冲流.                    socket.getOutputStream()));            //OutputStream os = socket.getOutputStream();            while (true) {                BufferedReader br = new BufferedReader(new InputStreamReader(                        System.in));                String s = br.readLine();                //os.write(s.getBytes());        bw.write(s+"\r\n");                //写出一行.        bw.flush();            }        } catch (IOException e) {            e.printStackTrace();        }    }}
[解决办法]
探讨
谢谢,根据这样改之后确实不出错了,但是如果是换行符的问题的话,那为什么下面这个可以输出呢,这个好像也没有换行符吧

热点排行