I use my own computer to build a server, the first problem is that we need port mapping on the router, the outside network to access their own computers, how to port mapping can look at this article:
http://blog.csdn.net/zhoubin1992/article/details/45917775
One, D-Link port forwarding settings
Mine is D-Link.
Name write the name of your client.
IP address: The IP address assigned by the router to the native, typically starting with 192.168. Query method: cmd under ipconfig
Fill in the private port is the router needs to open the port, you can also set a range
The public port is the port to be accessed by the external network.
The traffic type is optional.
Second, the socket Communication program error
Execute to socket socket=new socket ("xx.xx.xxx.xxx", 8888); throw exception. Report timeout error.
The reason is that the client is not connected to the socket.
Workaround:
1, first determine another thread to complete the network connection.
New Thread () {public void run () {try{//connects the network and opens the stream s = new Socket ("110.83.75.12", 8888); Dout = new DataOutputStream (S.getoutputstream ()); DIN = new DataInputStream (S.getinputstream ());} catch (Exception e) {//catch exception e.printstacktrace ();//Print Exception}
2, this still does not, finally inStackOverflow found the answer,also needOpen Strictmode in OnCreate
Strictmode.setthreadpolicy (New StrictMode.ThreadPolicy.Builder () . Detectdiskreads () . Detectdiskwrites () . Detectnetwork (). penaltylog () . Build ()); Strictmode.setvmpolicy (New StrictMode.VmPolicy.Builder () . Detectleakedsqlliteobjects () . Penaltylog () . Penaltydeath () . Build ()); }
I also do not know why to add strict mode, some people know the message to tell me.
It's time to communicate, but when I use my own 3G traffic, there's a problem. The problem of socket native IP was found after lookup.
This machine also has an external network IP, obtain method: In Baidu fill in the native IP , the real IP of this machine is present.
Then the Socket ("xx.xx.xxx.xxx", 8888), the IP replacement is good ~
Android development: Native Tomcat build server, client socket network connection not problem and D-Link port forwarding settings