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打印的异常是什么?
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。