多线程代理应用程序无法正常工作

时间:2016-04-17 22:48:32

标签: java multithreading sockets proxy

我正在尝试创建代理应用程序,但我在服务器套接字中遇到问题。服务器套接字不接受连接并返回套接字。因此,我无法测试代理应用程序。有什么问题?

问题行在WebServe.java中显示:

public class WebServe implements Runnable {
    Socket soc;
    OutputStream os;
    BufferedReader is;
    String resource;

    WebServe(Socket s) throws IOException {
        soc = s;
        os = soc.getOutputStream();
        is = new BufferedReader(new InputStreamReader(soc.getInputStream()));
    }

    public void run() {
        System.err.println("Running");
        getRequest();
        returnResponse();
        close();
    }

    public static void main(String args[]) {
        try {
            System.out.println("Proxy Thread");
            ServerSocket s = new ServerSocket(8080);
            for (;;) {
                s.setSoTimeout(10000);
                WebServe w = new WebServe(s.accept()); // Problem is here
                Thread thr = new Thread(w);
                thr.start();
                w.getRequest();
                w.returnResponse();
                w.close();
            }

        } catch (IOException i) {
            System.err.println("IOException in Server");
        }
    }

    void getRequest() {
        System.out.println("Getting Request");
        try {
            String message;
            while ((message = is.readLine()) != null) {
                if (message.equals("")) {
                    break;
                }
                System.err.println(message);
                StringTokenizer t = new StringTokenizer(message);
                String token = t.nextToken();
                if (token.equals("GET")) {
                    resource = t.nextToken();
                }
            }
        } catch (IOException e) {
            System.err.println("Error receiving Web request");
        }
    }

    void returnResponse() {
        int c;
        try {
            FileInputStream f = new FileInputStream("." + resource);
            while ((c = f.read()) != -1) {
                os.write(c);
            }
            f.close();
        } catch (IOException e) {
            System.err.println("IOException is reading in web");
        }
    }

    public void close() {
        try {
            is.close();
            os.close();
            soc.close();
        } catch (IOException e) {
            System.err.println("IOException in closing connection");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

public static void main(String args[]){     
    try {
        System.out.println("Proxy Thread");
        ServerSocket s = new ServerSocket (8080);
        for (;;){
            s.setSoTimeout(10000);

将其移到循环之前。您无需继续设置它。实际上你根本不需要它。

            WebServe w  = new WebServe (s.accept()); //Problem is here

问题出在这里只是因为你设置了一个实际上并不需要的套接字超时。

            Thread thr = new Thread (w);
            thr.start();

到目前为止一切顺利。

            w.getRequest();
            w.returnResponse();
            w.close();

删除。下一个问题就在这里。 run() WebServ方法已经执行此操作。

至于其余部分,您没有在响应中编写HTTP标头。