package networkprogramming;
/*
文件上传。
注意:上传完,还需要给个结束标记。
*/
import java.io.*;
import java.net.*;
class TextClient {
public static void main(String[] args) throws Exception {
Socket s = new Socket("192.168.0.100", 10006);
BufferedReader br = new BufferedReader(new FileReader("IPDemo.java"));
// long time = System.currentTimeMillis();
// DataOutputStream dos = new DataOutputStream(s.getOutputStream());
// dos.writeLong(time);
PrintWriter out = new PrintWriter(s.getOutputStream(), true);
String line = null;
while ((line = br.readLine()) != null) {
out.println(line);
}
s.shutdownOutput(); // 关闭输出流,相当于给流中加入一个结束标记-1。
// dos.writeLong(time);
BufferedReader bufIn = new BufferedReader(new InputStreamReader(
s.getInputStream()));
String str = bufIn.readLine();
System.out.println(str);
// dos.close();
br.close();
s.close();
}
}
class TextServer {
public static void main(String[] args) throws Exception {
ServerSocket ss = new ServerSocket(10006);
Socket s = ss.accept();
// DataInputStream dis = new DataIputStream(s.getInputStream());
// long overTime = dis.readLong();
BufferedReader bufIn = new BufferedReader(new InputStreamReader(
s.getInputStream()));
PrintWriter out = new PrintWriter(new FileWriter("server.txt"), true);
String line = null;
while ((line = bufIn.readLine()) != null) {
// if(overTmie.toString().equals(line))
// break;
out.println(line);
}
PrintWriter pw = new PrintWriter(s.getOutputStream(), true);
pw.println("上传成功");
// dis.close();
out.close();
s.close();
ss.close();
}
}
我想问下在使用 s.shutdownOutput(); 之前,老师说的先传一个结束标记(基本是我注释掉那些代码)。这个结束标记是 long 的,要真像这代码这样写下去,在服务器中怎么读取并判断这个结束标记啊?希望能给想法,能给完整代码更好了。
|