本帖最后由 张亭 于 2012-5-26 19:06 编辑
import java.io.*;
import java.net.*;
class TextClient {
public static void main(String[] args) throws Exception {
Socket s = new Socket("127.0.0.1", 10005);
BufferedReader br = new BufferedReader(new FileReader("TCPDemo.java"));
String line = null;
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
s.getOutputStream()));// 老师是这里用的打印流我用的字符流
while ((line = br.readLine()) != null) {
bw.write(line);
bw.newLine();
bw.flush();
}
bw.write("over");// 结束标记
bw.newLine();//要加上这句
bw.flush();
BufferedReader brIn = new BufferedReader(new InputStreamReader(
s.getInputStream()));
System.out.println(brIn.readLine());
br.close();
s.close();
}
}
class TextServer {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(10005);
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip);
BufferedWriter bw = new BufferedWriter(new FileWriter("Server.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(
s.getInputStream()));
BufferedWriter bwIn = new BufferedWriter(new OutputStreamWriter(
s.getOutputStream()));
String line = null;
while ((line = br.readLine()) != null) {
if ("over".equals(line))
break;
bw.write(line);
bw.newLine();
bw.flush();
}
bwIn.write("上传成功");// 返回客户端字样
bwIn.newLine();//加上这两句
bwIn.close();
bw.close();
ss.close();
}
}
因为你用的都是readLine()方法,这个方法要读到换行标记才结束
所以要添上对应的newLine()
|