Java 当我的客户端程序托管在DigitalOcean服务器上时,它不会连接到服务器程序。这可能吗?
我有一个客户端和服务器程序。当我在localhost上同时运行此程序时,它可以工作,但现在我已将服务器程序移动到digital Ocean上托管的服务器上。我希望在笔记本电脑上执行客户端程序时,客户端和服务器能够通信 这是客户端代码Java 当我的客户端程序托管在DigitalOcean服务器上时,它不会连接到服务器程序。这可能吗?,java,networking,client,server,Java,Networking,Client,Server,我有一个客户端和服务器程序。当我在localhost上同时运行此程序时,它可以工作,但现在我已将服务器程序移动到digital Ocean上托管的服务器上。我希望在笔记本电脑上执行客户端程序时,客户端和服务器能够通信 这是客户端代码 public class ClientExample{ Socket requestSocket; ObjectOutputStream out; ObjectInputStream in; String message; C
public class ClientExample{
Socket requestSocket;
ObjectOutputStream out;
ObjectInputStream in;
String message;
ClientExample(){}
void run()
{
try{
//1. creating a socket to connect to the server
requestSocket = new Socket("182.15.6.1", 2222); //ip is example
System.out.println("Connected to localhost in port 2004");
//2. get Input and Output streams
out = new ObjectOutputStream(requestSocket.getOutputStream());
out.flush();
in = new ObjectInputStream(requestSocket.getInputStream());
//3: Communicating with the server
do{
try{
message = (String)in.readObject();
System.out.println("server>" + message);
sendMessage("hello!");
sendMessage("How are you");
message = "bye";
sendMessage(message);
}
catch(ClassNotFoundException classNot){
System.err.println("data received in unknown format");
}
}while(!message.equals("bye"));
}
catch(UnknownHostException unknownHost){
System.err.println("You are trying to connect to an unknown host!");
}
catch(IOException ioException){
ioException.printStackTrace();
}
finally{
//4: Closing connection
try{
in.close();
out.close();
requestSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}
void sendMessage(String msg)
{
try{
out.writeObject(msg);
out.flush();
System.out.println("client>" + msg);
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
public static void main(String args[])
{
ClientExample client = new ClientExample();
client.run();
}
}
这是我的服务器代码
public class ExampleServer{
ServerSocket providerSocket;
Socket connection = null;
ObjectOutputStream out;
ObjectInputStream in;
String message;
ExampleServer(){}
void run()
{
try{
//1. creating a server socket
providerSocket = new ServerSocket(2222, 10);
//2. Wait for connection
System.out.println("Waiting for connection");
connection = providerSocket.accept();
System.out.println("Connection received from " + connection.getInetAddress().getHostName());
//3. get Input and Output streams
out = new ObjectOutputStream(connection.getOutputStream());
out.flush();
in = new ObjectInputStream(connection.getInputStream());
sendMessage("Connection successful");
//4. The two parts communicate via the input and output streams
do{
try{
message = (String)in.readObject();
System.out.println("client>" + message);
if (message.equals("bye"))
sendMessage("bye");
}
catch(ClassNotFoundException classnot){
System.err.println("Data received in unknown format");
}
}while(!message.equals("bye"));
}
catch(IOException ioException){
ioException.printStackTrace();
}
finally{
//4: Closing connection
try{
in.close();
out.close();
providerSocket.close();
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
}
void sendMessage(String msg)
{
try{
out.writeObject(msg);
out.flush();
System.out.println("server>" + msg);
}
catch(IOException ioException){
ioException.printStackTrace();
}
}
public static void main(String args[])
{
ExampleServer server = new ExampleServer();
while(true){
server.run();
}
}
}
我得到的错误是:
java.net.ConnectException: Connection timed out: connect
at java.net.DualStackPlainSocketImpl.connect0(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
at java.net.PlainSocketImpl.connect(Unknown Source)
at java.net.SocksSocketImpl.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.connect(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at java.net.Socket.<init>(Unknown Source)
at ClientExample.run(ClientExample.java:13)
at ClientExample.main(ClientExample.java:66)
Exception in thread "main" java.lang.NullPointerException
at ClientExample.run(ClientExample.java:43)
at ClientExample.main(ClientExample.java:66)
检查服务器上的防火墙规则,基本上检查端口2222上的入站连接是否允许。还引发了NullPointerException。ClientExample的第43行有什么?可能是端口。如果您在linux上运行,请尝试nmap localhost-p 2222command@NishantShreshth第43行的代码为in.close@nafas我这样做了,我得到了这个回复端口状态服务2222/tcp封闭以太网/IP-1我猜状态应该是开放的?你能告诉我怎么做吗?
Pinging MyServer with 32 bytes of data:
Reply from MyServer: bytes=32 time=151ms TTL=50
Reply from MyServer: bytes=32 time=116ms TTL=50
Reply from MyServer: bytes=32 time=101ms TTL=50
Reply from MyServer: bytes=32 time=98ms TTL=50
Ping statistics for MyServer:
Packets: Sent = 4, Received = 4, Lost = 0 (0% los
Approximate round trip times in milli-seconds:
Minimum = 98ms, Maximum = 151ms, Average = 116ms