Android UDP programming

Source: Internet
Author: User

============ Problem Description ============


package com.example.udpclient;import android.app.activity;import android.os.bundle;import  android.util.log;import java.io.ioexception;import java.net.datagrampacket;import  java.net.datagramsocket;import java.net.inetaddress;import java.net.socketexception;import  java.net.unknownhostexception;public class mainactivity extends activity{private  static final int max_data_packet_length = 100;       private byte[] buffer = new byte[max_data_packet_length];       private DatagramPacket dataPacket = null;       private datagramsocket udpsocket = null;      @ Overrideprotected void oncreate (bundle savedinstancestate) {super.oncreate (savedInstanceState); Setcontentview (R.layout.activity_main); Thread thRead = new thread (runnable); Thread.Start ();} Runnable runnable = new runnable () {@Overridepublic  void run () {// todo  Auto-generated method stubtry{udpsocket = new datagramsocket (;d) atapacket =  new datagrampacket (buffer, max_data_packet_length);   LOG.V ("PACKET",  "Pakcet  built! ");         String str =  "HELLO,JDH";   //this is the data to be transferred           byte out [] = new byte[]{ (byte) 127,  (Byte) 17,  (byte) 10};  //breaks the contents of the transfer into bytes            datapacket.setdata (out);         datapacket.setlength (out.length);         LOG.V ("Packet length",  "pakcet  Length! ");        &nbsP;try{datapacket.setaddress (Inetaddress.getbyname ("10.13.33.136"));d Atapacket.setport (5000); LOG.V ("Packet port",  "pakcet port!");}  catch  (unknownhostexception e) {// todo auto-generated catch  Blocke.printstacktrace ();}}  catch  (socketexception e) {// todo auto-generated catch  Blocke.printstacktrace ();} Try{udpsocket.send (Datapacket); LOG.V ("Packet sent",  "packet sent!");}  catch  (ioexception e) {// todo auto-generated catch blocke.printstacktrace ( );}}};} The computer writes the client can send to the server, but the cell phone side this client but sends the unsuccessful, 10.13.33.136 is my native IP, my mobile phone even is the WiFi. Solve, is not the phone side write where there is a problem? Thank you!

============ Solution 1============


Look at the Logcat print exception?

Android UDP programming

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.