更新一:通过使用TCP port checker,我能够验证AWS实例上的端口8080是否处于活动状态并接受连接。
这意味着端口可能不会以某种方式正确绑定,如果我设法解决此问题,将更新回来
此外,通过使用AWS服务器实例日志,/var/log/nginx/error.log下的条目表明连接被拒绝,尽管访问协议不受限制。
我有一个基于Socket的服务器,在localhost上运行完全正常。我启动服务器,然后启动实例客户端调用,行为符合预期和期望;服务器返回" Hello World"到客户端并终止连接 但是,如果我将服务器上传到AWS并将其作为Java应用程序托管,我就无法建立连接并重现localhost服务的行为。
HelloWorldServer:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class NetMiddlewareServer {
public static void main(String[] args) {
// TODO code application logic here
int portNumber = 8080;
while(true){
try (
ServerSocket serverSocket = new ServerSocket(portNumber);
Socket clientSocket = serverSocket.accept();
PrintWriter out =
new PrintWriter(clientSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(clientSocket.getInputStream()));
) {
System.out.println("Test");
String inputLine, outputLine;
// Initiate conversation with client
outputLine = "Hello World";
out.println(outputLine);
clientSocket.close();
} catch (IOException e) {
System.out.println("Exception caught when trying to listen on port "
+ portNumber + " or listening for a connection");
System.out.println(e.getMessage());
}
}
}
HelloWorldClient:
import java.io.*;
import java.net.*;
public class KKPClient {
public static void main(String[] args) throws IOException {
String hostName = "localhost";
int portNumber = 8080;
try (
Socket hwSocket = new Socket(hostName, portNumber);
PrintWriter out = new PrintWriter(hwSocket.getOutputStream(), true);
BufferedReader in = new BufferedReader(
new InputStreamReader(hwSocket.getInputStream()));
) {
BufferedReader stdIn =
new BufferedReader(new InputStreamReader(System.in));
String fromServer;
while ((fromServer = in.readLine()) != null) {
System.out.println("Server: " + fromServer);
}
} catch (UnknownHostException e) {
System.err.println("Don't know about host " + hostName);
System.exit(1);
}
catch (IOException e) {
System.err.println("Couldn't get I/O for the connection to " +
hostName);
System.exit(1);
}
}
}
上述代码在本地运行完全正常,但是将服务器上传到AWS并更改hostName会导致错误,我相信这就是问题所在。 服务器实例的IP地址(例如,52.214.166.199)未连接,并且在尝试声明Socket hwSocket时抛出IOException。由于未正确创建Socket,因此未返回任何输入,因此未收到Hello World消息。
非常感谢任何帮助,谢谢。
堆栈追踪:
Exception in thread "main" java.net.SocketException: Socket is not connected: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at java.net.Socket.connect(Socket.java:538)
at java.net.Socket.<init>(Socket.java:434)
at java.net.Socket.<init>(Socket.java:211)
at kkpclient.KKPClient.main(hwClient.java:19)
C:\Users\[me]\AppData\Local\NetBeans\Cache\8.1\executor-snippets\run.xml:53: Java returned: 1