-3

I've been trying to connect a socket client with a URL. Am I supposed to add the URL's IP in the Windows host file ("C:\Windows\System32\drivers\etc\hosts")?

What is socket?
Sockets allow communication between two different processes on the same or different machines. To be more precise, it's a way to talk to other computers using standard Unix file descriptors. In Unix, every I/O action is done by writing or reading a file descriptor. A file descriptor is just an integer associated with an open file and it can be a network connection, a text file, a terminal, or something else.

But I've got this error:

java.net.UnknownHostException: https://socket.edu.net
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.io.DataInputStream.readLine()" because "this.input" is null
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Sunny
  • 33
  • 3
  • 10
  • The machine you run your application on cannot resolve that hostname. That can have a variety of reasons, none of which anyone here can tell with a certainty. – f1sh Jun 27 '22 at 12:13
  • The error is telling you that you tried to pass a complete URL where just a hostname is expected (ie, it was expecting only `"socket.edu.net"` but you gave it `"https://socket.edu.net"` instead). Which means your code/configuration has a bug that needs to be fixed. But nobody can help you do that because you didn't show your actual code to begin with. If you must take in a URL as input, you need to parse the URL to extract the hostname and port from it, and then you can connect using just those values instead. – Remy Lebeau Jun 27 '22 at 20:19

1 Answers1

-1

I solved the problem. I've changed https://socket.edu.net to socket.edu.net.

Then successfully connected.

EDIT: I restored hosts file and now in its original form and still connected successfully.

Sunny
  • 33
  • 3
  • 10
  • 1
    Modifying the `hosts` file is the wrong solution. Especially since you modified the client's code to match the file. So, just revert the file change to remove the redirect to localhost, but don't revert the code change so it keeps trying to connect to the hostname, and then the connect will likely work as expected. – Remy Lebeau Jun 27 '22 at 20:20
  • Modifying the `hosts` file in an incorrect way is *definitely* not a solution. The `hosts` file is there to map IP addresses to hostnames. URLs were not even invented when the `hosts` file was devised, and should not appear in them: and 127.0.0.1 should map to `localhost` and nothing else. You will break the world with shotgun techniques like this. Do not do this. – user207421 Jun 28 '22 at 00:07
  • Thank you so much for your advices. I restored hosts file and now in its original form and still connected. – Sunny Jun 29 '22 at 08:18