简体   繁体   中英

Connecting to an “online” server through java ServerSocket

Is it possible to connect to a server like "www.google.com" for example server side. Not actually connecting through google.com but that is the idea.

ServerSocket serverSocket = new ServerSocket(0, 50, InetAddress.getByName("www.google.com"));

This code returns null, I am new to this connection related stuff so I don't know exactly what that would be meaning.

This is the code I have currently for setting up the server, it works fine when using localhost, but I am unsure on how to continue with an actual domain and server so anyone can connect and communicate.

If it isn't possible to set up a server like that, what is the best way to implement and I am trying to do.

you can open raw sockets to whatever you want, but a ServerSocket is if you want to be a server, not if you want to connect to one; you'd use normal Socket for that.

sockets is for raw TCP/IP. You run some service on top of it. For example, HTTP or HTTPS, which is what the web runs on, is built on top of it.

I really doubt you want to write an HTTP client just for www.google.com, it's rather complicated. Fortunately, java has one built in. Don't use Socket , use HttpClient .

See the java HTTP client tutorial here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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