欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

android开发socket编程之udp发送实例分析

程序员文章站 2022-03-23 13:59:09
本文实例讲述了android开发socket编程之udp发送实现方法。分享给大家供大家参考。具体分析如下: 需要实现的功能:采用udp下的socket编程,当按下确认键,...

本文实例讲述了android开发socket编程之udp发送实现方法。分享给大家供大家参考。具体分析如下:

需要实现的功能:采用udp下的socket编程,当按下确认键,模拟器发送文本框数据,pc机上的网络调试助手接收

一、环境:

win7 + eclipse + sdk

二、代码:

package test.soket; 
//import com.test_button.r; 
import java.io.dataoutputstream; 
import java.io.ioexception; 
import java.net.datagrampacket; 
import java.net.datagramsocket; 
import java.net.inetaddress; 
import java.net.socket; 
import java.net.socketexception; 
import java.net.unknownhostexception; 
import android.app.activity; 
import android.os.bundle; 
import android.view.view; 
import android.widget.button; 
import android.widget.textview; 
public class test_socket extends activity { 
 public static textview show; 
 public static button press; 
 public static boolean flag; 
 private static final int max_data_packet_length = 40; 
 private byte[] buffer = new byte[max_data_packet_length]; 
 private datagrampacket datapacket; 
 private datagramsocket udpsocket; 
 /** called when the activity is first created. */ 
 @override 
 public void oncreate(bundle savedinstancestate) { 
  super.oncreate(savedinstancestate); 
  setcontentview(r.layout.main); 
  //开辟控件空间 
  show = (textview)findviewbyid(r.id.edittext1); 
  press = (button)findviewbyid(r.id.button1); 
  flag = false; 
  //soket_send thread = new soket_send(); 
  //thread.init(); 
  //thread.start(); 
  try 
  { 
   udpsocket = new datagramsocket(5554); 
  } 
  catch (socketexception e) 
  { 
   // todo auto-generated catch block 
   e.printstacktrace(); 
  } 
  datapacket = new datagrampacket(buffer, max_data_packet_length); 
  string str = "hello,jdh"; //这是要传输的数据 
  byte out [] = str.getbytes(); //把传输内容分解成字节 
  datapacket.setdata(out); 
  datapacket.setlength(out.length); 
  datapacket.setport(5554); 
  try 
  { 
   inetaddress broadcastaddr = inetaddress.getbyname("192.168.0.248"); 
   datapacket.setaddress(broadcastaddr); 
   udpsocket.send(datapacket); 
  } 
  catch (ioexception e) 
  { 
   // todo auto-generated catch block 
   e.printstacktrace(); 
  } 
    
  press.setonclicklistener(new button.onclicklistener() 
  { 
   @override 
   public void onclick(view v) 
   { 
    flag = true; 
    /* 
    string str = "hello,jdh"; //这是要传输的数据 
    byte out [] = str.getbytes(); //把传输内容分解成字节 
    datapacket.setdata(out); 
    datapacket.setlength(out.length); 
    */ 
    //获得输入框文本 
    charsequence str =test_socket.show.gettext(); 
    byte out[] = str.tostring().getbytes(); 
    datapacket.setdata(out); 
    datapacket.setlength(out.length); 
    try 
    { 
     inetaddress broadcastaddr = inetaddress.getbyname("192.168.0.248"); 
     datapacket.setaddress(broadcastaddr); 
     udpsocket.send(datapacket); 
    } 
    catch (ioexception e) 
    { 
     // todo auto-generated catch block 
     e.printstacktrace(); 
    } 
   } 
  }); 
 } 
}

界面如下:

android开发socket编程之udp发送实例分析

注意:在模拟器ip为本机ip,端口为模拟器名称

希望本文所述对大家的android程序设计有所帮助。