标签:android io os ar java sp 数据 on art
============问题描述============
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============
标签:android io os ar java sp 数据 on art
原文地址:http://www.cnblogs.com/lengyanyue39/p/4033874.html