socket的问题 为什么通过socket发送的数据有的时候客户端接收不到,有的时候接收得到
服务端:
public class ServerDome {
public static void main(String[] args) {
ServerDome();
}
private static void ServerDome() {
ServerSocket ss;
Socket s;
BufferedReader in;
BufferedWriter out;
try {
ss=new ServerSocket(10000);
s=ss.accept();
in=new BufferedReader(new InputStreamReader(s.getInputStream()));
out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
out.write("123");
out.close();
s.close();
ss.close();
} catch (
IOException e) {
e.printStackTrace();
}
}
客户端:
public class ClientDome {
public static void main(String[] args) {
ClentDome();
}
private static void ClentDome() {
Socket s;
BufferedReader in;
BufferedWriter out;
try {
s=new Socket("127.0.0.1",10000);
in=new BufferedReader(new InputStreamReader(s.getInputStream()));
out = new BufferedWriter(new OutputStreamWriter(s.getOutputStream()));
System.out.print("1");
if(in.ready()){
String str=in.readLine();
System.out.print("客户端:");
System.out.println(str);
}
in.close();
s.close();
} catch (
UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
------解决方案--------------------
应该是你的那个in.ready()的问题