2023年分布式实验报告_第1页
2023年分布式实验报告_第2页
2023年分布式实验报告_第3页
2023年分布式实验报告_第4页
2023年分布式实验报告_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

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

文档简介

华南理工大学《分布式计算技术》课程实验报告实验题目:___Socket程序设计____________姓名:_____________学号:________________班级:______________组别:________合作者:_____________指导教师:__________实验概述【实验目的及规定】

1.在Uinx/Linux/Windows环境下通过socket方式实现一个基于Client/Server或是P2P模式的文献传输程序。

【实验原理】

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

2.客户端:用面向连接的方式实现通信。采用Socket类对象,接受服务器发送的文献并保存在特定的位置。3.服务器端:监听客户请求,读取磁盘文献并向客户端发送文献。【实验环境】

Windows环境,jdk1.6

实验过程(设计过程与程序演示)【实验方案设计】

服务端importjava.io.DataInputStream;importjava.io.File;importjava.io.FileOutputStream;importjava.net.ServerSocket;importjava.net.Socket;/***服务器*/publicclassHelloServerextendsServerSocket{privatestaticfinalintPORT=2023;privateServerSocketserver;privateSocketclient;privateDataInputStreamdis;privateFileOutputStreamfos;privateints=0;publicHelloServer()throwsException{try{try{server=newServerSocket(PORT);while(true){ﻩSystem.out.println("----正在等待客户请求连接----");client=server.accept();System.out.println("----连接成功----");dis=newDataInputStream(client.getInputStream());//文献名和长度StringfileName=dis.readUTF();longfileLength=dis.readLong();fos=newFileOutputStream(newFile("E:/"+fileName));byte[]sendBytes=newbyte[1024];inttransLen=0;System.out.println("----开始接受文献<"+fileName+">,文献大小为<"+fileLength+">----");while(true){intread=0;read=dis.read(sendBytes);if(read==-1)break;transLen+=read;if(s==0)ﻩSystem.out.println("接受文献进度"+100*transLen/fileLength+"%...");s=(s+1)%100;fos.write(sendBytes,0,read);fos.flush();}System.out.println("----接受文献<"+fileName+">成功-------");client.close();}}catch(Exceptione){e.printStackTrace();}finally{if(dis!=null)dis.close();if(fos!=null)fos.close();server.close();}}catch(Exceptione){e.printStackTrace();}}publicstaticvoidmain(String[]args)throwsException{newHelloServer();}}客户端importjava.io.BufferedReader;importjava.io.DataOutputStream;importjava.io.File;importjava.io.FileInputStream;importjava.io.InputStreamReader;importjava.net.Socket;/***客户端*/publicclassHelloClientextendsSocket{privatestaticfinalStringSERVER_IP="127.0.0.1";privatestaticfinalintSERVER_PORT=2023;privateSocketclient;privateFileInputStreamfis;privateDataOutputStreamdos;publicHelloClient(){try{try{client=newSocket(SERVER_IP,SERVER_PORT);//向服务端传送文献BufferedReaderstrin=newBufferedReader(newInputStreamReader(System.in));System.out.println("----连接成功----");System.out.println("----请输入需要传输的文献途径名----");StringfileName=strin.readLine();Filefile=newFile(fileName);fis=newFileInputStream(file);dos=newDataOutputStream(client.getOutputStream());//文献名和长度dos.writeUTF(file.getName());dos.flush();dos.writeLong(file.length());dos.flush();//传输文献byte[]sendBytes=newbyte[1024];intlength=0;while((length=fis.read(sendBytes,0,sendBytes.length))>0){dos.write(sendBytes,0,length);dos.flush();}}catch(Exceptione){e.printStackTrace();}finally{if(fis!=null)fis.close();if(dos!=null)dos.close();client.close();}}catch(Exceptione){e.printStackTrace();}}publicstaticvoidmain(String[]args)throwsException{newHelloClient();}}【实验过程】(实验环节、记录、数据、分析)1.服务器先启动,等待客户端连接2.客户端启动,连接到服务器3.客户端输入文献途径,回车,开始传输文献4.服务器接受文献。5.服务端接

温馨提示

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

评论

0/150

提交评论