当前位置:文档之家› 分布式实验报告

分布式实验报告

西安邮电大学

(计算机学院)

课内实验报告

实验名称:简单的web服务器及浏览器

专业名称:

班级:

学生姓名:

学号(8位):

指导教师:

实验日期:2015年4月9日

一. 实验目的及实验环境

实验目的

熟悉网络编程,tcp基本知识;

通过socket方式实现一个基于Client/Server 或是P2P模式的文件传

输程序;

实验环境

Windows jdk1.6

二. 实验内容

1.构建简单的web服务器及客户端,客户端以命令行界面与用户进行交互

2. web服务器程序在端口创建一个socket,等待接入客户程序发来的请求,无论是否合法的请求,客户程序都应获得遵循RFC规范的响应。

三.方案设计

实验原理

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

件传程序。

2. 客户端:用面向连接的方式实现通信。采用Socket 类对象,接收服务

器发送的文件并保存在特定的位置。

3. 服务器端:监听客户请求,读取磁盘文件并向客户端发送文件。

实验过程

1.服务器先启动,等待客户端连接

2.客户端启动,连接到服务器

3.客户端输入文件路径,回车,开始传输文件

4.服务器接收文件。

5.服务端接收文件完毕,等待下次接收。

四.测试数据及运行结果

服务端截图:

客户端截图:

五.总结

用java语言能很容易地实现网络通信,使用socket可以传输任何类型的文件,文件读取时要熟悉java中文件读取的方式以及文件结束的标记。

1. 对java语言的应用有了进一步的熟悉。

2. 对于网络通信的实现更深入的了解和强化了自身使用socket的能力。

3. 加深了分布式计算的实现

六.附录:源代码(电子版)

服务端

import java.io.DataInputStream;

import java.io.File;

import java.io.FileOutputStream;

import https://www.doczj.com/doc/0a5044281.html,.ServerSocket;

import https://www.doczj.com/doc/0a5044281.html,.Socket;

/**

* 服务器

*/

public class HelloServer extends ServerSocket{

private static final int PORT =2014;

private ServerSocket server;

private Socket client;

private DataInputStream dis;

private FileOutputStream fos;

private int s=0;

public HelloServer()throws Exception{

try {

try {

server =new ServerSocket(PORT);

while(true){

System.out.println("----正在等待客户请求连接----");

client = server.accept();

System.out.println("----连接成功----");

dis =new DataInputStream(client.getInputStream());

//文件名和长度

String fileName = dis.readUTF();

long fileLength = dis.readLong();

fos =new FileOutputStream(new File("E:/" + fileName));

byte[] sendBytes =new byte[1024];

int transLen =0;

System.out.println("----开始接收文件<" + fileName +">,文件大小为<" + fileLength +">----");

while(true){

int read =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 (Exception e) {

e.printStackTrace();

}finally {

if(dis !=null)

dis.close();

if(fos !=null)

fos.close();

server.close();

}

}catch (Exception e) {

e.printStackTrace();

}

public static void main(String[] args)throws Exception { new HelloServer();

}

}

//客户端

import java.io.BufferedReader;

import java.io.DataOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStreamReader;

import https://www.doczj.com/doc/0a5044281.html,.Socket;

public class HelloClient extends Socket{

private static final String SERVER_IP ="127.0.0.1";

private static final int SERVER_PORT =2014;

private Socket client;

private FileInputStream fis;

private DataOutputStream dos;

public HelloClient(){

try {

try {

client =new Socket(SERVER_IP, SERVER_PORT);

//向服务端传送文件

BufferedReader strin=new BufferedReader(new InputStreamReader(System.in));

System.out.println("----连接成功----");

System.out.println("----请输入需要传输的文件路径名----");

String fileName = strin.readLine();

File file =new File(fileName);

fis =new FileInputStream(file);

dos =new DataOutputStream(client.getOutputStream());

//文件名和长度

dos.writeUTF(file.getName());

dos.flush();

dos.writeLong(file.length());

dos.flush();

//传输文件

byte[] sendBytes =new byte[1024];

int length =0;

while((length = fis.read(sendBytes,0, sendBytes.length)) >0){ dos.write(sendBytes,0, length);

dos.flush();

}

}catch (Exception e) {

e.printStackTrace();

}finally{

if(fis !=null)

fis.close();

if(dos !=null)

dos.close();

client.close();

}

}catch (Exception e) {

e.printStackTrace();

}

}

public static void main(String[] args)throws Exception { new HelloClient();

}

}

相关主题
文本预览
相关文档 最新文档