Home > Mobile >  Java android socket communication failure
Java android socket communication failure

Time:09-24

Recently doing a about android and problem of the communication between servers, meet a problem, the following is part of the program code (server android mobile phones)
The computer end
Try {
The Socket Socket=new Socket (" 192.168.1.102 ", 2005);//local IP192.168.1.102 (LAN), connect to the server port 2005
If (socket. IsConnected ()==true)
{
OutputStream OutputStream=socket. GetOutputStream ();
DataOutputStream DataOutputStream=new DataOutputStream (outputStream);
InputStream InputStream=socket. GetInputStream ();
A DataInputStream a DataInputStream=new a DataInputStream (inputStream);
DataOutputStream. WriteUTF (" the Client is ON ");
While (true)
{
The switch (a dataInputStream readUTF ())
{
Case "password?" :
DataOutputStream. WriteUTF (password);
A case of "OK" :
System. The out. Println (" You are succeed in connecting ");
break;
Case "error" :
System. The out. Println (" You passsword may be wrong ");
break;
Default:
System. The out. Println (" both please try again ");
}
}
}
} the catch (IOException e) {
e.printStackTrace();
}
AnZhuoDuan (server)
@ Override
Public void the run () {
Super. The run ();
Try {
ServerSocket ServerSocket=new ServerSocket (2005);
The e (" tag ", "& amp; MY THREAD IS running ");
The Socket Socket=serverSocket. The accept ();
The e (" tag ", "MY THREAD IS running");
Change=true;
Localipaddress=socket. GetLocalAddress (). The toString ();
If (socket. IsConnected ()==true) {
InputStream InputStream=socket. GetInputStream ();
OutputStream OutputStream=socket. GetOutputStream ();
A DataInputStream a DataInputStream=new a DataInputStream (inputStream);
DataOutputStream DataOutputStream=new DataOutputStream (outputStream);
If (socket. IsConnected ()==true) {
If (a dataInputStream. ReadUTF (). The toString ()=="Client is ON") {
DataOutputStream. WriteUTF (" password?" );
If (a dataInputStream. ReadUTF (). The toString ()==passwordText) {
DataOutputStream. WriteUTF (" OK ");
The setContentView (R.l ayout. Error);
} else {
DataOutputStream. WriteUTF (" error ");
The setContentView (R.l ayout. Ok);
}
}
}
}
} the catch (IOException e) {
e.printStackTrace();
}
The following is a computer the error
'java.net.ConnectException: Connection refused (Connection refused)
The at java.base/java.net.PlainSocketImpl.socketConnect (Native Method)
At java.base/java.net.AbstractPlainSocketImpl.doConnect AbstractPlainSocketImpl. Java: (399)
At java.base/java.net.AbstractPlainSocketImpl.connectToAddress AbstractPlainSocketImpl. Java: (242)
At java.base/java.net.AbstractPlainSocketImpl.connect AbstractPlainSocketImpl. Java: (224)
At java.base/java.net.SocksSocketImpl.connect SocksSocketImpl. Java: (403)
At java.base/java.net.Socket.connect (591) Socket. Java:
At java.base/java.net.Socket.connect (540) Socket. Java:
The at java.base/java.net.Socket. (Socket. Java: 436)
The at java.base/java.net.Socket. (Socket. Java: 213)
The at ArithmeticAssistant. AndroidPhone. & lt; init> (AndroidPhone. Java: 8)
Bosses come quickly
  • Related