简体   繁体   English

Java套接字连接被拒绝

[英]Java socket connection refused

I am trying to run the "GreetingClient" code from the following page: http://www.tutorialspoint.com/java/java_networking.htm 我正在尝试从以下页面运行“ GreetingClient”代码: http : //www.tutorialspoint.com/java/java_networking.htm

The servername is being set to "localhost" and port 80. With these parameters I am able to run the program without any exceptions, but the code spins when it gets to in.readUTF() line. 服务器名被设置为“ localhost”和端口80。使用这些参数,我可以无任何例外地运行程序,但是当代码到达in.readUTF()行时,代码将旋转。

When the port is changed to any other port, I get a connection refused exception. 当端口更改为任何其他端口时,出现连接拒绝异常。

On the other hand, when I create a simple "ServerSocket" program that receives requests on port 80, I get "Address already in use" exception, any port other than 80 works for the "ServerSocket" code. 另一方面,当我创建一个简单的“ ServerSocket”程序以在端口80上接收请求时,会出现“ Address has in use”(地址已在使用中)异常,除80以外的任何端口都可用于“ ServerSocket”代码。

I have Red hat enterprise web server installed locally that uses port 80, but it is shutdown and does not run when these program are running. 我在本地安装了使用端口80的Red Hat Enterprise Web服务器,但是它已关闭并且在运行这些程序时无法运行。

Doing telnet gives me the following results: 进行telnet给我以下结果:

  1. telent localhost 80: Connection is not refused telent localhost 80:不拒绝连接
  2. telnet localhost 81 (or any other port other than 80): Connecting To localhost...Could not open connection to the host, on port 81: Connect failed telnet localhost 81(或80以外的任何其他端口):连接到localhost ...无法打开与主机的连接,端口81:连接失败

I have telnet client feature turned on. 我已打开telnet客户端功能。

What you're doing doesn't make sense. 你在做什么没有意义。 Clients and servers go together. 客户端和服务器在一起。 You can't connect any arbitrary client to any arbitrary server. 您不能将任意客户端连接到任意服务器。 They have to speak the same protocol, in this case a request/reply using writeUTF() and readUTF(). 他们必须说相同的协议,在这种情况下,使用writeUTF()readUTF().进行请求/答复readUTF().

This code will only work when connecting to the corresponding server in the same link. 仅当在同一链接中连接到相应的服务器时,此代码才有效。 You can't connect it to port 80, which is an HTTP server, not the GreetingServer, and expect it to work. 您无法将其连接到端口80(该端口是HTTP服务器,而不是GreetingServer,并且无法正常工作。 It's not an HTTP client, it is a client of its own server. 它不是HTTP客户端,而是其自己的服务器的客户端。 A telnet server won't work either, nor will aa Telnet client when connected to the GreetingServer. 连接到GreetingServer.时,telnet服务器也不起作用,Telnet客户端也不起作用GreetingServer.

'Connection refused' means there is nothing listening at that IP:port. “拒绝连接”表示在该IP:端口没有监听。

You need to run the GreetingServer on some port and next the client to that. 您需要在某个端口上运行GreetingServer ,然后在该客户端上运行它。

NB Curious code. NB好奇代码。 The server catches a SocketTimeoutException that is never thrown. 服务器捕获从未抛出的SocketTimeoutException

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

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