Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android開發socket編程之udp發送實例分析

android開發socket編程之udp發送實例分析

編輯:關於Android編程

本文實例講述了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(); 
    } 
   } 
  }); 
 } 
}

界面如下:

注意:在模擬器IP為本機IP,端口為模擬器名稱

希望本文所述對大家的Android程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved