3

I wrote a very simple Java http server for exercising purposes. I test it with cURL and everything seems to work fine but when I try to send a request from a browser

http://localhost:6666/

the server does not respond. I even put a marking System.out.println() at the point when the server socket accepts a connection which doesn't seem to fire when i try to hit the server through a browser. Please help me out with this. Thanks :)

EDIT: Part of the code:

public class Server {

    private ServerSocket serverSocket;
    private Socket socket;
    public Server() {
        try {
            serverSocket = new ServerSocket(6666);
            while (true) {
                socket = serverSocket.accept();
                System.out.println("Whoop! Connection!");
                Request request = new Request(socket);
                request.run();
            }
        } catch (IOException e) {
            e.printStackTrace();
          }
    }
}

Where Request is a class which extends Thread in order to handle multiple requests

nook
  • 2,378
  • 5
  • 34
  • 54
user1990198
  • 169
  • 1
  • 3
  • 13

3 Answers3

2

(I'm assuming you are using exactly the same URL in the browser and using curl ...)

If the browser is running on a different host to the service, then the reason is that localhost IP addresses (e.g. 127.0.0.1) are not routed to any other hosts apart from the host they were sent from. (That's what "local" means ...) In short, this is normal behaviour. (And maybe you are running curl and the browser on different hosts.)

If the browser is running on the same host as the service, this behaviour is a bit puzzling. However there are some possible explanations:

  • You may have some strange network proxy settings in your browser. For example, if you configure the browser to send ALL http requests (including 127.0.0.1) to an HTTP proxy on another machine, when the proxy relays the request to the real machine, it will go to the wrong place.

  • The localhost domain name may be bound to some strange IP address; e.g. something other than a 127.x.x.x IP address. (It is a strange thing to do, but I've heard of misguided people doing it.)

  • The 127.0.0.1 IP address might have been bound to something other that the loopback network adapter. (I don't know if this is technically possible ... )

  • If you are using iptables to implement routing on a virtual network, you could be sending 127.0.0.1 packets to the wrong place. (I don't know if this is technically possible ... )

The first bullet seems most likely to me.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

Looks like port 6666 is considered unsafe by many browsers for security reasons. Please try some other ports may be port 3000 or 5000(I am just throwing a number here) it should work.

tonjo
  • 1,394
  • 1
  • 14
  • 27
Prasad
  • 9
  • 1
  • you didn't understand the concept. A port itself can not "have security issues". You are referring to a specific service, IRC (Internet Relay Chat), which has issues. And it would have issues even if it was run on another port. In the question, he's creating a new service, completely unrelated to IRC. – tonjo Feb 14 '20 at 09:16
  • Correct, but my point here is that port 6660 through 6669 (used for IRC) were targeted for malicious purpose in the past and that could be the reason if you try to open the http url with port 6666 in **Chrome** it doesn't work but same url works on **Microsoft Edge** or **Firefox** and this is strange. @tonjo Please try it for your self and let me know your findings. My suggestion is just use some another port. – Prasad Feb 17 '20 at 03:51
  • Firefox on ubuntu clearly says "This address is restricted", so that is the reason. Any port could be targeted for malicious purposes, what kind of security would be simply not using certain ports? Nonsense. No need to be a Java expert (I am no), but even if you change port, that service using a generic `ServerSocket` is *not* an HTTP server. Try looking here, maybe it clarifies: https://stackoverflow.com/questions/10788125/a-simple-http-server-with-java-socket – tonjo Feb 17 '20 at 09:51
  • Agreed, may be I should have said __Looks like port 6666 has some restrictions from the certain browsers__ instead I mentioned **security** (my bad!). But @tonjo you got my point __Firefox on ubuntu clearly says "This address is restricted"__ bingo! :) (and it's ok if you are not Java expert) I appreciate your efforts. – Prasad Feb 17 '20 at 12:45
  • You are still completely missing the point. Your answer is wrong because you seem to ignore what an http server is. Have you tried the link? Try it, try also changing port. And study more. – tonjo Feb 17 '20 at 12:53
  • Question, Does your browser get any response from server on port 6666? yes or no. If your answer is **No** then please understand the original post just need to change the port because he already got the response using curl, so don't try to correct which is already working. Off course there are many better ways to create http server, but that is whole different topic. We are not expert here, everyone is trying to learn, I hope you understand. – Prasad Feb 17 '20 at 13:34
  • I edited your question, so now it's not misleading anymore, removed downvote, and upvoted. – tonjo Feb 17 '20 at 16:17
  • I appreciate your reconsideration and judgement here, thanks @tonjo – Prasad Feb 17 '20 at 16:35
0

Browsers only understand http and some other protocols, such as ftp. Your serverSocket does not implement any protocol. If you want to see something, maybe you can try in console

# telnet localhost 6666 

EDIT

Here another question (and answer) for a correct implementation of an HTTP server: A Simple Http Server with Java/Socket?

tonjo
  • 1,394
  • 1
  • 14
  • 27