全文预览已结束
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
这是一个简单的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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年菏泽学院单招职业适应性测试题库参考答案详解
- 2026年重庆商务职业学院单招职业技能测试题库及答案详解1套
- 2026广东茂名市信宜市卫生健康系统事业单位赴南方医科大学招聘卫生专业技术人员74人(公共基础知识)综合能力测试题带答案解析
- 2026年辽宁城市建设职业技术学院单招综合素质考试题库及参考答案详解
- 2026年闽江师范高等专科学校单招职业技能考试题库附答案
- 2025陕西安康宁陕县村级植保员选聘10人(公共基础知识)综合能力测试题带答案解析
- 2025广东省铁路建设投资集团有限公司社会招聘(公共基础知识)综合能力测试题附答案解析
- 2025广西南宁市第五中学秋季学期招聘教职工(编外聘用)1人(公共基础知识)综合能力测试题带答案解析
- 2025广西柳州市自然资源调查和保护中心公开招聘1人(公共基础知识)综合能力测试题附答案解析
- 2026年石家庄工程职业学院单招综合素质考试题库附答案
- 国开电大《11837行政法与行政诉讼法》期末答题库(机考字纸考)排序版 - 稻壳阅读器2025年12月13日12时58分54秒
- 2025年部队卫生员考试题库
- 【MOOC】分子生物学-华中农业大学 中国大学慕课MOOC答案
- 矿压动态监测工操作规程
- 《动画片中的场景气氛研究开题报告》
- 课程设计-逻辑信号电平测试器的设计
- GB/T 17521-1998化学试剂N,N-二甲基甲酰胺
- GB/T 1094.1-2013电力变压器第1部分:总则
- GA/T 744-2013汽车车窗玻璃遮阳膜
- GA/T 55-2011物证通用标签
- 奎屯河 流域概况
评论
0/150
提交评论