TCP写的一个点对点的聊天程序练习,可是总是有问题,连接上不能输入,求大神帮助
程序代码:
/** * @author 小南 * 客户端 */ import *; import *; public class TCPClient { public static void main(String args[]) throws Exception{ Socket s = new Socket("172.0.0.1",5003); System.out.println("client connect is ok"); Thread.sleep(2000); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedReader netin = new BufferedReader(new InputStreamReader(s.getInputStream())); BufferedWriter netou = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); String rd = null; rd = br.readLine(); while(rd!="bye") { netou.write(rd); netou.flush(); System.out.println("Server:" + netin.readLine()); rd = br.readLine(); } br.close(); netin.close(); netou.close(); s.close(); } }这里是服务器端
程序代码:
/** * @author 小南 * 服务器端 */ import *; import *; public class TCPServer { public static void main(String args[]) throws Exception{ ServerSocket ss= new ServerSocket(5003); Socket s = new Socket(); s = ss.accept(); Thread.sleep(2000); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); BufferedReader netin = new BufferedReader(new InputStreamReader(s.getInputStream())); BufferedWriter netou = new BufferedWriter(new OutputStreamWriter(s.getOutputStream())); String rd = null; rd = br.readLine(); while(rd!="bye") { netou.write(rd); netou.flush(); System.out.println("Client:" + netin.readLine()); rd = br.readLine(); } br.close(); netin.close(); netou.close(); s.close(); ss.close(); } }
连接正常,但是不能通话,实在找不到问题,求大神解决