操作系统socket作业.doc_第1页
操作系统socket作业.doc_第2页
操作系统socket作业.doc_第3页
操作系统socket作业.doc_第4页
操作系统socket作业.doc_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

使用 socket 编程实现简单的文件服务器 题目介绍: 使用 socket 编程实现一个简单的文件服务器。客户端程序实现 put 功能(将一个文件从本地传到文件服务器)和 get 功能(从文件服务器取一远程文件存为本地文件)。客户端和文件服务器不在同一台机器上。 put -h hostname -p portname local_filename remote_filename get -h hostname -p portname remote_filename local_filename 程序简介: 本程序使用 Java 实现,开发环境为 MyEclipse8.01。 调用示例如下: put -h -p 4098 d:local.ppt remote.ppt get -h -p 4098 remote.ppt d:local.ppt 核心代码如下: /*-FTPClient.java-*/ package .dlut.FTP; import .*; import java.io.*; /* *Socket 实现文件服务器的客户端, *包含发送接收文件功能 */ public class FTPClient private Socket client; private boolean connected; public FTPClient(String host,int port) try /新建 socket 对象 client=new Socket(host,port); System.out.println(服务器连接成功!); this.connected=true; catch(UnknownHostException e) this.connected=false; closeSocket(); catch(IOException e) System.out.println(服务器连接失败); this.connected=false; closeSocket(); public boolean isConnected() return connected; public void setConnected(boolean connected) this.connected=connected; /* * 发送文件内容 * * param localFileName 本地文件的全路径名 * param remoteFileName 远程文件的名称 */ public void sendFile(String localFileName,String remoteFileName) DataOutputStream out=null;/写 Socket 的输出流 DataInputStream reader=null;/读取本地文件的输入流 try if(client=null)return; File file=new File(localFileName); /检查文件是否存在 if(!file.exists() System.out.println(本地文件不存在,请查看文件名是否写错! ); this.connected=false; this.closeSocket(); return; /初始化本地文件读取流 reader=new DataInputStream(new BufferedInputStream(new FileInputStream(file); /将指令和文件名发送到 Socket 的输出流中 out=new DataOutputStream(client.getOutputStream(); out.writeUTF(put +remoteFileName);/将远程文件名发送出去 out.flush(); /开始发送文件 int bufferSize=10240;/10k byte buf=new bytebufferSize; int read=0; while(read=reader.read(buf)!=-1) out.write(buf, 0, read); out.flush(); System.out.println(发送成功!); catch(IOException ex) ex.printStackTrace(); closeSocket(); finally try reader.close(); out.close(); catch(IOException e) e.printStackTrace(); /* * 接收文件内容 * * param localFileName 本地文件的全路径名 * param remoteFileName 远程文件的名称 */ public void recvFile(String remoteFileName,String localFileName) DataOutputStream out=null;/写 Socket 的输出流 DataInputStream in=null;/度 Socket 的输入流 DataOutputStream writer=null;/写本地文件的输出流 try if(client=null)return; writer=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(new File(localFileName); /将指令和文件名发送到 Socket 的输出流中 out=new DataOutputStream(client.getOutputStream(); out.writeUTF(get +remoteFileName);/将远程文件名发送出去 out.flush(); /开始接收文件 in=new DataInputStream(new BufferedInputStream(client.getInputStream(); int bufferSize=10240;/10k byte buf=new bytebufferSize; int read=0; while(read=in.read(buf)!=-1) writer.write(buf,0,read); writer.flush(); System.out.println(数据接收完毕!); catch(IOException ex) ex.printStackTrace(); closeSocket(); finally try out.close(); writer.close(); in.close(); catch(IOException e) e.printStackTrace(); /* * 关闭 socket */ public void closeSocket() if(client!=null) try client.close(); catch(IOException e) e.printStackTrace(); /* * param * put -h hostname-p portname local_filename remote_filename * 本地文件上传到远程服务器 * get -h hostname-p portname remote_filename local_filename *从远程服务器上下载文件*/public static void main(String args) String defaultHost=; String defaultPort=4098; if(args.length!=7) System.out.println(参数数目不正确!); return; String hostName=args2; int port=0; String localFileName=; String remoteFileName=; FTPClient client=null; try port=Integer.parseInt(args4); catch(Exception e) System.out.println(端口号输入错误!); return; if(args0.equals(put) /上传文件 client=new FTPClient(hostName,port); localFileName=args5; remoteFileName=args6; if(client.isConnected() client.sendFile(localFileName, remoteFileName); client.closeSocket(); else if(args0.equals(get) /下载文件 client=new FTPClient(hostName,port); localFileName=args6; remoteFileName=args5; if(client.isConnected() client.recvFile(remoteFileName,localFileName); client.closeSocket(); else System.out.println(命令不正确,正确命令如下:); System.out.println(put -h hostname -p portname local_filename remote_filename); System.out.println(get -h hostname -p portname remote_filename local_filename); /*-FTPServer.java-*/ package .dlut.FTP; import java.io.IOException; import .*; public class FTPServer private int port; private String host; private String dirPath; private static ServerSocket server; public FTPServer(int port,String dirPath) this.port=port; this.server=null; this.dirPath=dirPath; public void run() if(server=null) try /新建ServeSocket实例 server=new ServerSocket(port); catch(IOException e) e.printStackTrace(); catch(IOException e) e.printStackTrace(); System.out.println(服务启动.); while(true) try /访问ServerSocket的accept方法获取一个 /客户端Socket对象 Socket client=server.accept(); if(client=null)continue; new SocketConnection(client,this.dirPath).run(); catch(IOException ex) ex.printStackTrace(); public static void main(String args) int port=4098; String dirPath=D:ServerFTP; new FTPServer(port,dirPath).run(); public static void main(String args) int port=4098; String dirPath=D:ServerFTP; new FTPServer(port,dirPath).run(); public static void main(String args) int port=4098; String dirPath=D:ServerFTP; new FTPServer(port,dirPath).run(); /*- -SocketConnection.java- -*/ package .dlut.FTP; import java.io.*; import .*; public class SocketConnection extends Thread private Socket client; private String filePath; public SocketConnection(Socket client,String filePath) this.client=client; this.filePath=filePath; public void run() if(client=null)return; DataInputStream in=null;/读取 Socket 的输入流 DataOutputStream writer=null;/写文件的输出流 DataOutputStream out=null;/写 Socket 的输出流 DataInputStream reader=null;/读文件的输入流 try /访问 Socket 对象的 getInputStream 方法取得客户端发送过来的数据 流 in=new DataInputStream(new BufferedInputStream(client.getInputStream(); String recvInfo=in.readUTF();/取得附带的指令及文件名 String info=recvInfo.split( ); String fileName=info1;/获取文件名 if(filePath.endsWith(/)=false&filePath.endsWith()=false) filePath+=; filePath+=fileName; if(info0.equals(put) /从客户端上传到服务器 /开始接收文件 writer=new DataOutputStream(new BufferedOutputStream(new FileOutputStream(new File(filePath); int bufferSize=10240;/10k byte buf=new bytebufferSize; int read=0;System.out.p

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论