日期:2014-05-20 浏览次数:20913 次
import java.io.*; import java.net.*; public class Server implements Runnable{ ServerSocket ser = null; int port = 6666; BufferedReader br = null; BufferedWriter bw = null; Socket soc = null; File file_send = null; File file_receive = null; InputStream ips = null; OutputStream ops = null; public void StartServer() { try { ser = new ServerSocket(port); while (true) { soc = ser.accept(); System.out.println("链接已建立!"); //Thread thread = new Thread(this); //thread.start(); } } catch (IOException e) { System.out.println("ServerSocket创建失败!检查端口是否被占用"); e.printStackTrace(); } } ////////////////接收//////////////// public void receive() { try { ips = soc.getInputStream(); file_receive = new File("D:/hello.java"); br = new BufferedReader(new InputStreamReader(ips)); bw = new BufferedWriter(new FileWriter(file_receive)); String s = null; while ((s = br.readLine()) != null) { bw.write(s); bw.newLine(); } bw.flush(); br.close(); bw.close(); } catch (IOException e) { e.printStackTrace(); } System.out.println("文件接收已完成!"); } /////////////////////发送///////////////////////// public void send(){ try { ops = soc.getOutputStream(); file_send = new File("D:/dos命令参数.txt"); bw = new BufferedWriter(new OutputStreamWriter(ops)); br = new BufferedReader(new FileReader(file_send)); String s = null; while((s = br.readLine()) != null){ bw.write(s,0,s.length()); bw.newLine(); } System.out.println("文件发送已完成!"); bw.flush(); br.close(); bw.close(); } catch (IOException e) { e.printStackTrace(); } } @Override public void run() { send(); receive(); } }
package com.yao; import java.io.*; import java.net.*; public class Client { public int port = 6666; public String host = "127.0.0.1"; Socket soc = null; File file_receive = null; File file_send = null; BufferedReader br = null; BufferedWriter bw = null; OutputStream ops = null; InputStream ips = null; /////////////建立连接///////////// public void Connect(){ try { soc = new Socket(host,port); } catch (UnknownHostException e) { System.out.println("无法在主机号:"+host+"创建监听!"); e.printStackTrace(); } catch (IOException e) { System.out.println("无法在端口"+port+"添加监听"); e.printStackTrace(); } } ////////////////发送///////////////////////// public void send(){ try { ops = soc.getOutputStream(); file_send = new File("D:/Hello.txt"); br = new BufferedReader(new FileReader(file_send)); bw = new BufferedWriter(new OutputStreamWriter(ops)); String s = null; while((s = br.readLine()) != null){ bw.write(s,0,s.length()); bw.newLine(); } System.out.println("文件发送已完成!"); bw.flush(); br.close(); bw.close(); } catch (IOException e) { e.printStackTrace(); } } /////////////////////接收///////////////////// public void receive() { try { ips = soc.getInputStream(); file_receive = new File("D:/dos命令参数.java"); br = new BufferedReader(new InputStreamReader(ips)); bw = new BufferedWriter(new FileWriter(file_receive)); String s = null; while ((s = br.readLine()) != null) { bw.write(s); bw.newLine(); } bw.flush(); br.close(); bw.close(); } catch (IOException e) { e.printStackTrace(); } System.out.println("文件接收已完成!"); } }