红色部分 获取不到客户端传过来的数据! 什么情况? while循环直接跳过!
package com.internet;
import java.io.*;
import java.net.*;
public class TextClient {
public static void main(String[] args)throws Exception{
Socket s=new Socket("127.0.0.1",30000);
BufferedReader bufr=
new BufferedReader(new FileReader("src\\com\\internet\\IPDemo.java"));
PrintWriter out=new PrintWriter(s.getOutputStream());
String line=null;
while((line=bufr.readLine())!=null){
out.println(line);
System.out.println(line);
}
s.shutdownOutput();//关闭客户端输出流.相当于给流中加入一个结束标记 -1;
BufferedReader bufIn=
new BufferedReader(new InputStreamReader(s.getInputStream()));
String str=bufIn.readLine();
System.out.println(str);
bufr.close();
s.close();
}
}
class TextServer{
public static void main(String[]args)throws Exception{
ServerSocket ss=new ServerSocket(30000);
Socket s=ss.accept();
String ip=s.getInetAddress().getHostAddress();
System.out.println(ip+"..........Connected");
//读取数据
BufferedReader bufIn=
new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintWriter out=new PrintWriter(new FileWriter("server.txt"),true);
String line=null;
System.out.println("while循环上面"+bufIn.readLine());//获取不到数据 bufIn.readLine()的值为null?
while((line=bufIn.readLine())!=null){
//System.out.println("我在这!");//为什么这一段不运行了?
System.out.println(line);
out.println(line);
}
System.out.println("我在下面!"); //直接跳到这一段!
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
pw.println("上传成功!");
out.close();
s.close();
ss.close();
}
}
|
|