关于i/o的一个问题
服务器主程序
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(); } }}
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(); } }}
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(); } }}
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(); }}
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(); } }}
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(); } }}
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(); } }}
[解决办法]