Android directly connected to the PC socket problem

Source: Internet
Author: User

Keyword: abdroid simulator socket

Suddenly some people say that using the Android emulator to do the socket server, the PC as a client, using UDP communication, the Android side can not receive packets. In turn no problem, I think this how possible, first if your code is not a problem, then only the real IP and port problems. So I searched, online is really a lot of people ask this question, I have to say that Java programmers are now SSH destroyed, many have come to the point of not thinking, encountered problems do not want to ask, originally you do? Since everyone is like this, it means that it can't be solved ... especially for Java, open Source Library, a lot of people have formed the habit of not thinking and trying, directly to find the existing implementation. This is completely without the fun of programming, Ah, well back to the point of this article. First of all, for tcp/udp communication, there must be at least some knowledge. First of all, regardless of the specific protocol and implementation of the TCP/UDP, network equipment and OSI protocols, at least understand the Java socket Framework, of course, the socket for all kinds of language is exactly the same, but the bottom implementation is different, To the Java even the implementation of the socket does not need to tube, just know how to use the class library can be. Socket communication requires at least two premises for each other's IP and port. This is also the basis of the requirements, so the problem is to exclude code problems can only appear here, of course, there is no problem with the physical structure of your network. Then the scope of this article is the scope of this article is the PC and Android simulator or using USB to the real Android socket communication. If Android is connected to the device, it is in the same position as the PC and has a separate IP, such as wifi. Nature is not covered in this article, when you look at the IP of both devices and then specify the port connection. An additional explanation of IP policy issues for Android devices is required here. When the Android device is connected to the PC, the default PC IP is 10.0.2.2, its own IP is 10.0.2.15/127.0.1. That means the Android device is connected to the PC, but in turn, the PC doesn't know the Android IP address, so you can't use a default value to make a connection request to the Android device IP10.0.2.2. When you think about it, it's basic to understand what to do next. TCP is the first TCP case where there is a connection state, so any connection can be established successfully to communicate. There are many scenarios for using Tcp/socket to connect Android devices, such as having an Android device with its own apk as the socket server, and then the PC-side program needs to establish a connection with it and then get the details of the Android device. This is usually usedThe ADB forward forwards the native TCP port to the TCP port on the Android device so that the PC can be used without knowing the IP of the Android device directly to the native, If a packet is sent on a port in 127.0.0.1, then the port forwarding mechanism forwards it to the Android device. This is not much to say, no meaning. Next is UDP, the reason for UDP is that UDP is very special. Fortunately here is a direct connection with the PC, but ADB forward cannot forward UDP port information, only TCP .... Well, that's why I found the basic command redir of the forwarding port. Redir Add < udp/tcp >:< pc port >:< emulator port, such as redir add udp:1096:1097 redir tcp:1096:1097, the role is to forward the PC's 1096 port to a The 1097 port of the ndroid device, of course two port numbers can be the same, because they are on two different devices. But there is a drawback, is not as flexible as the ADB forward. The operation process is as follows:

For example, the PC uses the Telnet command to connect to the Android device, Telnet requires the IP is the local, the port can be viewed using the ADB devices command, after the successful connection can use the redir command, there are list, add, del several parameters, List is to list the existence of the forwarding relationship, add added, Del is deleted, detailed reference documentation there is no need to say more. After setting the forwarding success there is a 1098 communication port from the PC, which looks from the Android ' device is a direct connect channel of 1097 communication ports. Next the code.

On the left is the Android code, implement a Udp/socket server, listen to the local 1097 port. The right side is the PC-side Java code that sends UDP packets directly to the native 1098 port. The sides are started separately, and Android is not subject to UDP packets until the forwarding relationship is established. Once the forwarding relationship 1098:1097 is established, the UDP server receives the PC-side message.

As shown, the right console sends content to the PC-side socket, and the left logcat is what the socket server in the Android device receives. Of course, in turn, you can send messages directly to the PC without having to forward them. Above. PS, I in the implementation process, with the Android simulator to the PC master broadcast data packets, the PC is not received, do not know if you have encountered this situation, seeking answers!!

Android directly connected to the PC socket problem

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.