Android development: Native Tomcat build server, client socket network connection not problem and D-Link port forwarding settings

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.