用JAVA编写Web服务器.doc

上传人:汽*** 文档编号:550849179 上传时间:2023-04-12 格式:DOC 页数:7 大小:31.50KB
返回 下载 相关 举报
用JAVA编写Web服务器.doc_第1页
第1页 / 共7页
用JAVA编写Web服务器.doc_第2页
第2页 / 共7页
用JAVA编写Web服务器.doc_第3页
第3页 / 共7页
用JAVA编写Web服务器.doc_第4页
第4页 / 共7页
用JAVA编写Web服务器.doc_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《用JAVA编写Web服务器.doc》由会员分享,可在线阅读,更多相关《用JAVA编写Web服务器.doc(7页珍藏版)》请在金锄头文库上搜索。

1、WebServer.java 用JAVA编写Web服务器/WebServer.java 用JAVA编写Web服务器import java.io.*;import .*;public class WebServer public static void main(String args) int i=1, PORT=8080; ServerSocket server=nul/WebServer.java 用JAVA编写Web服务器import java.io.*;import .*;public class WebServer public static void main(String arg

2、s) int i=1, PORT=8080; ServerSocket server=null; Socket client=null; try server=new ServerSocket(PORT); System.out.println(Web Server is listening on port +server.getLocalPort(); for (;) client=server.accept(); /接受客户机的连接请求 new ConnectionThread(client,i).start(); i+; catch (Exception e) System.out.pr

3、intln(e); /* ConnnectionThread类完成与一个Web浏览器的通信 */class ConnectionThread extends Thread Socket client; /连接Web浏览器的socket字 int counter; /计数器 public ConnectionThread(Socket cl,int c) client=cl; counter=c; public void run() /线程体 try String destIP=client.getInetAddress().toString(); /客户机IP地址 int destport=c

4、lient.getPort(); /客户机端口号 System.out.println(Connection +counter+:connected to +destIP+ on port +destport+.); PrintStream outstream=new PrintStream(client.getOutputStream(); DataInputStream instream=new DataInputStream(client.getInputStream(); String inline=instream.readLine(); /读取Web浏览器提交的请求信息 Syste

5、m.out.println(Received:+inline); if (getrequest(inline) /如果是GET请求 String filename=getfilename(inline); File file=new File(filename); if (file.exists() /若文件存在,则将文件送给Web浏览器 System.out.println(filename+ requested.); outstream.println(HTTP/1.0 200 OK); outstream.println(MIME_version:1.0); outstream.prin

6、tln(Content_Type:text/html); int len=(int)file.length(); outstream.println(Content_Length:+len); outstream.println(); sendfile(outstream,file); /发送文件 outstream.flush(); else /文件不存在时 String msg1=Not FoundError 404-file not found; outstream.println(HTTP/1.0 404 no found); outstream.println(Content_Typ

7、e:text/html); outstream.println(Content_Length:+msg1.length()+2); outstream.println(); outstream.println(msg1); outstream.flush(); /instream.close(); /outstream.close(); long m1=1; /延时 while (m10) if (s.substring(0,3).equalsIgnoreCase(GET) return true; return false;/* 获取要访问的文件名 */String getfilename(

8、String s)String f=s.substring(s.indexOf( )+1);f=f.substring(0,f.indexOf( );try if (f.charAt(0)=/) f=f.substring(1); catch (StringIndexOutOfBoundsException e) System.out.println(Exception:+e); if (f.equals() f=index.html;return f;/*把指定文件发送给Web浏览器 */void sendfile(PrintStream outs,File file)try DataInp

9、utStream in=new DataInputStream(new FileInputStream(file); int len=(int)file.length(); byte buf=new bytelen; in.readFully(buf); outs.write(buf,0,len); /* DataOutputStream ou=new DataOutputStream(System.out); ou.write(buf,0,len); */ outs.flush(); in.close(); catch (Exception e) System.out.println(Error retrieving file.); System.exit(1);

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 科普知识

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号