简体   繁体   English

如何在Java中的服务器中侦听和生成客户端响应?

[英]How to listen and generate respones for client in server in java?

This is my server side code in java listening on port 1880 and making http client request from browser/client.java code in localhost which gives connection refused error.I copied that server code from a webpage.I am not sure what exactly this code is doing. 这是我的服务器端Java代码,侦听端口1880,并从浏览器/客户端发出HTTP客户端请求。本地主机中的Java代码给出了连接拒绝错误。我从网页复制了该服务器代码。我不确定此代码到底是什么在做。

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class ServerHandlingHttpRequest {
   public void start() throws IOException
    {
                InetSocketAddress addr=new InetSocketAddress(1880);
                System.out.println(addr.getPort());
                System.out.println(addr.getAddress());
                System.out.println(addr.getHostName());
                System.out.println(addr.toString());

                HttpServer server=HttpServer.create(addr,0);
                server.createContext("/", new MyHandler());
                server.setExecutor(null);
                server.start();
                System.out.println("Server Listening");
                System.out.println(server.getAddress());
                server.stop(60);
        }
      class MyHandler implements HttpHandler {
    public void handle(HttpExchange t) throws IOException {
        System.out.println("Request Arrived");
        String response = "This is the response";
        t.sendResponseHeaders(200, response.length());
        OutputStream os = t.getResponseBody();
        os.write(response.getBytes());
        os.close();
    }
}
      public static void main(String[] args) throws IOException
    {
          ServerHandlingHttpRequest w=new ServerHandlingHttpRequest();
          w.start();
     }
    }

That code simply starts listening to the port that you have specified, as an HTTP listener. 该代码只是作为HTTP侦听器开始侦听您指定的端口。 Which means, any HTTP Request that might come into that port will be intercepted by your program. 这意味着,任何可能进入该端口的HTTP请求都将被您的程序拦截。 And when it intercepts, it looks for the context path that you have configured to handle the response to the request. 当它拦截时,它会查找您配置为处理对请求的响应的上下文路径。

So, when you run this program, it starts listening at port 1880. When a URL http://localhost:1880/ is hit, it will be intercepted by the handler, MyHandler. 因此,当您运行该程序时,它将开始在端口1880进行监听。当击中URL http:// localhost:1880 /时 ,它将被处理程序MyHandler拦截。 The response will be written to the OutputStream which will send back the request back to the client. 响应将被写入OutputStream,它将把请求发送回客户端。

One of the problems that I see is that you are stopping the server after printing some statements, while the example that you have refered is not doing so. 我看到的问题之一是您在打印一些语句后停止服务器,而您引用的示例却没有这样做。 May be, remove server.stop(60) statement and check whether the issue still persists. 可能是,删除server.stop(60)语句并检查问题是否仍然存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM