码迷,mamicode.com
首页 > 移动开发 > 详细

一个简单的Android UDP Client

时间:2015-01-28 19:23:49      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:

(该代码在4.2.2内调试通过)

1.记得加权限

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

 注意:Android 4.0之后,就不能在主线程进行socket通信,否则会抛异常。

2.代码

MainActivity.java

技术分享
 1 package mao.example.quicksend;
 2 
 3 
 4 import android.app.Activity;
 5 import android.content.Context;
 6 import android.os.Bundle;
 7 import android.os.Handler;
 8 import android.util.Log;
 9 import android.view.Menu;
10 import android.view.MenuItem;
11 import android.widget.Button;
12 import android.widget.TextView;
13 import android.widget.Toast;
14 import android.view.View;
15 
16 public class MainActivity extends Activity {
17     
18     @Override
19     protected void onCreate(Bundle savedInstanceState) {
20         super.onCreate(savedInstanceState);
21         setContentView(R.layout.activity_main);
22         
23         InitUI();
24         
25         //获得Button对象
26         Button btnConnect = (Button) findViewById(R.id.buttonConnect);
27         btnConnect.setOnClickListener(new Button.OnClickListener() {
28             public void onClick(View v)
29             {
30                 //这里处理事件
31                 
32                 //ConnectServer();
33                 //UDPClient udpET = new UDPClient("192.168.0.14","123");
34                 //udpET.start();
35                 //DisplayToast("点击了\"连接\"按钮");
36             }
37         });
38         
39         //发送Button
40         btnConnect = (Button) findViewById(R.id.buttonSend);
41         btnConnect.setOnClickListener(new Button.OnClickListener() {
42             public void onClick(View v)
43             {
44                 //这里处理事件
45                 SendText();
46             }
47         });
48     }
49 
50     @Override
51     public boolean onCreateOptionsMenu(Menu menu) {
52         // Inflate the menu; this adds items to the action bar if it is present.
53         getMenuInflater().inflate(R.menu.main, menu);
54         return true;
55     }
56 
57     @Override
58     public boolean onOptionsItemSelected(MenuItem item) {
59         // Handle action bar item clicks here. The action bar will
60         // automatically handle clicks on the Home/Up button, so long
61         // as you specify a parent activity in AndroidManifest.xml.
62         int id = item.getItemId();
63         if (id == R.id.action_settings) {
64             return true;
65         }
66         return super.onOptionsItemSelected(item);
67     }
68     
69     /* 显示Toast  */
70     public void DisplayToast(String str)
71     {
72         Toast.makeText(this, str, Toast.LENGTH_SHORT).show();
73     }
74     
75     public void InitUI()
76     {
77         TextView text=(TextView)findViewById(R.id.editTextIP);
78         text.setText("192.168.0.14");
79         
80         text = (TextView)findViewById(R.id.editTextFilePath);
81         text.setText("Udp Client Send Test");
82     }
83     
84     // connect server
85     public void SendText() 
86     {
87         TextView editIP=(TextView)findViewById(R.id.editTextIP);
88         TextView editText=(TextView)findViewById(R.id.editTextFilePath);
89         String message = editText.getText().toString() + "\r\n"; 
90         
91         UDPClient udpET = new UDPClient(editIP.getText().toString(), message);
92         udpET.start();
93     }
94 }
View Code

UDPClient.java

技术分享
 1 package mao.example.quicksend;
 2 
 3 import java.io.IOException;
 4 import java.net.DatagramPacket;
 5 import java.net.DatagramSocket;
 6 import java.net.InetAddress;
 7 import java.net.SocketException;
 8 import java.lang.String;
 9 
10 public class UDPClient extends Thread{
11 
12     public String m_serverIP;
13     public String m_text;
14     
15     public  UDPClient(String strServerIP, String strText)
16     {
17         m_serverIP = strServerIP;
18         m_text = strText;
19     }
20     
21     @Override
22     public void run() {
23         // TODO Auto-generated method stub
24         int TIMEOUT = 3000;
25         int servPort = 8800;
26         byte[] bytesToSend = m_text.getBytes();//"test_client".getBytes();
27 
28         try {
29             InetAddress serverAddress = InetAddress.getByName(m_serverIP);
30             DatagramSocket socket = new DatagramSocket();
31             socket.setSoTimeout(TIMEOUT);
32             
33             DatagramPacket sendPacket = new DatagramPacket(bytesToSend,bytesToSend.length,serverAddress,servPort);
34             socket.send(sendPacket);
35                 
36             socket.close();
37         } catch (SocketException e){
38             e.printStackTrace();
39         }catch(IOException e){
40             e.printStackTrace();
41         }
42     }
43 }
View Code

3. activity_main.xml

技术分享
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:id="@+id/LinearLayout1"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical"
 7     android:paddingBottom="@dimen/activity_vertical_margin"
 8     android:paddingLeft="@dimen/activity_horizontal_margin"
 9     android:paddingRight="@dimen/activity_horizontal_margin"
10     android:paddingTop="@dimen/activity_vertical_margin"
11     tools:context="mao.example.quicksend.MainActivity" >
12 
13     <EditText
14         android:id="@+id/editTextIP"
15         android:layout_width="match_parent"
16         android:layout_height="wrap_content"
17         android:hint="@string/text_IP" />
18 
19     <Button
20         android:id="@+id/buttonConnect"
21         android:layout_width="match_parent"
22         android:layout_height="wrap_content"
23         android:text="@string/text_connect" />
24 
25     <EditText
26         android:id="@+id/editTextFilePath"
27         android:layout_width="match_parent"
28         android:layout_height="wrap_content"
29         android:hint="@string/text_filePath" >
30 
31         <requestFocus />
32     </EditText>
33 
34     <Button
35         android:id="@+id/buttonFileSel"
36         android:layout_width="match_parent"
37         android:layout_height="wrap_content"
38         android:text="@string/text_sel" />
39 
40     <Button
41         android:id="@+id/buttonSend"
42         android:layout_width="match_parent"
43         android:layout_height="wrap_content"
44         android:text="@string/text_send" />
45 
46 </LinearLayout>
View Code

 

一个简单的Android UDP Client

标签:

原文地址:http://www.cnblogs.com/bluebbc/p/4256604.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!