简体   繁体   English

通过 java ServerSocket 连接到“在线”服务器

[英]Connecting to an “online” server through java ServerSocket

Is it possible to connect to a server like "www.google.com" for example server side.是否可以连接到像“www.google.com”这样的服务器,例如服务器端。 Not actually connecting through google.com but that is the idea.实际上并没有通过 google.com 连接,但这就是想法。

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.这段代码返回 null,我对这个连接相关的东西很陌生,所以我不知道这到底是什么意思。

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.这是我目前用于设置服务器的代码,它在使用 localhost 时工作正常,但我不确定如何继续使用实际的域和服务器,以便任何人都可以连接和通信。

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;您可以将原始 sockets 打开到任何您想要的位置,但是如果您想成为服务器,而不是连接到服务器,则可以使用ServerSocket you'd use normal Socket for that.你会为此使用普通的Socket

sockets is for raw TCP/IP. sockets 用于原始 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.例如,HTTP 或 HTTPS,这是 web 运行的基础,构建在它之上。

I really doubt you want to write an HTTP client just for www.google.com, it's rather complicated.我真的怀疑您是否只想为 www.google.com 编写 HTTP 客户端,这相当复杂。 Fortunately, java has one built in. Don't use Socket , use HttpClient .幸运的是,java 内置了一个。不要使用Socket ,使用HttpClient

See the java HTTP client tutorial here .请参阅此处的 java HTTP 客户端教程

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

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