TCP复制文件 上传
加时间戳作为结束标记,服务端用readLine读取time的时候,time是Long类型
怎么读的? 怎么判断?
*/
class CopyTextClientx
{
public static void main(String[] args) throws Exception
{
Socket s=new Socket("127.0.0.1",10016);
BufferedReader bufr=
new BufferedReader(new FileReader("CopyTextClient_c.java"));
PrintWriter out=new PrintWriter(s.getOutputStream(),true);
/*定义结束标记方法*/
DataOutputStream dos=new DataOutputStream(s.getOutputStream());
long time=System.currentTimeMillis();
dos.writeLong(time);
String line=null;
while ((line=bufr.readLine())!=null)
{
out.println(line);
}
dos.writeLong(time);//写个时间戳 作为结束标记
BufferedReader bufin=
new BufferedReader(new InputStreamReader(s.getInputStream()));
String str=bufin.readLine();//服务端等待读取,此处也会等待读取
System.out.println("......From Server:\t\n"+str);
bufr.close();
s.close();
}
}
class CopyTextServerx
{
public static void main(String[] args) throws Exception
{
ServerSocket ss=new ServerSocket(10016);
Socket s=ss.accept();
BufferedReader bufin=
new BufferedReader(new InputStreamReader(s.getInputStream()));
/*定义结束标记方法*/
DataInputStream dis=new DataInputStream(s.getInputStream());
Long lg=dis.readLong();
PrintWriter out=new PrintWriter(new FileWriter("CopyTextClientx.java"),true);
String line=null;
while ((line=bufin.readLine())!=null)//客户端没有输入流结束标记,此处等待读取
{
if ((lg+"").equals(line+""))//。。。。。。。。。。。。。。。。。。。。。判断时间戳,这样不行啊,该怎么处理?????
break;
out.println(line);
}
PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
pw.println("上传成功!");
out.close();
s.close();
ss.close();
}
}
|