【问题标题】:Multithreaded proxy application is not working多线程代理应用程序不工作
【发布时间】:2016-04-17 22:48:32
【问题描述】:

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

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");
        }
    }
}

【问题讨论】:

    标签: java multithreading sockets proxy


    【解决方案1】:
    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();
    

    删除。下一个问题就在这里。 WebServrun() 方法已经这样做了。

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

    【讨论】:

      猜你喜欢
      • 2013-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-19
      相关资源
      最近更新 更多