网站专题建设合同,图片免费设计在线生成,股票专业网站,工商登记查询网官网【实验目的及要求】 在 Uinx/Linux/Windows 环境下通过 socket 方式实现一个基于 Client/Server 文件传输程序。 【实验原理和步骤】 1. 确定传输模式:通过 socket 方式实现一个基于 Client/Server 或 P2P 模式的文件传输程序。 2. 如果选择的是 Client/Server 模式的文件传输…【实验目的及要求】 在 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 isnew InputStreamReader(System.in);
21 BufferedReader brnew BufferedReader(is);
22 System.out.print(Enter the port number: );
23 PortNumInteger.parseInt(br.readLine().trim());
24 ServerSocket ssnew ServerSocket(PortNum);
25 while(true){
26 System.out.println(Server get ready at port PortNum\nWaiting for connection);
27 FileTransfer ftnew 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 server11 * author alex12 *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_socketsocket;22 try {23 disnew DataInputStream(client_socket.getInputStream());24 dosnew 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 filenamedis.readUTF();65 File fnew File(filename);66 if(f.exists()){67 dos.writeUTF(File Already Existed);68 writedis.readUTF();69 }70 else writeY;71 if(write.equalsIgnoreCase(Y)){72 int ch;73 FileOutputStream foutnew FileOutputStream(f);74 String ttt;75 do{76 tttdis.readUTF();77 chInteger.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 filenamedis.readUTF();93 File fnew File(filename);94 if(!f.exists()){95 dos.writeUTF(File Not Existed);96 return;97 }else{98 dos.writeUTF(Dowloading File filename);99 FileInputStream finnew FileInputStream(f);
100 int ch;
101 do{
102 chfin.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 socket16 * author alex17 *18 */19 public class client {20 int PortNum;21 BufferedReader bfnew 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 PortNumInteger.parseInt(bf.readLine().trim());28 Socket socnew Socket(InetAddress.getByName(localhost),PortNum);29 disnew DataInputStream(soc.getInputStream());30 dosnew DataOutputStream(soc.getOutputStream());31 }32 33 public static void main(String[] args) throws UnknownHostException, IOException {34 client MyClientnew client();35 String option;36 while(true){37 optionMyClient.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 opbf.readLine();53 return op;54 }55 56 public void UploadFile() throws IOException{57 System.out.println(Enter the file name);58 String filenamebf.readLine().trim();59 File fnew 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 msgdis.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 writebf.readLine();72 }73 else writeY;74 dos.writeUTF(write);75 if(write.equalsIgnoreCase(Y)){76 System.out.println(Uploading File filename);77 FileInputStream finnew FileInputStream(f);78 int ch;79 do{80 chfin.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 filenamebf.readLine().trim();91 String msg,write;92 dos.writeUTF(DOWNLOAD);93 dos.writeUTF(filename);94 msgdis.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 fnew File(filename);
101 FileOutputStream fonew FileOutputStream(f);
102 int ch;
103 String ttt;
104 do{
105 tttdis.readUTF();
106 chInteger.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 转载于:https://www.cnblogs.com/alex-wood/p/3748781.html