Topic: 帮忙看看这段程序 (多线程web服务器)

  Print this page

1.帮忙看看这段程序 (多线程web服务器) Copy to clipboard
Posted by: little
Posted on: 2004-05-18 14:00

import java.util.*;
import java.io.*;
import java.net.*;

public final class mwebserver
{
public static void main(String args[]) throws Exception
{
int port = 6789;
//define the port
ServerSocket listensocket = new ServerSocket(port);
while (true)
{
Socket connectionsocket = listensocket.accept();
}

HttpRequest request = new HttpRequest(connectionsocket);

//creat a new thread to process the request
Thread thread1 = new Thread (request);
thread1.start();
}


static class HttpRequest implements Runnable
{
  final static String CRLF = "\r\n";
  Socket socket1;
  
  //constructor
  public HttpRequest(Socket socket1) throws Exception
  {
    this.socket1 = socket1;
  }
  
  //implement the run()method of the runnable interface
  public void run()
  {
    try
    {
      processrequest();
    }
    catch(Exception e)
    {
      System.out.println(e);
    }
private void processrequest() throws Exception
  {
    InputStream is = new InputStream ();
    DataOutputStream os = new DataOutputStream(socket1.getOutputStream());
  
   //set up input stream filters
  
   BufferedReader br = new BufferedReader(new InputStreamReader(socket1.getInputStream()));
  
   //get the request line of the http request message
   String requestline = br.readLine();
   System.out.println();
   System.out.println(requestline);
这是我编的多线程web服务器的一段源程序,每次编译的时候
HttpRequest request = new HttpRequest(connectionsocket);这一句都报错,请大家帮帮看看。

2.Re:帮忙看看这段程序 [Re: little] Copy to clipboard
Posted by: gouride
Posted on: 2004-05-19 08:43

把 HttpRequest 的 static 修饰符去掉看看

3.Re:帮忙看看这段程序 [Re: little] Copy to clipboard
Posted by: breezehou
Posted on: 2004-05-19 08:57

while (true)
{
Socket connectionsocket = listensocket.accept();
}

HttpRequest request = new HttpRequest(connectionsocket);
我想你的错误可能是,connectionsocket这个变量未定义,Socket connectionsocket = listensocket.accept();你在while语句里定义的,所以它的作用范围就在while语句里,可以这样写:

while(true)
{
Socket connectionsocket = listensocket.accept();
HttpRequest request = new HttpRequest(connectionsocket);

//creat a new thread to process the request
Thread thread1 = new Thread (request);
thread1.start();
}

4.Re:帮忙看看这段程序 [Re: little] Copy to clipboard
Posted by: little
Posted on: 2004-05-19 09:49

谢谢breezehou,我改了以后已经运行成功了!


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923