📄 phttpd.java
字号:
// 偄傫偪偒HTTP僒乕僶Phttpd.java
// 偙偺僾儘僌儔儉偼億乕僩斣崋80斣偱摦嶌偡傞僒乕僶偱偡
// 巊偄曽java Phttp 僨乕僞僼傽僀儖柤
// WWW僋儔僀傾儞僩偐傜偺愙懕偵懳偟,堷悢偱巜掕偟偨僼傽僀儖傪曉偟傑偡
// 儔僀僽儔儕偺棙梡
import java .io.* ;
import java.net.* ;
import java.util.* ;
// Phttpd僋儔僗
class Phttpd{
public static void main(String args[]){
// 僒乕僶僜働僢僩偺弨旛
ServerSocket servsock = null ;
Socket sock ;
// 擖弌椡偺弨旛
OutputStream out ;
BufferedReader in ;
FileInputStream infile = null;
byte[] buff = new byte[1024];
// 偦偺懠偺曄悢
boolean cont = true ;
int i ;
try{
// 僒乕僶梡僜働僢僩偺嶌惉乮億乕僩斣崋80斣乯
servsock = new ServerSocket(80,300) ;
while(true){
sock = servsock.accept() ;// 愙懕梫媮偺庴晅
// 愙懕愭偺昞帵
System.out.println("愙懕梫媮"
+ (sock.getInetAddress()).getHostName()) ;
// 僆僽僕僃僋僩infile傪嶌傝,僼傽僀儖傪弨旛偟傑偡
try{
infile = new FileInputStream(args[0]) ;
}
catch(Exception e){
// 僼傽僀儖弨旛偺幐攕
System.err.println("僼傽僀儖偑偁傝傑偣傫") ;
System.exit(1) ;
}
// 撉傒彂偒梡僆僽僕僃僋僩偺惗惉
in = new BufferedReader(new
InputStreamReader(sock.getInputStream()));
out = sock.getOutputStream() ;
// 偲傝偁偊偢夵峴傪俀偮撉傒旘偽偡
for(i = 0; i < 2;++i)
in.readLine() ;
// 僼傽僀儖偺弌椡
cont = true ;
while(cont){
// 僼傽僀儖偐傜偺撉傒崬傒偲僱僢僩儚乕僋弌椡
try{
int n = infile.read(buff);
out.write(buff,0,n) ;
}
catch(Exception e){
cont = false ;
}
}
// 愙懕廔椆
sock.close() ;
infile.close() ;
}
}catch(IOException e){
System.exit(1) ;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -