求解一个java socket编程 自己写的socket通讯发现当client输入信息后Server端无法接受到信息,求指出问题地方。
Client 源码:import java.net.*;
import java.io.*;
public class ClientSocket {
public static void main(String [] args) throws Exception{
Socket accs=new Socket("127.0.0.1",8700);
BufferedReader mbr=new BufferedReader(new InputStreamReader(System.in));
PrintWriter tpw=new PrintWriter(accs.getOutputStream());
BufferedReader fmbrs=new BufferedReader(new InputStreamReader(accs.getInputStream()));
String strMessage = mbr.readLine();
while(!strMessage.equals("exit")){
tpw.print(strMessage);
tpw.flush();
System.out.println("Send to server: "+ strMessage);
System.out.println("Serve say : "+ fmbrs.readLine());
strMessage = mbr.readLine();
}
tpw.close();
fmbrs.close();
accs.close();
}
}
Server端源码:
import java.net.*;
import java.io.*;
public class HostSocket {
public static void main(String [] args) throws Exception{
ServerSocket mss=new ServerSocket(8700);
Socket accs= mss.accept();
String strInMessage;
BufferedReader mbrc=new BufferedReader(new InputStreamReader(accs.getInputStream()));
PrintWriter mpw=new PrintWriter(accs.getOutputStream());
BufferedReader mbr=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Client Say: "+mbrc.readLine());
strInMessage=mbr.readLine();
while(!strInMessage.equals("exit")){
mpw.print(strInMessage);
mpw.flush();
System.out.println("Send to Client Message: "+strInMessage);
System.out.println("Client say "+mbrc.readLine());
strInMessage=mbr.readLine();
}
mpw.close();
mbrc.close();
accs.close();
mss.close();
}
}
----------------解决方案--------------------------------------------------------
目测没有发现问题....你自己断点好好调调吧
----------------解决方案--------------------------------------------------------
回复 2楼 husiwen
在BufferedReader.readLine()方法下,阻塞了找不到原因,能帮我调试下找下原因吗? ----------------解决方案--------------------------------------------------------
readLine() 方法阻塞,你的循环是跑不起来的
我也是新手,目前的认知水平,感觉不用线程,你这个实现好像不可能实现
如果说的不对,请大侠们指正
----------------解决方案--------------------------------------------------------