




全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
这是一个简单的Web服务器实现,主要功能就是根据用户请求在同一文件夹下查找对应文件并传输,找不到就传输错误页面。及运行截图如下:/测试网页在同一文件夹下import java.io.*;import .*;import java.util.*;import java.awt.*;import javax.swing.*;public class MultiThreadServer extends JFrame private JTextArea jta =new JTextArea();public static void main(String args)new MultiThreadServer();public MultiThreadServer() setLayout(new BorderLayout();/create the GUIadd(new JScrollPane(jta), BorderLayout.CENTER);setTitle(MultiThreadServer by Li Zhaoji);setSize(500, 300);setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setVisible(true);tryServerSocket serverSocket = new ServerSocket(8080);/wait request on port 8080jta.append(MultiThreadServer started at + new Date() + n);jta.append(Web Server is listening on port +serverSocket.getLocalPort() + n + n);int clientNo = 1;while (true) Socket socket = serverSocket.accept();/wait for requestjta.append(* + n);jta.append(Starting thread for client + clientNo + at + new Date() + n);InetAddress inetAddress = socket.getInetAddress();/get the client informationjta.append(Client + clientNo + s host name is + inetAddress.getHostName() + n);jta.append(Client + clientNo + s IP Address is + inetAddress.getHostAddress() + n);HandleAClient task = new HandleAClient(socket);new Thread(task).start();/create a new thread handle the requestclientNo+;catch(IOException ex) System.err.println(ex);class HandleAClient implements Runnable private Socket socket;public HandleAClient(Socket socket)this.socket = socket;public void run() tryBufferedReader inputFromClient = new BufferedReader(new InputStreamReader(socket.getInputStream();PrintStream outputToClient = new PrintStream(socket.getOutputStream();String url=inputFromClient.readLine();jta.append(The Server has a request + * + url + * + n);if(url.startsWith(GET)/get the requests document nameif(url.indexOf(HTTP/) 4)String filename = url.substring(5, url.indexOf(HTTP/)-1); File file=new File(filename); if (file.exists() /if the file exist in the server jta.append(file can be find! + n); jta.append(MultiThreadServer is working now! + n); DataInputStream localfiles = new DataInputStream(new FileInputStream(file); int n = localfiles.available(); byte buf = new byte1024; while(n = localfiles.read(buf) = 0) outputToClient.write(buf, 0, n);/start send with buf0, send n bytes /read the file and sent it to the client else if(url.indexOf(HTTP/) = 6) /if user doesnt input the file name, send default web page jta.append(Dont konw send which files, send defalut messenges! + n); DataInputStream localfiles = new DataInputStream(new FileInputStream(index.txt); int n = localfiles.available(); byte buf = new byte1024; while(n = localfiles.read(buf) = 0) outputToClient.write(buf, 0, n);/start send with buf0, send n bytes else /if the request file didnt exist jta.append(囧rzSorry!There is no file with the requested name! + n); DataInputStream localfiles = new DataInputStream(new FileInputStream(404notfound.htm); int n = localfiles.available(); byte buf = new byte1024; while(n = localfiles.read(buf) = 0) outputToClient.write(buf, 0,
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 地热开发增强型技术-洞察及研究
- 2025年公需科目测试及答案
- 设计院质量管理办法
- 订单管理办法适用于
- 不良事件管理办法分级
- 设备动态化管理办法
- 西藏车辆质押管理办法
- 大数据分发与处理的开源解决方案研究-洞察及研究
- 螺蛳粉采购管理办法
- 模拟游戏与儿童创造力-洞察及研究
- 家庭健康知识课件
- 拍卖保留价协议
- 医疗纠纷的防范与处理课件
- DG-TJ08-2062-2025 住宅工程套内质量验收标准
- 单位交纳公积金协议书
- 2025年4月自考00107现代管理学试题及答案含评分标准
- 烟气脱硝工艺培训课件
- 2025建筑工程施工标准合同
- 《激光切割机操作》课件
- 《电机与变压器》全套教学课件
- 电能质量技术 绪论、第1章 电能质量概述
评论
0/150
提交评论