Android udp 广播发送和接收 - the_Gaven - 博客园

baacloud免费翻墙vpn注册使用

来源: Android udp 广播发送和接收 – the_Gaven – 博客园

Android udp 广播发送和接收

最近在和同学开发一款app,作为课程大作业。其中,涉及到udp socket (多播) 的发送和接收、tcp socket 的发送和接收。作为一个Java的门外汉,在简单地看了一些理论地资料之后,实际编程中遇到了不少问题。然后,又在网上大搜这方面的博客,找来找去,其实 大家写的东西基本都一样,因为规则已经订好了。网上的代码不全,又有一些错漏,让我走了很多弯路,无数次推倒代码重写,Debug,终于调出了一个实际可 运行的版本。 希望初学的童鞋看到我的这篇博客能少走一些弯路.

 

-----------------------

转载请注明出处:

http://www.cnblogs.com/zhangzph/p/4475962.html

-----------------------

 

在给出代码之前,先简单介绍一下我的代码在做什么。

 

代码逻辑:

本机发送udp广播

本机开启线程,监听来自别的机器的udp广播,显示信息。 然后,对udp来源发送tcp连接

接收来自别的机器的tcp连接,并显示信息

(这里的udp广播,我使用udp多播代替了,多播具有广播的所有优点,而且有更少的缺点,实现上也比较简单,这里就不再过多地介绍了)

 

 

具体ui操作:

 

start 按钮用来启动udp 多播,stop按钮停止发送 (实际上,由于start 按钮按下之后只发送一次udp多播,stop按钮只是用于setEnabled操作)下面有两个TextView,内容为send的TextView 显示--本机发送 tcp socket 的信息; 内容为receive的TextView 显示--本机接收来自别的机器的udp socket 和 tcp socket 的信息.

 

 

几个需要注意的地方:

 

1. Android Manifest 权限设置、sdk版本信息:

本文所涉及到的这些功能需要获取 Android 的一些权限,下面是我的权限和版本信息

 

上面 条目 uses-sdk中的信息,需要在build.gradle文件中同步。

 

2. 注意udp广播,和udp广播监听需要绑定同一个端口

3. 其他的有关IDE抽风的问题,比如我的Android Studio,有时候你修改了代码,重新把程序烧进手机的时候,它竟然会用缓存中代码的老版本来烧程序。。。 还有,有时候project加载太慢,程序崩溃之后,logcat好长时间都不出错误信息,严重影响Debug

4. 建议使用android sdk版本比较新的手机进行测试。 我测试的时候,用一部4.4和5.1的成功了。混合另外一部4.0.x的则有时候不太灵通。

 

github上的项目链接:

https://github.com/zhangpzh/Anjay

 

 

主要代码:

 

xml 源码:

<div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a title="复制代码"><img src="http://www.mikel.cn/wp-content/uploads/2015/05/copycode7.gif" alt="复制代码" /></a></span></div>
<pre>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MyActivity"&gt;

    &lt;LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_horizontal"
        &gt;
        &lt;Button
            android:id="@+id/start"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="start"
            /&gt;
        &lt;Button
            android:id="@+id/stop"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="150dp"
            android:text="stop"
            /&gt;
    &lt;/LinearLayout&gt;

    &lt;LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center_horizontal"
        &gt;
        &lt;TextView
            android:id="@+id/send_information"
            android:layout_marginTop="50dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="send"
            android:layout_marginRight="110dp"
            /&gt;
        &lt;TextView
            android:id="@+id/receive_information"
            android:layout_marginTop="50dp"
            android:text="receive"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            /&gt;
    &lt;/LinearLayout&gt;
&lt;/LinearLayout&gt;

Java 源码:

 

(github 上面的代码已经把各个通信内部类给模块化了,不再像下面这样,全都定义在一个Activity里。但是为了集中展示app的功能,下面仍使用一个文件显示)

<div class="container">
<div class="line number1 index0 alt2"><code class="java keyword">package</code> <code class="java plain">com.example.user.anjay;</code></div>
<div class="line number2 index1 alt1"></div>
<div class="line number3 index2 alt2"><code class="java keyword">import</code> <code class="java plain">android.app.Activity;</code></div>
<div class="line number4 index3 alt1"><code class="java keyword">import</code> <code class="java plain">android.os.Bundle;</code></div>
<div class="line number5 index4 alt2"><code class="java keyword">import</code> <code class="java plain">android.util.Log;</code></div>
<div class="line number6 index5 alt1"><code class="java keyword">import</code> <code class="java plain">android.view.Menu;</code></div>
<div class="line number7 index6 alt2"><code class="java keyword">import</code> <code class="java plain">android.view.MenuItem;</code></div>
<div class="line number8 index7 alt1"><code class="java keyword">import</code> <code class="java plain">android.view.View;</code></div>
<div class="line number9 index8 alt2"><code class="java keyword">import</code> <code class="java plain">android.widget.Button;</code></div>
<div class="line number10 index9 alt1"><code class="java keyword">import</code> <code class="java plain">android.widget.TextView;</code></div>
<div class="line number11 index10 alt2"></div>
<div class="line number12 index11 alt1"><code class="java keyword">import</code> <code class="java plain">com.example.user.anjay.R;</code></div>
<div class="line number13 index12 alt2"></div>
<div class="line number14 index13 alt1"><code class="java keyword">import</code> <code class="java plain">org.apache.http.conn.util.InetAddressUtils;</code></div>
<div class="line number15 index14 alt2"></div>
<div class="line number16 index15 alt1"><code class="java keyword">import</code> <code class="java plain">java.io.BufferedReader;</code></div>
<div class="line number17 index16 alt2"><code class="java keyword">import</code> <code class="java plain">java.io.IOException;</code></div>
<div class="line number18 index17 alt1"><code class="java keyword">import</code> <code class="java plain">java.io.InputStreamReader;</code></div>
<div class="line number19 index18 alt2"><code class="java keyword">import</code> <code class="java plain">java.net.DatagramPacket;</code></div>
<div class="line number20 index19 alt1"><code class="java keyword">import</code> <code class="java plain">java.net.InetAddress;</code></div>
<div class="line number21 index20 alt2"><code class="java keyword">import</code> <code class="java plain">java.net.MulticastSocket;</code></div>
<div class="line number22 index21 alt1"><code class="java keyword">import</code> <code class="java plain">java.net.NetworkInterface;</code></div>
<div class="line number23 index22 alt2"><code class="java keyword">import</code> <code class="java plain">java.net.ServerSocket;</code></div>
<div class="line number24 index23 alt1"><code class="java keyword">import</code> <code class="java plain">java.net.Socket;</code></div>
<div class="line number25 index24 alt2"><code class="java keyword">import</code> <code class="java plain">java.net.SocketException;</code></div>
<div class="line number26 index25 alt1"><code class="java keyword">import</code> <code class="java plain">java.util.Enumeration;</code></div>
<div class="line number27 index26 alt2"></div>
<div class="line number28 index27 alt1"></div>
<div class="line number29 index28 alt2"><code class="java keyword">public</code> <code class="java keyword">class</code> <code class="java plain">MyActivity </code><code class="java keyword">extends</code> <code class="java plain">Activity {</code></div>
<div class="line number30 index29 alt1"></div>
<div class="line number31 index30 alt2"><code class="java spaces">    </code><code class="java keyword">private</code> <code class="java keyword">static</code> <code class="java plain">String LOG_TAG = </code><code class="java string">"WifiMulticastActivity"</code><code class="java plain">;</code></div>
<div class="line number32 index31 alt1"></div>
<div class="line number33 index32 alt2"><code class="java spaces">    </code><code class="java plain">Button startBroadCast;</code></div>
<div class="line number34 index33 alt1"><code class="java spaces">    </code><code class="java plain">Button stopBroadCast;</code></div>
<div class="line number35 index34 alt2"></div>
<div class="line number36 index35 alt1"><code class="java spaces">    </code><code class="java plain">TextView send_label;</code></div>
<div class="line number37 index36 alt2"><code class="java spaces">    </code><code class="java plain">TextView receive_label;</code></div>
<div class="line number38 index37 alt1"></div>
<div class="line number39 index38 alt2"><code class="java spaces">    </code><code class="java comments">/* 用于 udpReceiveAndTcpSend 的3个变量 */</code></div>
<div class="line number40 index39 alt1"><code class="java spaces">    </code><code class="java plain">Socket socket = </code><code class="java keyword">null</code><code class="java plain">;</code></div>
<div class="line number41 index40 alt2"><code class="java spaces">    </code><code class="java plain">MulticastSocket ms = </code><code class="java keyword">null</code><code class="java plain">;</code></div>
<div class="line number42 index41 alt1"><code class="java spaces">    </code><code class="java plain">DatagramPacket dp;</code></div>
<div class="line number43 index42 alt2"></div>
<div class="line number44 index43 alt1"><code class="java spaces">    </code><code class="java color1">@Override</code></div>
<div class="line number45 index44 alt2"><code class="java spaces">    </code><code class="java keyword">protected</code> <code class="java keyword">void</code> <code class="java plain">onCreate(Bundle savedInstanceState) {</code></div>
<div class="line number46 index45 alt1"><code class="java spaces">        </code><code class="java keyword">super</code><code class="java plain">.onCreate(savedInstanceState);</code></div>
<div class="line number47 index46 alt2"><code class="java spaces">        </code><code class="java plain">setContentView(R.layout.activity_my);</code></div>
<div class="line number48 index47 alt1"></div>
<div class="line number49 index48 alt2"><code class="java spaces">        </code><code class="java plain">startBroadCast = (Button) findViewById(R.id.start);</code></div>
<div class="line number50 index49 alt1"><code class="java spaces">        </code><code class="java plain">stopBroadCast = (Button) findViewById(R.id.stop);</code></div>
<div class="line number51 index50 alt2"></div>
<div class="line number52 index51 alt1"><code class="java spaces">        </code><code class="java plain">send_label = (TextView) findViewById(R.id.send_information);</code></div>
<div class="line number53 index52 alt2"><code class="java spaces">        </code><code class="java plain">receive_label = (TextView) findViewById(R.id.receive_information);</code></div>
<div class="line number54 index53 alt1"></div>
<div class="line number55 index54 alt2"><code class="java spaces">        </code><code class="java plain">send_label.append(</code><code class="java string">"\n\n"</code><code class="java plain">);</code></div>
<div class="line number56 index55 alt1"><code class="java spaces">        </code><code class="java plain">receive_label.append(</code><code class="java string">"\n\n"</code><code class="java plain">);</code></div>
<div class="line number57 index56 alt2"></div>
<div class="line number58 index57 alt1"><code class="java spaces">        </code><code class="java plain">startBroadCast.setOnClickListener(listener);</code></div>
<div class="line number59 index58 alt2"><code class="java spaces">        </code><code class="java plain">stopBroadCast.setOnClickListener(listener);</code></div>
<div class="line number60 index59 alt1"></div>
<div class="line number61 index60 alt2"><code class="java spaces">        </code><code class="java comments">/* 开一个线程接收tcp 连接*/</code></div>
<div class="line number62 index61 alt1"><code class="java spaces">        </code><code class="java keyword">new</code> <code class="java plain">tcpReceive().start();</code></div>
<div class="line number63 index62 alt2"></div>
<div class="line number64 index63 alt1"><code class="java spaces">        </code><code class="java comments">/* 开一个线程 接收udp多播 并 发送tcp 连接*/</code></div>
<div class="line number65 index64 alt2"><code class="java spaces">        </code><code class="java keyword">new</code> <code class="java plain">udpReceiveAndtcpSend().start();</code></div>
<div class="line number66 index65 alt1"><code class="java spaces">    </code><code class="java plain">}</code></div>
<div class="line number67 index66 alt2"></div>
<div class="line number68 index67 alt1"><code class="java spaces">    </code><code class="java keyword">private</code> <code class="java plain">View.OnClickListener listener = </code><code class="java keyword">new</code> <code class="java plain">View.OnClickListener() {</code></div>
<div class="line number69 index68 alt2"><code class="java spaces">        </code><code class="java color1">@Override</code></div>
<div class="line number70 index69 alt1"><code class="java spaces">        </code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">onClick(View v) {</code></div>
<div class="line number71 index70 alt2"><code class="java spaces">            </code><code class="java keyword">if</code> <code class="java plain">(v == startBroadCast ) {</code></div>
<div class="line number72 index71 alt1"><code class="java spaces">                </code><code class="java plain">startBroadCast.setEnabled(</code><code class="java keyword">false</code><code class="java plain">);</code></div>
<div class="line number73 index72 alt2"><code class="java spaces">                </code><code class="java plain">stopBroadCast.setEnabled(</code><code class="java keyword">true</code><code class="java plain">);</code></div>
<div class="line number74 index73 alt1"></div>
<div class="line number75 index74 alt2"><code class="java spaces">                </code><code class="java comments">/* 新开一个线程 发送 udp 多播 */</code></div>
<div class="line number76 index75 alt1"><code class="java spaces">                </code><code class="java keyword">new</code> <code class="java plain">udpBroadCast(</code><code class="java string">"hi ~!"</code><code class="java plain">).start();</code></div>
<div class="line number77 index76 alt2"><code class="java spaces">            </code><code class="java plain">}</code></div>
<div class="line number78 index77 alt1"><code class="java spaces">            </code><code class="java keyword">else</code> <code class="java plain">{</code></div>
<div class="line number79 index78 alt2"><code class="java spaces">                </code><code class="java plain">startBroadCast.setEnabled(</code><code class="java keyword">true</code><code class="java plain">);</code></div>
<div class="line number80 index79 alt1"><code class="java spaces">                </code><code class="java plain">stopBroadCast.setEnabled(</code><code class="java keyword">false</code><code class="java plain">);</code></div>
<div class="line number81 index80 alt2"><code class="java spaces">            </code><code class="java plain">}</code></div>
<div class="line number82 index81 alt1"><code class="java spaces">        </code><code class="java plain">}</code></div>
<div class="line number83 index82 alt2"><code class="java spaces">    </code><code class="java plain">};</code></div>
<div class="line number84 index83 alt1"></div>
<div class="line number85 index84 alt2"><code class="java spaces">    </code><code class="java comments">/* 发送udp多播 */</code></div>
<div class="line number86 index85 alt1"><code class="java spaces">    </code><code class="java keyword">private</code>  <code class="java keyword">class</code> <code class="java plain">udpBroadCast </code><code class="java keyword">extends</code> <code class="java plain">Thread {</code></div>
<div class="line number87 index86 alt2"><code class="java spaces">        </code><code class="java plain">MulticastSocket sender = </code><code class="java keyword">null</code><code class="java plain">;</code></div>
<div class="line number88 index87 alt1"><code class="java spaces">        </code><code class="java plain">DatagramPacket dj = </code><code class="java keyword">null</code><code class="java plain">;</code></div>
<div class="line number89 index88 alt2"><code class="java spaces">        </code><code class="java plain">InetAddress group = </code><code class="java keyword">null</code><code class="java plain">;</code></div>
<div class="line number90 index89 alt1"></div>
<div class="line number91 index90 alt2"><code class="java spaces">        </code><code class="java keyword">byte</code><code class="java plain">[] data = </code><code class="java keyword">new</code> <code class="java keyword">byte</code><code class="java plain">[</code><code class="java value">1024</code><code class="java plain">];</code></div>
<div class="line number92 index91 alt1"></div>
<div class="line number93 index92 alt2"><code class="java spaces">        </code><code class="java keyword">public</code> <code class="java plain">udpBroadCast(String dataString) {</code></div>
<div class="line number94 index93 alt1"><code class="java spaces">            </code><code class="java plain">data = dataString.getBytes();</code></div>
<div class="line number95 index94 alt2"><code class="java spaces">        </code><code class="java plain">}</code></div>
<div class="line number96 index95 alt1"></div>
<div class="line number97 index96 alt2"><code class="java spaces">        </code><code class="java color1">@Override</code></div>
<div class="line number98 index97 alt1"><code class="java spaces">        </code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">run() {</code></div>
<div class="line number99 index98 alt2"><code class="java spaces">            </code><code class="java keyword">try</code> <code class="java plain">{</code></div>
<div class="line number100 index99 alt1"><code class="java spaces">                </code><code class="java plain">sender = </code><code class="java keyword">new</code> <code class="java plain">MulticastSocket();</code></div>
<div class="line number101 index100 alt2"><code class="java spaces">                </code><code class="java plain">group = InetAddress.getByName(</code><code class="java string">"224.0.0.1"</code><code class="java plain">);</code></div>
<div class="line number102 index101 alt1"><code class="java spaces">                </code><code class="java plain">dj = </code><code class="java keyword">new</code> <code class="java plain">DatagramPacket(data,data.length,group,</code><code class="java value">6789</code><code class="java plain">);</code></div>
<div class="line number103 index102 alt2"><code class="java spaces">                </code><code class="java plain">sender.send(dj);</code></div>
<div class="line number104 index103 alt1"><code class="java spaces">                </code><code class="java plain">sender.close();</code></div>
<div class="line number105 index104 alt2"><code class="java spaces">            </code><code class="java plain">} </code><code class="java keyword">catch</code><code class="java plain">(IOException e) {</code></div>
<div class="line number106 index105 alt1"><code class="java spaces">                </code><code class="java plain">e.printStackTrace();</code></div>
<div class="line number107 index106 alt2"><code class="java spaces">            </code><code class="java plain">}</code></div>
<div class="line number108 index107 alt1"><code class="java spaces">        </code><code class="java plain">}</code></div>
<div class="line number109 index108 alt2"><code class="java spaces">    </code><code class="java plain">}</code></div>
<div class="line number110 index109 alt1"></div>
<div class="line number111 index110 alt2"><code class="java spaces">    </code><code class="java comments">/*接收udp多播 并 发送tcp 连接*/</code></div>
<div class="line number112 index111 alt1"><code class="java spaces">    </code><code class="java keyword">private</code> <code class="java keyword">class</code> <code class="java plain">udpReceiveAndtcpSend </code><code class="java keyword">extends</code>  <code class="java plain">Thread {</code></div>
<div class="line number113 index112 alt2"><code class="java spaces">        </code><code class="java color1">@Override</code></div>
<div class="line number114 index113 alt1"><code class="java spaces">        </code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">run() {</code></div>
<div class="line number115 index114 alt2"><code class="java spaces">            </code><code class="java keyword">byte</code><code class="java plain">[] data = </code><code class="java keyword">new</code> <code class="java keyword">byte</code><code class="java plain">[</code><code class="java value">1024</code><code class="java plain">];</code></div>
<div class="line number116 index115 alt1"><code class="java spaces">            </code><code class="java keyword">try</code> <code class="java plain">{</code></div>
<div class="line number117 index116 alt2"><code class="java spaces">                </code><code class="java plain">InetAddress groupAddress = InetAddress.getByName(</code><code class="java string">"224.0.0.1"</code><code class="java plain">);</code></div>
<div class="line number118 index117 alt1"><code class="java spaces">                </code><code class="java plain">ms = </code><code class="java keyword">new</code> <code class="java plain">MulticastSocket(</code><code class="java value">6789</code><code class="java plain">);</code></div>
<div class="line number119 index118 alt2"><code class="java spaces">                </code><code class="java plain">ms.joinGroup(groupAddress);</code></div>
<div class="line number120 index119 alt1"><code class="java spaces">            </code><code class="java plain">} </code><code class="java keyword">catch</code> <code class="java plain">(Exception e) {</code></div>
<div class="line number121 index120 alt2"><code class="java spaces">                </code><code class="java plain">e.printStackTrace();</code></div>
<div class="line number122 index121 alt1"><code class="java spaces">            </code><code class="java plain">}</code></div>
<div class="line number123 index122 alt2"></div>
<div class="line number124 index123 alt1"><code class="java spaces">            </code><code class="java keyword">while</code> <code class="java plain">(</code><code class="java keyword">true</code><code class="java plain">) {</code></div>
<div class="line number125 index124 alt2"><code class="java spaces">                </code><code class="java keyword">try</code> <code class="java plain">{</code></div>
<div class="line number126 index125 alt1"><code class="java spaces">                    </code><code class="java plain">dp = </code><code class="java keyword">new</code> <code class="java plain">DatagramPacket(data, data.length);</code></div>
<div class="line number127 index126 alt2"><code class="java spaces">                    </code><code class="java keyword">if</code> <code class="java plain">(ms != </code><code class="java keyword">null</code><code class="java plain">)</code></div>
<div class="line number128 index127 alt1"><code class="java spaces">                       </code><code class="java plain">ms.receive(dp);</code></div>
<div class="line number129 index128 alt2"><code class="java spaces">                </code><code class="java plain">} </code><code class="java keyword">catch</code> <code class="java plain">(Exception e) {</code></div>
<div class="line number130 index129 alt1"><code class="java spaces">                    </code><code class="java plain">e.printStackTrace();</code></div>
<div class="line number131 index130 alt2"><code class="java spaces">                </code><code class="java plain">}</code></div>
<div class="line number132 index131 alt1"></div>
<div class="line number133 index132 alt2"><code class="java spaces">                </code><code class="java keyword">if</code> <code class="java plain">(dp.getAddress() != </code><code class="java keyword">null</code><code class="java plain">) {</code></div>
<div class="line number134 index133 alt1"><code class="java spaces">                    </code><code class="java keyword">final</code> <code class="java plain">String quest_ip = dp.getAddress().toString();</code></div>
<div class="line number135 index134 alt2"></div>
<div class="line number136 index135 alt1"><code class="java spaces">                    </code><code class="java comments">/* 若udp包的ip地址 是 本机的ip地址的话,丢掉这个包(不处理)*/</code></div>
<div class="line number137 index136 alt2"></div>
<div class="line number138 index137 alt1"><code class="java spaces">                    </code><code class="java comments">//String host_ip = getLocalIPAddress();</code></div>
<div class="line number139 index138 alt2"></div>
<div class="line number140 index139 alt1"><code class="java spaces">                    </code><code class="java plain">String host_ip = getLocalHostIp();</code></div>
<div class="line number141 index140 alt2"></div>
<div class="line number142 index141 alt1"><code class="java spaces">                    </code><code class="java plain">System.out.println(</code><code class="java string">"host_ip:  --------------------  "</code> <code class="java plain">+ host_ip);</code></div>
<div class="line number143 index142 alt2"><code class="java spaces">                    </code><code class="java plain">System.out.println(</code><code class="java string">"quest_ip: --------------------  "</code> <code class="java plain">+ quest_ip.substring(</code><code class="java value">1</code><code class="java plain">));</code></div>
<div class="line number144 index143 alt1"></div>
<div class="line number145 index144 alt2"><code class="java spaces">                    </code><code class="java keyword">if</code><code class="java plain">( (!host_ip.equals(</code><code class="java string">""</code><code class="java plain">))  &amp;&amp; host_ip.equals(quest_ip.substring(</code><code class="java value">1</code><code class="java plain">)) ) {</code></div>
<div class="line number146 index145 alt1"><code class="java spaces">                        </code><code class="java keyword">continue</code><code class="java plain">;</code></div>
<div class="line number147 index146 alt2"><code class="java spaces">                    </code><code class="java plain">}</code></div>
<div class="line number148 index147 alt1"></div>
<div class="line number149 index148 alt2"><code class="java spaces">                    </code><code class="java keyword">final</code> <code class="java plain">String codeString = </code><code class="java keyword">new</code> <code class="java plain">String(data, </code><code class="java value">0</code><code class="java plain">, dp.getLength());</code></div>
<div class="line number150 index149 alt1"></div>
<div class="line number151 index150 alt2"><code class="java spaces">                    </code><code class="java plain">receive_label.post(</code><code class="java keyword">new</code> <code class="java plain">Runnable() {</code></div>
<div class="line number152 index151 alt1"><code class="java spaces">                        </code><code class="java color1">@Override</code></div>
<div class="line number153 index152 alt2"><code class="java spaces">                        </code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">run() {</code></div>
<div class="line number154 index153 alt1"><code class="java spaces">                            </code><code class="java plain">receive_label.append(</code><code class="java string">"收到来自: \n"</code> <code class="java plain">+ quest_ip.substring(</code><code class="java value">1</code><code class="java plain">) + </code><code class="java string">"\n"</code> <code class="java plain">+</code><code class="java string">"的udp请求\n"</code><code class="java plain">);</code></div>
<div class="line number155 index154 alt2"><code class="java spaces">                            </code><code class="java plain">receive_label.append(</code><code class="java string">"请求内容: "</code> <code class="java plain">+ codeString + </code><code class="java string">"\n\n"</code><code class="java plain">);</code></div>
<div class="line number156 index155 alt1"><code class="java spaces">                        </code><code class="java plain">}</code></div>
<div class="line number157 index156 alt2"><code class="java spaces">                    </code><code class="java plain">});</code></div>
<div class="line number158 index157 alt1"><code class="java spaces">                    </code><code class="java keyword">try</code> <code class="java plain">{</code></div>
<div class="line number159 index158 alt2"><code class="java spaces">                        </code><code class="java keyword">final</code> <code class="java plain">String target_ip = dp.getAddress().toString().substring(</code><code class="java value">1</code><code class="java plain">);</code></div>
<div class="line number160 index159 alt1"><code class="java spaces">                        </code><code class="java plain">send_label.post(</code><code class="java keyword">new</code> <code class="java plain">Runnable() {</code></div>
<div class="line number161 index160 alt2"><code class="java spaces">                            </code><code class="java color1">@Override</code></div>
<div class="line number162 index161 alt1"><code class="java spaces">                            </code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">run() {</code></div>
<div class="line number163 index162 alt2"><code class="java spaces">                                </code><code class="java plain">send_label.append(</code><code class="java string">"发送tcp请求到: \n"</code> <code class="java plain">+ target_ip + </code><code class="java string">"\n"</code><code class="java plain">);</code></div>
<div class="line number164 index163 alt1"><code class="java spaces">                            </code><code class="java plain">}</code></div>
<div class="line number165 index164 alt2"><code class="java spaces">                        </code><code class="java plain">});</code></div>
<div class="line number166 index165 alt1"><code class="java spaces">                        </code><code class="java plain">socket = </code><code class="java keyword">new</code> <code class="java plain">Socket(target_ip, </code><code class="java value">8080</code><code class="java plain">);</code></div>
<div class="line number167 index166 alt2"><code class="java spaces">                    </code><code class="java plain">} </code><code class="java keyword">catch</code> <code class="java plain">(IOException e) {</code></div>
<div class="line number168 index167 alt1"><code class="java spaces">                        </code><code class="java plain">e.printStackTrace();</code></div>
<div class="line number169 index168 alt2"><code class="java spaces">                    </code><code class="java plain">} </code><code class="java keyword">finally</code> <code class="java plain">{</code></div>
<div class="line number170 index169 alt1"></div>
<div class="line number171 index170 alt2"><code class="java spaces">                        </code><code class="java keyword">try</code> <code class="java plain">{</code></div>
<div class="line number172 index171 alt1"><code class="java spaces">                            </code><code class="java keyword">if</code> <code class="java plain">(socket != </code><code class="java keyword">null</code><code class="java plain">)</code></div>
<div class="line number173 index172 alt2"><code class="java spaces">                                </code><code class="java plain">socket.close();</code></div>
<div class="line number174 index173 alt1"><code class="java spaces">                        </code><code class="java plain">} </code><code class="java keyword">catch</code> <code class="java plain">(IOException e) {</code></div>
<div class="line number175 index174 alt2"><code class="java spaces">                            </code><code class="java plain">e.printStackTrace();</code></div>
<div class="line number176 index175 alt1"><code class="java spaces">                        </code><code class="java plain">}</code></div>
<div class="line number177 index176 alt2"><code class="java spaces">                    </code><code class="java plain">}</code></div>
<div class="line number178 index177 alt1"><code class="java spaces">                </code><code class="java plain">}</code></div>
<div class="line number179 index178 alt2"><code class="java spaces">            </code><code class="java plain">}</code></div>
<div class="line number180 index179 alt1"><code class="java spaces">        </code><code class="java plain">}</code></div>
<div class="line number181 index180 alt2"><code class="java spaces">    </code><code class="java plain">}</code></div>
<div class="line number182 index181 alt1"></div>
<div class="line number183 index182 alt2"></div>
<div class="line number184 index183 alt1"></div>
<div class="line number185 index184 alt2"><code class="java spaces">    </code><code class="java comments">/* 接收tcp连接 */</code></div>
<div class="line number186 index185 alt1"><code class="java spaces">    </code><code class="java keyword">private</code> <code class="java keyword">class</code> <code class="java plain">tcpReceive </code><code class="java keyword">extends</code>  <code class="java plain">Thread {</code></div>
<div class="line number187 index186 alt2"><code class="java spaces">        </code><code class="java plain">ServerSocket serverSocket;</code></div>
<div class="line number188 index187 alt1"><code class="java spaces">        </code><code class="java plain">Socket socket;</code></div>
<div class="line number189 index188 alt2"><code class="java spaces">        </code><code class="java plain">BufferedReader in;</code></div>
<div class="line number190 index189 alt1"><code class="java spaces">        </code><code class="java plain">String source_address;</code></div>
<div class="line number191 index190 alt2"></div>
<div class="line number192 index191 alt1"><code class="java spaces">        </code><code class="java color1">@Override</code></div>
<div class="line number193 index192 alt2"><code class="java spaces">        </code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">run() {</code></div>
<div class="line number194 index193 alt1"><code class="java spaces">            </code><code class="java keyword">while</code><code class="java plain">(</code><code class="java keyword">true</code><code class="java plain">) {</code></div>
<div class="line number195 index194 alt2"><code class="java spaces">                </code><code class="java plain">serverSocket = </code><code class="java keyword">null</code><code class="java plain">;</code></div>
<div class="line number196 index195 alt1"><code class="java spaces">                </code><code class="java plain">socket = </code><code class="java keyword">null</code><code class="java plain">;</code></div>
<div class="line number197 index196 alt2"><code class="java spaces">                </code><code class="java plain">in = </code><code class="java keyword">null</code><code class="java plain">;</code></div>
<div class="line number198 index197 alt1"><code class="java spaces">                </code><code class="java keyword">try</code> <code class="java plain">{</code></div>
<div class="line number199 index198 alt2"><code class="java spaces">                    </code><code class="java plain">Log.i(</code><code class="java string">"Tcp Receive"</code><code class="java plain">,</code><code class="java string">" new ServerSocket ++++++++++"</code><code class="java plain">);</code></div>
<div class="line number200 index199 alt1"><code class="java spaces">                    </code><code class="java plain">serverSocket = </code><code class="java keyword">new</code> <code class="java plain">ServerSocket(</code><code class="java value">8080</code><code class="java plain">);</code></div>
<div class="line number201 index200 alt2"></div>
<div class="line number202 index201 alt1"><code class="java spaces">                    </code><code class="java plain">socket = serverSocket.accept();</code></div>
<div class="line number203 index202 alt2"><code class="java spaces">                    </code><code class="java plain">Log.i(</code><code class="java string">"Tcp Receive"</code><code class="java plain">,</code><code class="java string">" get socket ++++++++++++++++"</code><code class="java plain">);</code></div>
<div class="line number204 index203 alt1"></div>
<div class="line number205 index204 alt2"><code class="java spaces">                    </code><code class="java keyword">if</code><code class="java plain">(socket != </code><code class="java keyword">null</code><code class="java plain">) {</code></div>
<div class="line number206 index205 alt1"><code class="java spaces">                        </code><code class="java plain">in = </code><code class="java keyword">new</code> <code class="java plain">BufferedReader(</code><code class="java keyword">new</code> <code class="java plain">InputStreamReader(socket.getInputStream()));</code></div>
<div class="line number207 index206 alt2"><code class="java spaces">                        </code><code class="java plain">StringBuilder sb = </code><code class="java keyword">new</code> <code class="java plain">StringBuilder();</code></div>
<div class="line number208 index207 alt1"><code class="java spaces">                        </code><code class="java plain">sb.append(socket.getInetAddress().getHostAddress());</code></div>
<div class="line number209 index208 alt2"></div>
<div class="line number210 index209 alt1"><code class="java spaces">                        </code><code class="java plain">String line = </code><code class="java keyword">null</code><code class="java plain">;</code></div>
<div class="line number211 index210 alt2"><code class="java spaces">                        </code><code class="java keyword">while</code> <code class="java plain">((line = in.readLine()) != </code><code class="java keyword">null</code> <code class="java plain">) {</code></div>
<div class="line number212 index211 alt1"><code class="java spaces">                            </code><code class="java plain">sb.append(line);</code></div>
<div class="line number213 index212 alt2"><code class="java spaces">                        </code><code class="java plain">}</code></div>
<div class="line number214 index213 alt1"></div>
<div class="line number215 index214 alt2"><code class="java spaces">                        </code><code class="java plain">source_address = sb.toString().trim();</code></div>
<div class="line number216 index215 alt1"><code class="java spaces">                        </code><code class="java plain">receive_label.post(</code><code class="java keyword">new</code> <code class="java plain">Runnable() {</code></div>
<div class="line number217 index216 alt2"><code class="java spaces">                            </code><code class="java color1">@Override</code></div>
<div class="line number218 index217 alt1"><code class="java spaces">                            </code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">run() {</code></div>
<div class="line number219 index218 alt2"><code class="java spaces">                                </code><code class="java plain">receive_label.append(</code><code class="java string">"收到来自: "</code><code class="java plain">+</code><code class="java string">"\n"</code> <code class="java plain">+source_address+</code><code class="java string">"\n"</code><code class="java plain">+</code><code class="java string">"的tcp请求\n\n"</code><code class="java plain">);</code></div>
<div class="line number220 index219 alt1"><code class="java spaces">                            </code><code class="java plain">}</code></div>
<div class="line number221 index220 alt2"><code class="java spaces">                        </code><code class="java plain">});</code></div>
<div class="line number222 index221 alt1"><code class="java spaces">                    </code><code class="java plain">}</code></div>
<div class="line number223 index222 alt2"><code class="java spaces">                </code><code class="java plain">} </code><code class="java keyword">catch</code> <code class="java plain">(IOException e1) {</code></div>
<div class="line number224 index223 alt1"><code class="java spaces">                    </code><code class="java plain">e1.printStackTrace();</code></div>
<div class="line number225 index224 alt2"><code class="java spaces">                </code><code class="java plain">} </code><code class="java keyword">finally</code> <code class="java plain">{</code></div>
<div class="line number226 index225 alt1"><code class="java spaces">                    </code><code class="java keyword">try</code> <code class="java plain">{</code></div>
<div class="line number227 index226 alt2"><code class="java spaces">                        </code><code class="java keyword">if</code> <code class="java plain">(in != </code><code class="java keyword">null</code><code class="java plain">)</code></div>
<div class="line number228 index227 alt1"><code class="java spaces">                            </code><code class="java plain">in.close();</code></div>
<div class="line number229 index228 alt2"><code class="java spaces">                        </code><code class="java keyword">if</code> <code class="java plain">(socket != </code><code class="java keyword">null</code><code class="java plain">)</code></div>
<div class="line number230 index229 alt1"><code class="java spaces">                            </code><code class="java plain">socket.close();</code></div>
<div class="line number231 index230 alt2"><code class="java spaces">                        </code><code class="java keyword">if</code> <code class="java plain">(serverSocket != </code><code class="java keyword">null</code><code class="java plain">)</code></div>
<div class="line number232 index231 alt1"><code class="java spaces">                            </code><code class="java plain">serverSocket.close();</code></div>
<div class="line number233 index232 alt2"><code class="java spaces">                    </code><code class="java plain">} </code><code class="java keyword">catch</code> <code class="java plain">(IOException e) {</code></div>
<div class="line number234 index233 alt1"><code class="java spaces">                        </code><code class="java plain">e.printStackTrace();</code></div>
<div class="line number235 index234 alt2"><code class="java spaces">                    </code><code class="java plain">}</code></div>
<div class="line number236 index235 alt1"><code class="java spaces">                </code><code class="java plain">}</code></div>
<div class="line number237 index236 alt2"><code class="java spaces">            </code><code class="java plain">}</code></div>
<div class="line number238 index237 alt1"><code class="java spaces">        </code><code class="java plain">}</code></div>
<div class="line number239 index238 alt2"><code class="java spaces">    </code><code class="java plain">}</code></div>
<div class="line number240 index239 alt1"></div>
<div class="line number241 index240 alt2"><code class="java spaces">    </code><code class="java keyword">public</code> <code class="java plain">String getLocalHostIp() {</code></div>
<div class="line number242 index241 alt1"><code class="java spaces">        </code><code class="java plain">String ipaddress = </code><code class="java string">""</code><code class="java plain">;</code></div>
<div class="line number243 index242 alt2"><code class="java spaces">        </code><code class="java keyword">try</code> <code class="java plain">{</code></div>
<div class="line number244 index243 alt1"><code class="java spaces">            </code><code class="java plain">Enumeration&lt;NetworkInterface&gt; en = NetworkInterface</code></div>
<div class="line number245 index244 alt2"><code class="java spaces">                    </code><code class="java plain">.getNetworkInterfaces();</code></div>
<div class="line number246 index245 alt1"><code class="java spaces">            </code><code class="java comments">// 遍历所用的网络接口</code></div>
<div class="line number247 index246 alt2"><code class="java spaces">            </code><code class="java keyword">while</code> <code class="java plain">(en.hasMoreElements()) {</code></div>
<div class="line number248 index247 alt1"><code class="java spaces">                </code><code class="java plain">NetworkInterface nif = en.nextElement();</code><code class="java comments">// 得到每一个网络接口绑定的所有ip</code></div>
<div class="line number249 index248 alt2"><code class="java spaces">                </code><code class="java plain">Enumeration&lt;InetAddress&gt; inet = nif.getInetAddresses();</code></div>
<div class="line number250 index249 alt1"><code class="java spaces">                </code><code class="java comments">// 遍历每一个接口绑定的所有ip</code></div>
<div class="line number251 index250 alt2"><code class="java spaces">                </code><code class="java keyword">while</code> <code class="java plain">(inet.hasMoreElements()) {</code></div>
<div class="line number252 index251 alt1"><code class="java spaces">                    </code><code class="java plain">InetAddress ip = inet.nextElement();</code></div>
<div class="line number253 index252 alt2"><code class="java spaces">                    </code><code class="java keyword">if</code> <code class="java plain">(!ip.isLoopbackAddress()</code></div>
<div class="line number254 index253 alt1"><code class="java spaces">                            </code><code class="java plain">&amp;&amp; InetAddressUtils.isIPv4Address(ip</code></div>
<div class="line number255 index254 alt2"><code class="java spaces">                            </code><code class="java plain">.getHostAddress())) {</code></div>
<div class="line number256 index255 alt1"><code class="java spaces">                        </code><code class="java keyword">return</code> <code class="java plain">ip.getHostAddress();</code></div>
<div class="line number257 index256 alt2"><code class="java spaces">                    </code><code class="java plain">}</code></div>
<div class="line number258 index257 alt1"><code class="java spaces">                </code><code class="java plain">}</code></div>
<div class="line number259 index258 alt2"><code class="java spaces">            </code><code class="java plain">}</code></div>
<div class="line number260 index259 alt1"><code class="java spaces">        </code><code class="java plain">}</code></div>
<div class="line number261 index260 alt2"><code class="java spaces">        </code><code class="java keyword">catch</code><code class="java plain">(SocketException e)</code></div>
<div class="line number262 index261 alt1"><code class="java spaces">        </code><code class="java plain">{</code></div>
<div class="line number263 index262 alt2"><code class="java spaces">                </code><code class="java plain">Log.e(</code><code class="java string">"feige"</code><code class="java plain">, </code><code class="java string">"获取本地ip地址失败"</code><code class="java plain">);</code></div>
<div class="line number264 index263 alt1"><code class="java spaces">                </code><code class="java plain">e.printStackTrace();</code></div>
<div class="line number265 index264 alt2"><code class="java spaces">        </code><code class="java plain">}</code></div>
<div class="line number266 index265 alt1"><code class="java spaces">        </code><code class="java keyword">return</code> <code class="java plain">ipaddress;</code></div>
<div class="line number267 index266 alt2"><code class="java spaces">    </code><code class="java plain">}</code></div>
<div class="line number268 index267 alt1"></div>
<div class="line number269 index268 alt2"><code class="java spaces">    </code><code class="java keyword">private</code> <code class="java plain">String getLocalIPAddress() {</code></div>
<div class="line number270 index269 alt1"><code class="java spaces">        </code><code class="java keyword">try</code> <code class="java plain">{</code></div>
<div class="line number271 index270 alt2"><code class="java spaces">            </code><code class="java keyword">for</code> <code class="java plain">(Enumeration&lt;NetworkInterface&gt; en = NetworkInterface</code></div>
<div class="line number272 index271 alt1"><code class="java spaces">                    </code><code class="java plain">.getNetworkInterfaces(); en.hasMoreElements();) {</code></div>
<div class="line number273 index272 alt2"><code class="java spaces">                </code><code class="java plain">NetworkInterface intf = en.nextElement();</code></div>
<div class="line number274 index273 alt1"><code class="java spaces">                </code><code class="java keyword">for</code> <code class="java plain">(Enumeration&lt;InetAddress&gt; enumIpAddr = intf</code></div>
<div class="line number275 index274 alt2"><code class="java spaces">                        </code><code class="java plain">.getInetAddresses(); enumIpAddr.hasMoreElements();) {</code></div>
<div class="line number276 index275 alt1"><code class="java spaces">                    </code><code class="java plain">InetAddress inetAddress = enumIpAddr.nextElement();</code></div>
<div class="line number277 index276 alt2"><code class="java spaces">                    </code><code class="java keyword">if</code> <code class="java plain">(!inetAddress.isLoopbackAddress()) {</code></div>
<div class="line number278 index277 alt1"><code class="java spaces">                        </code><code class="java keyword">return</code> <code class="java plain">inetAddress.getHostAddress().toString();</code></div>
<div class="line number279 index278 alt2"><code class="java spaces">                    </code><code class="java plain">}</code></div>
<div class="line number280 index279 alt1"><code class="java spaces">                </code><code class="java plain">}</code></div>
<div class="line number281 index280 alt2"><code class="java spaces">            </code><code class="java plain">}</code></div>
<div class="line number282 index281 alt1"><code class="java spaces">        </code><code class="java plain">} </code><code class="java keyword">catch</code> <code class="java plain">(SocketException ex) {</code></div>
<div class="line number283 index282 alt2"><code class="java spaces">            </code><code class="java plain">Log.e(LOG_TAG, ex.toString());</code></div>
<div class="line number284 index283 alt1"><code class="java spaces">        </code><code class="java plain">}</code></div>
<div class="line number285 index284 alt2"><code class="java spaces">        </code><code class="java keyword">return</code> <code class="java keyword">null</code><code class="java plain">;</code></div>
<div class="line number286 index285 alt1"><code class="java spaces">    </code><code class="java plain">}</code></div>
<div class="line number287 index286 alt2"></div>
<div class="line number288 index287 alt1"><code class="java spaces">    </code><code class="java comments">// 按下返回键时,关闭 多播socket ms</code></div>
<div class="line number289 index288 alt2"><code class="java spaces">    </code><code class="java color1">@Override</code></div>
<div class="line number290 index289 alt1"><code class="java spaces">    </code><code class="java keyword">public</code> <code class="java keyword">void</code> <code class="java plain">onBackPressed() {</code></div>
<div class="line number291 index290 alt2"><code class="java spaces">        </code><code class="java plain">ms.close();</code></div>
<div class="line number292 index291 alt1"><code class="java spaces">        </code><code class="java keyword">super</code><code class="java plain">.onBackPressed();</code></div>
<div class="line number293 index292 alt2"><code class="java spaces">    </code><code class="java plain">}</code></div>
<div class="line number294 index293 alt1"></div>
<div class="line number295 index294 alt2"><code class="java spaces">    </code><code class="java color1">@Override</code></div>
<div class="line number296 index295 alt1"><code class="java spaces">    </code><code class="java keyword">public</code> <code class="java keyword">boolean</code> <code class="java plain">onCreateOptionsMenu(Menu menu) {</code></div>
<div class="line number297 index296 alt2"><code class="java spaces">        </code><code class="java comments">// Inflate the menu; this adds items to the action bar if it is present.</code></div>
<div class="line number298 index297 alt1"><code class="java spaces">        </code><code class="java plain">getMenuInflater().inflate(R.menu.my, menu);</code></div>
<div class="line number299 index298 alt2"><code class="java spaces">        </code><code class="java keyword">return</code> <code class="java keyword">true</code><code class="java plain">;</code></div>
<div class="line number300 index299 alt1"><code class="java spaces">    </code><code class="java plain">}</code></div>
<div class="line number301 index300 alt2"></div>
<div class="line number302 index301 alt1"><code class="java spaces">    </code><code class="java color1">@Override</code></div>
<div class="line number303 index302 alt2"><code class="java spaces">    </code><code class="java keyword">public</code> <code class="java keyword">boolean</code> <code class="java plain">onOptionsItemSelected(MenuItem item) {</code></div>
<div class="line number304 index303 alt1"><code class="java spaces">        </code><code class="java comments">// Handle action bar item clicks here. The action bar will</code></div>
<div class="line number305 index304 alt2"><code class="java spaces">        </code><code class="java comments">// automatically handle clicks on the Home/Up button, so long</code></div>
<div class="line number306 index305 alt1"><code class="java spaces">        </code><code class="java comments">// as you specify a parent activity in AndroidManifest.xml.</code></div>
<div class="line number307 index306 alt2"><code class="java spaces">        </code><code class="java keyword">int</code> <code class="java plain">id = item.getItemId();</code></div>
<div class="line number308 index307 alt1"><code class="java spaces">        </code><code class="java keyword">if</code> <code class="java plain">(id == R.id.action_settings) {</code></div>
<div class="line number309 index308 alt2"><code class="java spaces">            </code><code class="java keyword">return</code> <code class="java keyword">true</code><code class="java plain">;</code></div>
<div class="line number310 index309 alt1"><code class="java spaces">        </code><code class="java plain">}</code></div>
<div class="line number311 index310 alt2"><code class="java spaces">        </code><code class="java keyword">return</code> <code class="java keyword">super</code><code class="java plain">.onOptionsItemSelected(item);</code></div>
<div class="line number312 index311 alt1"><code class="java spaces">    </code><code class="java plain">}</code></div>
<div class="line number313 index312 alt2"><code class="java plain">}</code></div>
<div class="line number313 index312 alt2">
如果有错漏的地方,还请批评指正。毕竟是初学者,不出错,不疏忽是不可能的。
赞(0) 打赏
分享到: 更多 (0)
标签:

相关推荐

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏