平台要求:<o:p></o:p>
1. Windows或Unix/Linux
2. JAVA/VC/BCB/Delphi
<o:p> </o:p>
功能要求:<o:p></o:p>
1. 通过SOCKET实现点对点的文件交换功能
2. 支持连接握手、登录校验、文件传输请求应答、出错重传等交互的通信协议机制
3. 提供简单的用户界面或命令行界面
java 代码
java 代码
- package filetranslate;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.DataInputStream;
- import java.io.DataOutputStream;
- import java.io.File;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.io.RandomAccessFile;
- import java.net.Socket;
- import java.util.HashMap;
- public class FileServ extends Thread{
- Socket sc=null;
- InputStream in =null;
- OutputStream out=null;
- DataInputStream inStream = null;
- DataOutputStream outStream = null;
- BufferedInputStream bn =null;
- BufferedOutputStream bo =null;
- String servercepath="d:/socketserver/";
- String tempfilename=null;
- static int point=0;
- HashMap hs=new HashMap();
- public FileServ(){
- }
- public FileServ(Socket sc){
- this.sc=sc;
- try {
- bn=new BufferedInputStream( new DataInputStream(sc.getInputStream()));
- bo=new BufferedOutputStream(new DataOutputStream(sc.getOutputStream()));
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * 接受发送消息
- * @return 返回的消息
- * @throws IOException
- */
- public String receiverMessage() throws IOException {
- int num = 0;
- byte[] br = null;
- br = new byte[2048];
- num = bn.read(br);
- if(num!=-1){
- return new String(br, 0, num);
- }else{
- return null;
- }
- }/**
- * 接受文件
- * @return
- */
- public boolean receviceFile(){
- //使用本地文件系统接受网络数据并存为新文件
- File file=new File(servercepath+tempfilename);
- RandomAccessFile raf=null;
- int count=0;
- try {
- file.createNewFile();
- raf=new RandomAccessFile(file,"rw");
- byte[] buf=new byte[2048];
- int num=bn.read(buf);
- while(num!=(-1)){//是否读完所有数据
- raf.write(buf,0,num);//将数据写往文件
- count=count+num;
- raf.skipBytes(num);//顺序写文件字节
- num=bn.read(buf);//继续从网络中读取文件
- }
- bn.close();
- raf.close();
- } catch (IOException e) {
- hs.put(tempfilename, new Integer(count));
- e.printStackTrace();
- return false;
- }finally{
- if(raf!=null){
- try {
- raf.close();
- } catch (IOException e) {
- return false;
- }
- }
- }
- hs.put(tempfilename, new Integer(count));
- return true;
- }
- /**
- * 接受断点文件
- * @return
- */
- public boolean recevicefollowFile(){
- //使用本地文件系统接受网络数据并存为新文件
- File file=new File(servercepath+tempfilename);
- RandomAccessFile raf=null;
- int count=0;
- try {
- raf=new RandomAccessFile(file,"rw");
- byte[] buf=new byte[2048];
- int num=bn.read(buf);
- while(num!=(-1)){//是否读完所有数据
- raf.skipBytes(point);//顺序写文件字节
- raf.write(buf,0,num);//将数据写往文件
- count=count+num;
- raf.skipBytes(num);//顺序写文件字节
- num=bn.read(buf);//继续从网络中读取文件
- }
- bn.close();
- raf.close();
- } catch (IOException e) {
- hs.put(tempfilename, new Integer(count));
- e.printStackTrace();
- return false;
- }finally{
- if(raf!=null){
- try {
- raf.close();
- } catch (IOException e) {
- return false;
- }
- }
- }
- hs.put(tempfilename, new Integer(count));
- return true;
- }
- /**
- * 发送消息
- * @return 返回的消息
- * @throws IOException
- */
- public void sendMessage(String str) throws IOException {
- byte[] bw = str.getBytes();
- try {
- bo.write(bw);
- bo.flush();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- }
- /**
- * 关闭流和socket
- *
- */
- public void destory() {
- if (bo != null) {
- try {
- bo.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (bn != null) {
- try {
- bn.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- if (sc != null) {
- try {
- sc.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- public void run() {
- while(true){
- try {
- String str=receiverMessage() ;
- if(str!=null){
- if(str.substring(0, 1).equalsIgnoreCase("1")){
- System.out.println("认证处理");
- String [] strarry=str.split(",");
- if(strarry[1].equalsIgnoreCase("admin")&&strarry[2].equalsIgnoreCase("admin")){
- System.out.println("认证成功");
- sendMessage("ok");
- }else{
- System.out.println("认证失败");
- sendMessage("error");
- }
- }else if(str.substring(0, 1).equalsIgnoreCase("2")){//上传请求处理
- String [] strarry=str.split(",");
- tempfilename=strarry[1].substring(strarry[1].lastIndexOf("/")+1);
- System.out.println("文件接受:文件名为"+tempfilename);
- if(!hs.containsKey(tempfilename)){
- sendMessage("2,ok");
- if(receviceFile()){
- System.out.println("文件接受成功");
- }else{
- System.out.println("文件接受失败");
- }
- break;
- }else{
- sendMessage("2,error,"+((Integer)hs.get(tempfilename)).intValue());
- }
- }else if(str.substring(0, 1).equalsIgnoreCase("3")){//重传处理3,1. 续传3,2
- String [] strarry=str.split(",");
- if(strarry[1].equalsIgnoreCase("1")){
- System.out.println("客户端重新上传文件");
- sendMessage("3,ok");
- File fl=new File(servercepath+tempfilename);
- if(fl.exists())
- {
- fl.delete();
- }
- hs.remove(tempfilename);
- if(receviceFile()){
- System.out.println("文件接受成功");
- }else{
- System.out.println("文件接受失败");
- }
- break;
- }else if(strarry[1].equalsIgnoreCase("2")){
- System.out.println("客户端续传文件");
- sendMessage("3,ok");
- hs.remove(tempfilename);
- if(receviceFile()){
- System.out.println("续传接受成功");
- }else{
- System.out.println("续传接受失败");
- }
- break;
- }
- }
- }
- } catch (IOException e) {
- }
- }
- }
- }
java 代码
- package filetranslate;
- import java.io.*;
- import java.net.*;
- import java.util.HashMap;
- /**
- * 文件传输服务器端口
- * @author sydxide
- *
- */
- public class FileServer {
- ServerSocket ss = null;
- Socket s = null;
- DataInputStream inStream = null;
- DataOutputStream outStream = null;
- String servercepath="d:/socketserver/";
- String tempfilename=null;
- static int point=0;
- HashMap hs=new HashMap();
- public FileServer() {
- try {
- ss = new ServerSocket(5678);
- init();
- } catch (Exception e) {
- System.out.println(e.toString());
- }
- }
- /**
- * 初始化SOCKET
- * @throws Exception
- */
- public void init() throws Exception {
- s = ss.accept();
- System.out.println("客户端连接了服务器 "+s.getInetAddress());
- if(inStream==null){
- inStream = new DataInputStream(s.getInputStream());}
- if(outStream==null){
- outStream = new DataOutputStream(s.getOutputStream());}
- process();
- }
- /**
- * 接受发送消息
- * @return 返回的消息
- * @throws IOException
- */
- public String receiverMessage() throws IOException {
- int num = 0;
- byte[] br = null;
- br = new byte[2048];
- num = inStream.read(br);
- if(num!=-1){
- return new String(br, 0, num);
- }else{
- return null;
- }
- }
- /**
- * 根据接受的参数进行处理
- *
- */
- public void process(){
- while(true){
- try {