日期:2014-05-20 浏览次数:20909 次
public class ClientDemo implements Runnable{ public void run(){ try{ //连接服务器 Socket s=new Socket("127.0.0.1",10033); //读取本机文件 BufferedReader br=new BufferedReader(new FileReader("j:\\new.txt")); //获得输出流 System.out.println("获取成功"); PrintWriter out=new PrintWriter(s.getOutputStream(),true); //增加标记 // long time=System.currentTimeMillis(); //DataOutputStream dos=new DataOutputStream(s.getOutputStream()); //dos.writeLong(time); //像服务器写文件 String line=null; while((line=br.readLine())!=null){ out.print(line); } System.out.println(line); //System.out.println("上传成功"); s.shutdownOutput();//关闭客户端输出流,相当于给个-1; //获得输入流 BufferedReader Sbr=new BufferedReader(new InputStreamReader(s.getInputStream())); //获得服务器返回信息 String str=Sbr.readLine(); System.out.println(str); br.close(); s.close(); } catch(IOException e){ e.printStackTrace(); } } }
public class ServerDemo implements Runnable{ public void run(){ try { ServerSocket ss=new ServerSocket(10033); Socket s=ss.accept(); String ip=s.getInetAddress().getHostAddress(); System.out.println(ip+"....connected....."); BufferedReader br=new BufferedReader(new InputStreamReader(s.getInputStream())); //DataInputStream dis=new DataInputStream(s.getInputStream()); //long l=dis.readLong(); PrintWriter out=new PrintWriter(new FileWriter("text.txt"),true); String line=null; while((line=br.readLine())!=null){ //if("over".equals(line)){ //break; //} out.print(line); } PrintWriter Cout=new PrintWriter(s.getOutputStream()); Cout.write("上传成功"); out.close(); s.close(); ss.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
public class Demo1 { /** * @param args */ public static void main(String[] args) { new Thread(new ServerDemo()).start(); new Thread(new ClientDemo()).start(); } }
获取成功 127.0.0.1....connected..... null null