Sunday, February 9, 2014

android using socket failed to connect with the PC


            
Made a java server running on the machine, the machine ip address is 192.168.0.110
The server code is:
 static int clientNum = 1; 

public static void main (String [] args) {
/ / TODO code application logic here
ServerSocket server = null;

try {
server = new ServerSocket (8316);
} Catch (IOException e) {
e.printStackTrace ();
System.exit (-1);
}

while (true) {
try {
Socket ss = server.accept ();
new ServerThread (ss, clientNum) start ();.
} Catch (Exception e) {
e.printStackTrace ();
}
clientNum + +;
}

}

}


android client emulator running on the machine, the client code:
 public class MobileNewsActivity extends Activity {
/ ** Called when the activity is first created. * /
@ Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
Socket socket = null;
DataInputStream dis;
try {
socket = new Socket ("192.168.0.110", 8316);
dis = new DataInputStream (socket.getInputStream ());

} Catch (Exception e) {
e.printStackTrace ();
}

setContentView (R.layout.main);
}
}


Now the client and server can not establish a connection
Manifest which has been written




Help us to see where the problem
Reply:
No detailed error contents, they might just your windows firewall at play.

Please on windows server telnet localhost 8316 see if you can effectively establish Socket
And then on the windows server telnet xxx.xxx.xxx.xxx 8316 把 xxx.xxx.xxx.xxx replaced your windows server NIC IP, which has been effective recognition server socket listening on port 8316.
Then again on another machine and then telnet xxx.xxx.xxx.xxx 8316 to see if the server can effectively establish Socket
from another device or host
The above is no problem, then the error logcat glue to give you a look at it.
Reply:
socket = new Socket ("192.168.0.110", 8316);
IP behind this code in a space, in addition, when you visit the local IP in the simulator, it is recommended to change the IP 10.0.2.2 try.
Reply:
If the virtual machine is more than 4.0 drops, there may be a problem Oh
4.0 The above is not permitted in the interface directly SOCKET
Reply:
android3.0 version than the main thread can not be applied directly to Network Programming

No comments:

Post a Comment