当前位置:文档之家› 线程实验报告

线程实验报告

线程实验报告黄澄宇3200709112411.分析(1)问题阐述,描述。

运用线程的方法设计一个ftp面向客户\服务器的网络应用程序,主要包括服务器端和客户端两方面的工作。

(2)问题分析。

针对问题要求,应该能够实现客户端和服务器端的功能。

其中服务器端主要对客户端进行监听。

客户端向服务器端进行提出请求。

(3)寻找揭发。

TCP\IP服务器端应用程序都是通过JA V A语言中提供的SeverSocket和Socket两个有关网络的类来实现。

SeverSocket类除了建立一个Sever之外,还通过accept()方法提供了随时监听客户端连接请求的功能。

2.设计。

在服务器端指定一个用来等待连接的端口号,在客户端规定一个主机和端口号,从而在客户端和服务器端创建Socket\ServerSocket实例。

现在服务器端生成一个ServerSocket实例的对象,随时监听客户端的连接请求。

当客户端需要连接时,相应的要生成一个Socket实例的对象,并发出连接请求,其在host参数指明该主机名,port参数指明端口号。

服务器端通过accept()方法接收到客户端的请求后,开辟一个接口与之进行连接,利用输入输出流,按照一定的协议对Socket 进行读写操作。

关闭输入输出流和Socket。

3.实现。

//FTPServer.javaimport .*;import java.io.*;import java.util.*;public class FTPServer{public static void main(String args[]) throws Exception{ServerSocket soc=new ServerSocket(5127);System.out.println("FTP Server Started on Port Number ");while(true){System.out.println("Waiting for Connection..."); transferfile t=new transferfile(soc.accept());}}}class transferfile extends Thread{Socket ClientSoc;DataInputStream din;DataOutputStream dout;transferfile(Socket soc){try{ClientSoc=soc;din=new DataInputStream(ClientSoc.getInputStream()); dout=new DataOutputStream(ClientSoc.getOutputStream()); System.out.println("FTP Client Connected...");start();}catch(Exception ex){}}void SendFile() throws Exception{String filename=din.readUTF();File f=new File(filename);if(!f.exists()){dout.writeUTF("File Not Found"); return;}else{dout.writeUTF("READY"); FileInputStream fin=new FileInputStream(f); int ch;do{ch=fin.read();dout.writeUTF(String.valueOf(ch));}while(ch!=-1);fin.close();dout.writeUTF("File Receive Successfully"); }}void ReceiveFile() throws Exception{String filename=din.readUTF();if(pareTo("File not found")==0) {return;}File f=new File(filename);String option;if(f.exists()){dout.writeUTF("File Already Exists"); option=din.readUTF();}else{dout.writeUTF("SendFile");option="Y";if(pareTo("Y")==0){FileOutputStream fout=new FileOutputStream(f); int ch;String temp;do{temp=din.readUTF();ch=Integer.parseInt(temp);if(ch!=-1){fout.write(ch);}}while(ch!=-1);fout.close();dout.writeUTF("File Send Successfully");}else{return;}public void run(){while(true){try{System.out.println("Waiting for Command..."); String Command=din.readUTF();if(pareTo("GET")==0){System.out.println("\tGET Command Received..."); SendFile();continue;}else if(pareTo("SEND")==0){System.out.println("\tSEND Command Receiced "); ReceiveFile();continue;}else if(pareTo("DISCONNECT")==0)System.out.println("\tDisconnect Command Received "); System.exit(1);}}catch(Exception ex){}}}}/FTPClient.javaimport .*;import java.io.*;import java.util.*;class FTPClient{public static void main(String args[]) throws Exception {Socket soc=new Socket("127.0.0.1",5127); transferfileClient t=new transferfileClient(soc);t.displayMenu();}class transferfileClient{Socket ClientSoc;DataInputStream din;DataOutputStream dout;BufferedReader br;transferfileClient(Socket soc){try{ClientSoc=soc;din=new DataInputStream(ClientSoc.getInputStream()); dout=new DataOutputStream(ClientSoc.getOutputStream()); br=new BufferedReader(new InputStreamReader(System.in)); }catch(Exception ex){}}void SendFile() throws ExceptionString filename;System.out.print("Enter File Name :");filename=br.readLine();File f=new File(filename);if(!f.exists()){System.out.println("File not Exists");dout.writeUTF("File not found");return;}dout.writeUTF(filename);String msgFromServer=din.readUTF();if(pareTo("File Already Exists")==0){String Option;System.out.println("File Already Exists Want to OverWrite (Y/N) ?"); Option=br.readLine();if(Option=="Y"){dout.writeUTF("Y");}else{dout.writeUTF("N");return;}}System.out.println("Sending File..."); FileInputStream fin=new FileInputStream(f); int ch;do{ch=fin.read();dout.writeUTF(String.valueOf(ch));}while(ch!=-1);fin.close();System.out.println(din.readUTF());}void ReceiveFile() throws Exception{String fileName;System.out.print("Enter File Name :"); fileName=br.readLine();dout.writeUTF(fileName);String msgFromServer=din.readUTF();if(pareTo("File Not Found")==0){System.out.println("File not found on Server...");return;}else if(pareTo("READY")==0){System.out.println("Receiving File... ");File f=new File(fileName);if(f.exists()){String Option;System.out.println("File Already Exists. Want to OverWrite (Y/N) ?"); Option=br.readLine();if(Option=="N"){dout.flush();return;}}FileOutputStream fout=new FileOutputStream(f); int ch;String temp;do{temp=din.readUTF();ch=Integer.parseInt(temp);if(ch!=-1){fout.write(ch);}}while(ch!=-1);fout.close();System.out.println(din.readUTF());}}public void displayMenu() throws Exception {while(true){System.out.println("[ MENU ]");System.out.println("1.Send File");System.out.println("2.Receive File"); System.out.println("3.Exit"); System.out.print("\nEnter Choice :"); int choice;choice=Integer.parseInt(br.readLine()); if(choice==1){dout.writeUTF("SEND");SendFile();}else if(choice==2){dout.writeUTF("GET"); ReceiveFile();}else{dout.writeUTF("DISCONNECT"); System.exit(1);}}}}。

相关主题