最后完善的JAVA WEB服务器源代码(5) - 我的代码 - szxyz

(这条文章已经被阅读了 35 次) 时间:2001-04-11 08:56:03 来源:szxyz (zjxyz) 原创-IT

HttpConnect.java
————————————————-
package http;
import java.io.*;
import java.net.*;
import java.lang.*;

//——————– class ConnectThread —————–
public class HttpConnect{
Socket ConnectSocket=null;
ServerSocket ListenSocket=null;

byte readfromfile[];

public HttpConnect(){
System.out.print(“Start Server”);

try{
ListenSocket = new ServerSocket(Integer.parseInt(jwsconf.HttpConf(“Port”),10),
Integer.parseInt(jwsconf.HttpConf(“MaxKeepAliveRequests”),10));
}//end try
catch (IOException e) {}//end catch

System.out.println(” OK!”);

}//end HttpConnect()

public void Listen(){
for(;;){
try{
ConnectSocket=ListenSocket.accept();
}//end try
catch (IOException e) {
}

ConnectThread HttpThread=new ConnectThread(ConnectSocket);
HttpThread.start();
}//end for

}

}//end class HttpConnect

class ConnectThread extends Thread{

private HttpRequest httprequest = null;
private HttpResponse httpresponse = null;

Socket Client;
BufferedReader receivmsg;
PrintStream sendmsg;
String ReadFromClient[];

ConnectThread(Socket ConnectSocket){
Client=ConnectSocket;
}

public void run(){
int r=0;
System.out.print(“accept From ” + Client.getInetAddress().getHostAddress() + ”
“);

ReadFromClient=new String[1024];

try{
receivmsg =new BufferedReader(new InputStreamReader(Client.getInputStream()));

sendmsg =new PrintStream(Client.getOutputStream());

ReadFromClient[r++] = receivmsg.readLine();
//System.out.println(ReadFromClient[r]);

int c=0,off=0;
char[] readbytes=new char[1024*1024];

while(receivmsg.ready()){
c = receivmsg.read();
//System.out.print((char)c);

if (c == ‘
‘){
ReadFromClient[r]=new String(readbytes,0,off-1);
off=0;
r++;
continue;
}
readbytes[off++]=(char)c;
}

httprequest = new HttpRequest(ReadFromClient); //处理浏览器发来的请求
httpresponse = new HttpResponse(sendmsg,httprequest);
httpresponse.Toclient(); //输出信息到浏览器
CloseConnect();

}catch (IOException e) {
CloseConnect();
}//end catch

}//end run()

void CloseConnect(){
try{
receivmsg.close();
sendmsg.close();
Client.close();
}catch (IOException e) {}//end catch
}

}//——————– end class ConnectThread —————

作者:XYZ※版权所有,
转载请注明来源
【CGI编程设计室】
http://zjxyz.yeah.net