国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android編程實現簡單的UDP Client實例

2019-12-12 06:39:29
字體:
來源:轉載
供稿:網友

本文實例講述了Android編程實現簡單的UDP Client。分享給大家供大家參考,具體如下:

該代碼在4.2.2內調試通過

1.記得加權限

<uses-permission android:name="android.permission.INTERNET"/>

注意:Android 4.0之后,就不能在主線程進行socket通信,否則會拋異常。

2.代碼

MainActivity.java:

package mao.example.quicksend;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.os.Handler;import android.util.Log;import android.view.Menu;import android.view.MenuItem;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;import android.view.View;public class MainActivity extends Activity {  @Override  protected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    InitUI();    //獲得Button對象    Button btnConnect = (Button) findViewById(R.id.buttonConnect);    btnConnect.setOnClickListener(new Button.OnClickListener() {      public void onClick(View v)      {        //這里處理事件        //ConnectServer();        //UDPClient udpET = new UDPClient("192.168.0.14","123");        //udpET.start();        //DisplayToast("點擊了/"連接/"按鈕");      }    });    //發送Button    btnConnect = (Button) findViewById(R.id.buttonSend);    btnConnect.setOnClickListener(new Button.OnClickListener() {      public void onClick(View v)      {        //這里處理事件        SendText();      }    });  }  @Override  public boolean onCreateOptionsMenu(Menu menu) {    // Inflate the menu; this adds items to the action bar if it is present.    getMenuInflater().inflate(R.menu.main, menu);    return true;  }  @Override  public boolean onOptionsItemSelected(MenuItem item) {    // Handle action bar item clicks here. The action bar will    // automatically handle clicks on the Home/Up button, so long    // as you specify a parent activity in AndroidManifest.xml.    int id = item.getItemId();    if (id == R.id.action_settings) {      return true;    }    return super.onOptionsItemSelected(item);  }  /* 顯示Toast */  public void DisplayToast(String str)  {    Toast.makeText(this, str, Toast.LENGTH_SHORT).show();  }  public void InitUI()  {    TextView text=(TextView)findViewById(R.id.editTextIP);    text.setText("192.168.0.14");    text = (TextView)findViewById(R.id.editTextFilePath);    text.setText("Udp Client Send Test");  }  // connect server  public void SendText()   {    TextView editIP=(TextView)findViewById(R.id.editTextIP);    TextView editText=(TextView)findViewById(R.id.editTextFilePath);    String message = editText.getText().toString() + "/r/n";     UDPClient udpET = new UDPClient(editIP.getText().toString(), message);    udpET.start();  }}

UDPClient.java:

package mao.example.quicksend;import java.io.IOException;import java.net.DatagramPacket;import java.net.DatagramSocket;import java.net.InetAddress;import java.net.SocketException;import java.lang.String;public class UDPClient extends Thread{  public String m_serverIP;  public String m_text;  public UDPClient(String strServerIP, String strText)  {    m_serverIP = strServerIP;    m_text = strText;  }  @Override  public void run() {    // TODO Auto-generated method stub    int TIMEOUT = 3000;    int servPort = 8800;    byte[] bytesToSend = m_text.getBytes();//"test_client".getBytes();    try {      InetAddress serverAddress = InetAddress.getByName(m_serverIP);      DatagramSocket socket = new DatagramSocket();      socket.setSoTimeout(TIMEOUT);      DatagramPacket sendPacket = new DatagramPacket(bytesToSend,bytesToSend.length,serverAddress,servPort);      socket.send(sendPacket);      socket.close();    } catch (SocketException e){      e.printStackTrace();    }catch(IOException e){      e.printStackTrace();    }  }}

3. activity_main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  xmlns:tools="http://schemas.android.com/tools"  android:id="@+id/LinearLayout1"  android:layout_width="match_parent"  android:layout_height="match_parent"  android:orientation="vertical"  android:paddingBottom="@dimen/activity_vertical_margin"  android:paddingLeft="@dimen/activity_horizontal_margin"  android:paddingRight="@dimen/activity_horizontal_margin"  android:paddingTop="@dimen/activity_vertical_margin"  tools:context="mao.example.quicksend.MainActivity" >  <EditText    android:id="@+id/editTextIP"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:hint="@string/text_IP" />  <Button    android:id="@+id/buttonConnect"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="@string/text_connect" />  <EditText    android:id="@+id/editTextFilePath"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:hint="@string/text_filePath" >    <requestFocus />  </EditText>  <Button    android:id="@+id/buttonFileSel"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="@string/text_sel" />  <Button    android:id="@+id/buttonSend"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="@string/text_send" /></LinearLayout>

更多關于Android相關內容感興趣的讀者可查看本站專題:《Android通信方式總結》、《Android調試技巧與常見問題解決方法匯總》、《Android開發入門與進階教程》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結》、《Android視圖View技巧總結》、《Android布局layout技巧總結》及《Android控件用法總結

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 本溪| 平利县| 奉节县| 高雄县| 博湖县| 广安市| 临澧县| 柘荣县| 福泉市| 河南省| 叙永县| 登封市| 湖北省| 漾濞| 揭东县| 禹城市| 三河市| 淅川县| 龙山县| 荣昌县| 昌乐县| 武强县| 德江县| 奉化市| 磴口县| 什邡市| 太白县| 凉城县| 大同县| 青冈县| 独山县| 武威市| 马公市| 田阳县| 南部县| 颍上县| 鄯善县| 廊坊市| 忻州市| 南平市| 凌云县|