froggy_
froggy_

Reputation: 43

Server listenning at port with endpoint

I have created a server using sockets in Java and it is ready to receive HTTP requests and emit HTTP response. The server is on a port of my PC, but I want to listen only into an "endpoint", I mean, I want to mount the server i.e. on localhost:60802/json. Is this possible? My code is here:

            PORT = Integer.parseInt(portSelection.getText());
            th =  new Thread(new Runnable() {

                //String file;
                @Override
                public void run() {
                    try{
                        server = new ServerSocket(PORT);

                        while(open) {
                            //Accept connections
                            connection = server.accept();

                            in = new BufferedReader(new InputStreamReader(connection.getInputStream())); 

                            try {
                                String line="";

                                while ((line=in.readLine())!= null && !line.equals("")){log.append(line);}                                                              
                                in.close();
                            }catch (Exception e1) {
                            }
                        }
                    }catch (EOFException e1 ) {
                        e1.printStackTrace();
                    }catch (BindException e1) {
                        log.setText("Error. Port "+PORT+" already in use.");
                        open=false;
                    }catch (SocketException e1) {

                    }catch (IOException e1) {
                        e1.printStackTrace();
                    }
                }
            });
            th.start();

I have omitted some parts, so maybe you detect an error, but code works perfect at this time.

Upvotes: 0

Views: 750

Answers (3)

Stephen C
Stephen C

Reputation: 718698

I want to mount the server i.e. on localhost:60802/json. Is this possible?

You can listen on localhost port 60802. That will accept all connnections on that IP / port. Beyond that your server code needs to:

  1. read the HTTP request
  2. decode the first line to extract the request URL
  3. decide if the URL's path matches the path of your "endpoint" URL; i.e. "/json/*"
    • if yes, then continue
    • if no, then send an appropriate HTTP error response; e.g. 404 Not Found

(Alternatively, you could put your server code behind a reverse proxy that deals with that stuff and rewrites the request URL as well. But that may defeat your purpose in doing this using sockets ....)


But to be frank, writing an HTTP server using sockets is a bad idea. A lot of work, and you are likely to end up with a service that doesn't implement the HTTP specs correctly. You are better of using an existing web server / container (e.g. Tomcat, Jetty, Glassfish, SpringBoot, etcetera) or the Apache HttpComponents library.

Upvotes: 3

Luca Tampellini
Luca Tampellini

Reputation: 1849

If you use spring boot to mount your server and the web-service it will be much easier than to re-code everything.

Please have a look at: Building a RESTful Web Service with Spring Boot Actuator

Then you can develop an application for every group of web-services and for every group of web-services you can specify a particular port under the application.properties file:

server.port= 60802

Upvotes: 0

szogoon
szogoon

Reputation: 644

Server is bound to particular TCP port (and interface if provided). If you want to give response only at /json path just make a conditional statement that is checking URL.

Upvotes: 0

Related Questions