Home > Back-end >  Abnormal Connection refused: connect what reason be
Abnormal Connection refused: connect what reason be

Time:11-30

According to the written tutorial video, but run the server class report this exception, the IP address that's right, don't know why, she
 package morepeople; 

Import the Java. IO. BufferedReader;
Import the Java. IO. IOException;
Import the Java. IO. InputStream;
Import the Java. IO. InputStreamReader;
Import the Java. IO. OutputStream;
Import the Java. IO. PrintStream;
The import java.net.ServerSocket;
The import java.net.Socket;

Public class testServer {

Public static void main (String [] args) throws IOException {
//TODO Auto - generated method stub
ServerSocket server=new ServerSocket (9999);

A Boolean flag=true;
While (flag) {
The Socket Socket=server. The accept ();
MessageHander mh=new MessageHander (socket);

Mh. Start ();

}
Server. The close ();
}

}

The class MessageHander extends Thread {

private Socket socket;

Public MessageHander (Socket Socket) {
super();
This. The socket=socket;
}

@ Override
Public void the run () {
Try {
InputStream=in the socket. GetInputStream ();
InputStreamReader isr=new InputStreamReader (in);
BufferedReader br=new BufferedReader (isr);

OutputStream out=socket. GetOutputStream ();
PrintStream ps=new PrintStream (out);

The String word;

While ((word=br. ReadLine ())!=null) {
If (" bye ". The equals (word)) {
break;
}
The StringBuilder sb=new StringBuilder (word);
Sb. Reverse ();

Ps. Println (sb. ToString ());
}

} the catch (IOException e) {
//TODO Auto - generated the catch block,
e.printStackTrace();
} the finally {
Try {
socket.close();
} the catch (IOException e) {
//TODO Auto - generated the catch block,
e.printStackTrace();
}
}
}

}


 package morepeople; 
Import the Java. IO. BufferedReader;
Import the Java. IO. IOException;
Import the Java. IO. InputStream;
Import the Java. IO. InputStreamReader;
Import the Java. IO. OutputStream;
Import the Java. IO. PrintStream;
The import java.net.Socket;
The import java.net.UnknownHostException;
import java.util.Scanner;

Public class testClient {

Public static void main (String [] args) throws UnknownHostException, IOException {
//TODO Auto - generated method stub
The Socket Socket=new Socket (" 192.168.1.119 ", 9999);

OutputStream out=socket. GetOutputStream ();
PrintStream ps=new PrintStream (out);

InputStream is=socket. GetInputStream ();
InputStreamReader isr=new InputStreamReader (is);
BufferedReader br=new BufferedReader (isr);

Scanner sc=new Scanner(System.in);

While (true) {
System. The out. Println (" please enter the words: ");
The String word=sc. Next ();

Ps. Println (word);

If (" bye ". The equals (word)) {
break;
}
The String result=br. ReadLine ();
System. The out. Println (" the results returned from the server: "+ result);
}

Is the close ();
socket.close();
}

}


Abnormal
The Exception in the thread "main" java.net.ConnectException: Connection refused: connect
The at java.net.DualStackPlainSocketImpl.connect0 (Native Method)
The at java.net.DualStackPlainSocketImpl.socketConnect (Unknown Source)
The at java.net.AbstractPlainSocketImpl.doConnect (Unknown Source)
The at java.net.AbstractPlainSocketImpl.connectToAddress (Unknown Source)
The at java.net.AbstractPlainSocketImpl.connect (Unknown Source)
The at java.net.PlainSocketImpl.connect (Unknown Source)
The at java.net.SocksSocketImpl.connect (Unknown Source)
The at java.net.Socket.connect (Unknown Source)
The at java.net.Socket.connect (Unknown Source)
The at java.net.Socket. (Unknown Source)
The at java.net.Socket. (Unknown Source)
The at morepeople. TestClient. Main (testClient. Java: 16)

CodePudding user response:

If the main method of testServer started on your computer, the socket in the testClient method initialized to modify the IP parameters, such as the following
 
The Socket Socket=new Socket (" localhost ", 9999);

CodePudding user response:

This exception in addition to the IP address is wrong, it is also possible that your server-side program does not run normally,

CodePudding user response:

This kind of situation, is basically a TCP connection is not on the database server, also not to verify the identity of the step, such as the server is not boot, database service has not started, the port is blocked by a firewall, etc

CodePudding user response:

Ping the IP impassability? Telnet IP port the impassability? First determine whether the network problems

CodePudding user response:

reference 4 floor Alpaca666 response:
ping IP impassability? Telnet IP port the impassability? To determine whether the network problem
yes

CodePudding user response:

reference 5 floor drowned shrimp reply:
Quote: refer to 4th floor Alpaca666 response:
ping IP impassability? Telnet IP port the impassability? To determine whether the network problem
to
1
  • Related