Tuesday, February 25, 2014

The UDP communication between PC and Android


Brother recently a project (beginner android not know). The main content is this: is sent by the PC to Android UDP (not TCP) information. Code is as follows:
PC side:
UdpClient uc = new UdpClient ();
IPEndPoint Point = new IPEndPoint (IPAddress.Broadcast, 20130);
while (true)
{
int i = new Random () Next (1, 5);.
byte [] sendbuff = new byte [8];
sendbuff = Encoding.Default.GetBytes (i.ToString ());

uc.Send (sendbuff, sendbuff.Length, Point);
Thread.Sleep (5000);
}
Android:
DatagramSocket socket = new DatagramSocket (20130);
byte data [] = new byte [8];
DatagramPacket packet = new DatagramPacket (data, data.length);
socket.receive (packet);
String result = new String (packet.getData (), packet.getOffset (), packet.getLength ());
Above code: If the PC side of the IPEndPoint Point = new IPEndPoint (IPAddress.Broadcast, 20130); specify the receiving end IP words such as IPEndPoint Point = new IPEndPoint (IPAddress.parse ("192.16.1.8"), 20130). In the android will be able to receive the information, but if the above code does not specify the ip address but can not broadcast the words android received not know why. Members seeking to solve the great God.
Another note: android did not use the simulator, is an independent mobile phone and pc connected to the same network segment.<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Their top, Online
Reply:
Beg proficient in C #, the god Android solving ah, do you
wood was
Reply:
Read this article, I hope to help you, which was previously write their own
http://blog.csdn.net/yudajun/article/details/8477149
Reply:
Upstairs Hello, you have seen examples of, and I write is the same, but is not, I do not know why
Reply:
I do not know is not a permissions problem, or firewall Grenoble

No comments:

Post a Comment