基于socket的简单文件传输系统

【实验目的及要求】

Uinx/Linux/Windows 环境下通过 socket 方式实现一个基于 Client/Server 文件传输程序。

【实验原理和步骤】

1. 确定传输模式:通过 socket 方式实现一个基于 Client/Server P2P 模式的文件传输程序。

2. 如果选择的是 Client/Server 模式的文件传输程序,则需要分别实现客户端和服务器端程序。客户端:用面向连接的方式实现通信。采用 Socket 类对象,接收服务器发送的文件并保存在特定的位置。服务器端:监听客户请求,读取磁盘文件并向客户端发送文件。注意:需要实现文件的读写操作。


【方案设计】


1)编写基于socket的文件传输系统中的public class server

2)将server中需要调用到的类与方法集合在public class FileTransfer extends Thread中;

3)在FileTransfer中完成ReceiveFile()SendFile()方法的声明;

4)编写客户端程序 public class client,在类client中完成主要用到的方法DownloadFile()UploadFile()方法的声明;

5)在ubuntu虚拟终端中利用java虚拟机对程序进行运行调试。



【实验环境】

ubuntu 12.04

java version "1.7.0_17"

Eclipse Platform Version: 4.2.1

 

怪无聊的,最初想写的是p2p文件分享系统,结果项目老师一个催促,让我心跳加快=。=什么都顾不上了,草草了事。。。不说废话了,这实验不难,直接上源码。

server

 

 1 import java.io.BufferedReader;
 2 import java.io.DataInputStream;
 3 import java.io.DataOutputStream;
 4 import java.io.IOException;
 5 import java.io.InputStreamReader;
 6 import java.net.ServerSocket;
 7 import java.net.Socket;
 8 
 9 /**
10  * Server of a file sharing system based on socket
11  * @author alex
12  *
13  */
14 
15 public class server {
16 
17     
18     public static void main(String[] args) throws IOException{
19         int PortNum;
20         InputStreamReader is=new InputStreamReader(System.in);
21         BufferedReader br=new BufferedReader(is);
22         System.out.print("Enter the port number: ");
23         PortNum=Integer.parseInt(br.readLine().trim());
24         ServerSocket ss=new ServerSocket(PortNum);
25         while(true){
26             System.out.println("Server get ready at port "+PortNum+"\nWaiting for connection");
27             FileTransfer ft=new FileTransfer(ss.accept());
28         }
29     }
30     
31 }
View Code

 

 

FileTransfer

  1 import java.io.DataInputStream;
  2 import java.io.DataOutputStream;
  3 import java.io.File;
  4 import java.io.FileInputStream;
  5 import java.io.FileOutputStream;
  6 import java.io.IOException;
  7 import java.net.Socket;
  8 
  9 /**
 10  * FileTransfer of a file sharing system based on socket, of which the methods are used in the server
 11  * @author alex
 12  *
 13  */
 14     public class FileTransfer extends Thread{
 15 
 16         DataInputStream dis;
 17         DataOutputStream dos;
 18         Socket client_socket;
 19         
 20         public FileTransfer(Socket socket){
 21             client_socket=socket;
 22             try {
 23                 dis=new DataInputStream(client_socket.getInputStream());
 24                 dos=new DataOutputStream(client_socket.getOutputStream());
 25                 System.out.println("Socket connected");
 26                 start();
 27             } catch (IOException e) {
 28                 e.printStackTrace();
 29             }
 30             
 31         }
 32         
 33         
 34         public void run(){    
 35             System.out.println("Connection Established");
 36             while(true){
 37                 System.out.println("Waiting for client command... ");
 38                 String cd;
 39                 try {
 40                     cd = dis.readUTF();
 41                     if(cd.equalsIgnoreCase("DOWNLOAD")){
 42                         System.out.println("Client requests to download");
 43                         SendFile();
 44                     }
 45                     else if(cd.equalsIgnoreCase("UPLOAD")){
 46                         System.out.println("Client requests to upload");
 47                         ReceiveFile();
 48                     }
 49                     else if(cd.equalsIgnoreCase("DISCONNECT")){
 50                         System.out.println("Connection shutdown...");
 51                         System.exit(1);
 52                     }
 53                 } catch (IOException e) {
 54                     e.printStackTrace();
 55                 }
 56             }
 57             
 58         }
 59 
 60 
 61         private void ReceiveFile() {
 62             try {
 63                 String write;
 64                 String filename=dis.readUTF();
 65                 File f=new File(filename);
 66                 if(f.exists()){
 67                     dos.writeUTF("File Already Existed");
 68                     write=dis.readUTF();
 69                 }
 70                 else write="Y";
 71                 if(write.equalsIgnoreCase("Y")){
 72                     int ch;
 73                     FileOutputStream fout=new FileOutputStream(f);
 74                     String ttt;
 75                     do{
 76                         ttt=dis.readUTF();
 77                         ch=Integer.parseInt(ttt);
 78                         fout.write(ch);
 79                     }while(ch!=-1);
 80                     fout.close();
 81                     dos.writeUTF("File Uploaded");
 82                 }
 83                 else return;
 84             } catch (IOException e) {
 85                 e.printStackTrace();
 86             }
 87             
 88         }
 89 
 90 
 91         private void SendFile() throws IOException {
 92             String filename=dis.readUTF();
 93             File f=new File(filename);
 94             if(!f.exists()){
 95                 dos.writeUTF("File Not Existed");
 96                 return;
 97             }else{
 98                 dos.writeUTF("Dowloading File "+filename);
 99                 FileInputStream fin=new FileInputStream(f);
100                 int ch;
101                 do{
102                     ch=fin.read();
103                     dos.writeUTF(String.valueOf(ch));
104                 }while(ch!=-1);
105                 fin.close();
106                 dos.writeUTF("File "+filename+" Download Successfully");
107             }
108             
109         }
110         
111     }
View Code

 

client

  1 import java.io.BufferedReader;
  2 import java.io.DataInputStream;
  3 import java.io.DataOutputStream;
  4 import java.io.File;
  5 import java.io.FileInputStream;
  6 import java.io.FileOutputStream;
  7 import java.io.IOException;
  8 import java.io.InputStreamReader;
  9 import java.net.InetAddress;
 10 import java.net.Socket;
 11 import java.net.UnknownHostException;
 12 
 13 
 14 /**
 15  * Client of a file sharing system based on socket
 16  * @author alex
 17  *
 18  */
 19 public class client {
 20     int PortNum;
 21     BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
 22     DataInputStream dis;
 23     DataOutputStream dos;
 24     
 25     public client() throws UnknownHostException, IOException{
 26         System.out.println("Enter the port number:");
 27         PortNum=Integer.parseInt(bf.readLine().trim());
 28         Socket soc=new Socket(InetAddress.getByName("localhost"),PortNum);
 29         dis=new DataInputStream(soc.getInputStream());
 30         dos=new DataOutputStream(soc.getOutputStream());
 31     }
 32     
 33     public static void main(String[] args) throws UnknownHostException, IOException {
 34         client MyClient=new client();
 35         String option;
 36         while(true){
 37             option=MyClient.display();
 38             switch(option){
 39             case "1":    MyClient.DownloadFile();break;
 40             case "2":    MyClient.UploadFile();break;
 41             case "3":    MyClient.Disconnect();break;
 42                 default: break;
 43             }
 44         }
 45     }
 46     
 47     public String display() throws IOException{
 48         System.out.println("Choose the operation you want to make:");
 49         System.out.println("1.Download A File");
 50         System.out.println("2.Upload A File");
 51         System.out.println("3.Disconnect");
 52         String op=bf.readLine();
 53         return op;
 54     }
 55     
 56     public void UploadFile() throws IOException{
 57         System.out.println("Enter the file name");
 58         String filename=bf.readLine().trim();
 59         File f=new File(filename);
 60         String msg,write;
 61         if(!f.exists()){
 62             System.out.println("The File "+ filename+" Not Found!");
 63             return;
 64         }
 65         else{
 66             dos.writeUTF("UPLOAD");
 67             dos.writeUTF(filename);
 68             msg=dis.readUTF();
 69             if(msg.equalsIgnoreCase("File Already Existed")){
 70                 System.out.println("File Already Existed On Server, Do You Want To Overwrite it?(Y/N)");
 71                 write=bf.readLine();
 72             }
 73             else write="Y";
 74             dos.writeUTF(write);
 75             if(write.equalsIgnoreCase("Y")){
 76                 System.out.println("Uploading File "+filename);
 77                 FileInputStream fin=new FileInputStream(f);
 78                 int ch;
 79                 do{
 80                     ch=fin.read();
 81                     dos.writeUTF(String.valueOf(ch));
 82                 }while(ch!=-1);
 83                 fin.close();
 84                 System.out.println(dis.readUTF());
 85             }
 86         }
 87     }
 88     public void DownloadFile() throws IOException{
 89         System.out.println("Enter the file name");
 90         String filename=bf.readLine().trim();
 91         String msg,write;
 92         dos.writeUTF("DOWNLOAD");
 93         dos.writeUTF(filename);
 94         msg=dis.readUTF();
 95         if(msg.equalsIgnoreCase("File Not Existed")){
 96             System.out.println("File "+filename+" Not Found On Server");
 97             return;
 98         }else{
 99             System.out.println(msg);
100             File f=new File(filename);
101             FileOutputStream fo=new FileOutputStream(f);
102             int ch;
103             String ttt;
104             do{
105                 ttt=dis.readUTF();
106                 ch=Integer.parseInt(ttt);
107                 fo.write(ch);
108             }while(ch!=-1);
109             fo.close();
110             System.out.println(dis.readUTF());
111             return;
112         }
113             
114     }
115     public void Disconnect() throws IOException{
116         dos.writeUTF("DISCONNECT");
117         System.exit(1);
118     }
119 }
View Code

 

 

posted on 2014-05-23 21:02  alex_wood  阅读(1547)  评论(0编辑  收藏  举报