首页 > 代码库 > android udp编程
android udp编程
============问题描述============
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; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Thread thread = new Thread(runnable); thread.start(); } Runnable runnable = new Runnable() { @Override public void run() { // TODO Auto-generated method stub try { udpSocket = new DatagramSocket(5000); dataPacket = new DatagramPacket(buffer, MAX_DATA_PACKET_LENGTH); Log.v("packet", "pakcet built!"); String str = "hello,jdh"; //这是要传输的数据 byte out [] = new byte[]{(byte)127, (byte)17, (byte)10}; //把传输内容分解成字节 dataPacket.setData(out); dataPacket.setLength(out.length); Log.v("packet length", "pakcet length!"); try { dataPacket.setAddress(InetAddress.getByName("10.13.33.136")); dataPacket.setPort(5000); Log.v("packet port", "pakcet port!"); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (SocketException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { udpSocket.send(dataPacket); Log.v("packet sent", "packet sent!"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }; } 电脑写的客户端可以发送给服务器,但是手机端这个客户端却发送不成功,10.13.33.136是我本机ip,我手机连的是wifi。求解,是不是手机端写的哪里有问题?谢啦!
============解决方案1============
看下logcat打印的异常是什么?
android udp编程
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。