android UDP模拟

  • y9_264672
    了解作者
  • 54.9KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-20 04:16
    上传日期
android的UDP示例,多个runnable,在模拟器里可以运行
udpen.rar
内容介绍
package com.androidituts.udp; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class UdpActivity extends Activity implements OnClickListener{ public static final String SERVERIP = "127.0.0.1"; // 'Within' the emulator! public static final int SERVERPORT = 4444; public TextView text1; public EditText input; public Button btn; public boolean start; public Handler Handler; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); text1=(TextView)findViewById(R.id.textView1); input=(EditText)findViewById(R.id.editText1); btn = (Button)findViewById(R.id.button1); btn.setOnClickListener(this); start=false; new Thread(new Server()).start(); try { Thread.sleep(500); } catch (InterruptedException e) { } new Thread(new Client()).start(); Handler = new Handler() { @Override public void handleMessage(Message msg) { String text = (String)msg.obj; text1.append(text); } }; } public class Client implements Runnable { @Override public void run() { while(start==false) { } try { Thread.sleep(500); } catch (InterruptedException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } try { InetAddress serverAddr = InetAddress.getByName(SERVERIP); updatetrack("Client: Start connecting\n"); DatagramSocket socket = new DatagramSocket(); byte[] buf; if(!input.getText().toString().isEmpty()) { buf=input.getText().toString().getBytes(); } else { buf = ("Default message").getBytes(); } DatagramPacket packet = new DatagramPacket(buf, buf.length, serverAddr, SERVERPORT); updatetrack("Client: Sending '" + new String(buf) + "'\n"); socket.send(packet); updatetrack("Client: Message sent\n"); updatetrack("Client: Succeed!\n"); } catch (Exception e) { updatetrack("Client: Error!\n"); } } } public class Server implements Runnable { @Override public void run() { while(start==false) { } try { InetAddress serverAddr = InetAddress.getByName(SERVERIP); updatetrack("\nServer: Start connecting\n"); DatagramSocket socket = new DatagramSocket(SERVERPORT, serverAddr); byte[] buf = new byte[17]; DatagramPacket packet = new DatagramPacket(buf, buf.length); updatetrack("Server: Receiving\n"); socket.receive(packet); updatetrack("Server: Message received: '" + new String(packet.getData()) + "'\n"); updatetrack("Server: Succeed!\n"); } catch (Exception e) { updatetrack("Server: Error!\n"); } } } @Override public void onClick(View v) { // TODO Auto-generated method stub start=true; } public void updatetrack(String s){ Message msg = new Message(); String textTochange = s; msg.obj = textTochange; Handler.sendMessage(msg); } }
评论
    相关推荐
    • android udpsocket
      手机之间udp通信的例子
    • android udp通信示例
      功能:PC上运行一个UDP服务程序,android手机上app在局域网内发送UDP广播的方式实现远程关机操作,一个android UDP收发示例。
    • android udp传输实例
      实现了androidudp的应用,比较好的资料
    • android UDP通讯
      基于udp协议的android 通信 1 将两台手机连上同一个wifi 2 分别将两个应用装到两台手机上,启动 3 将server界面上显示的ip地址输入到sender界面的editText内 4 在sender的输入框内输入文字,点击发送 5 server会...
    • Android UDP网络通信
      Android UDP网络通信程序,简单易懂,欢迎大家下载~~
    • android udp
      androidudp调试助手。支持16进制收发
    • AndroidUDP
      AndroidUDP数据的接收与发送,可以实际运用哦
    • android Socket UDP通信
      android SocketUDP通信,自己写的一个demo,既可以发送,也可以接收并且成功接收后并返回给发送者一个响应表示自己成功接收到了数据,挺好用的,分享给大家,欢迎大家吐槽。需要在Client类更改下IP,IP改为要发送到...
    • android UDP
      简单实现手机与电脑的UDP通讯,供初学者使用!
    • GaussDB_100_1.0.1-DATABASE-REDHAT-64bit.tar.gz
      guassdb100在redhat上安装包,单机部署的包,安装步骤请看我的文中介绍,经过大量实验搭建总结出来的文档