tcp上传

package com.tz.util;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;


public class TextClient {
 public static void main(String[] args) throws Exception{
  Socket s=new Socket("192.168.1.254",10006);
  BufferedReader bufr=new BufferedReader(new FileReader("txt.txt"));
  //时间标记
  PrintWriter out=new PrintWriter(s.getOutputStream(),true);
  DataOutputStream dos=new DataOutputStream(s.getOutputStream());
  long time=System.currentTimeMillis();
  dos.writeLong(time);
//  out.println(time);
  
  String line=null;
  while ((line=bufr.readLine())!=null) {
   out.println(line);
  }
  s.shutdownOutput();//关闭客服端输出流,相当于加结束标记-1
  dos.writeLong(time);
  out.println("over");//添加结束标记
  BufferedReader bufIn=new BufferedReader(new InputStreamReader(s.getInputStream()));
  String str=bufIn.readLine();
  System.out.println(str);
  bufr.close();
  s.close();
 }

}
class TextServer{
 private void mian() throws Exception {
  ServerSocket ss=new ServerSocket(10006);
  Socket s=ss.accept();
  
  DataInputStream dos=new DataInputStream(s.getInputStream());
  long l=dos.readLong();//时间编辑
  
  BufferedReader bufIn=new BufferedReader(new InputStreamReader(s.getInputStream()));
  PrintWriter out=new PrintWriter(new FileWriter("txt.txt",true));
  String line=null;
  while ((line=bufIn.readLine())!=null) {
   if ("over".equals(line)) {
    break;
   }
   out.println(line);
  }
  PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
  pw.println("yes");
  
  out.close();
  s.close();
  ss.close();
 }
}

你可能感兴趣的:(tcp上传)