Android UDP网络通信

  • f4_647477
    了解作者
  • 62.6KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-15 05:31
    上传日期
Android UDP网络通信程序,简单易懂,欢迎大家下载~~
Android网络通信.zip
内容介绍
package com.android.tcp; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.InetAddress; import java.net.InetSocketAddress; import java.net.NetworkInterface; import java.net.Socket; import java.net.SocketAddress; import java.net.SocketException; import java.util.Enumeration; import com.android.tcp.R; import android.app.Activity; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; public class TCPComActivity extends Activity implements OnClickListener{ /** Called when the activity is first created. */ private final String TAG="TcpClientActivity"; Button btnConnect; Button btnSend; EditText edtIpAddr; EditText edtPort; EditText edtSend; TextView tvIpPort; TextView tvRecv; boolean isConnected=false; Socket client; private OutputStream outputStream=null; private InputStream inputStream=null; private boolean thread_flag=true; private boolean thread_read_flag=true; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnConnect=(Button)findViewById(R.id.btnConnect); btnConnect.setOnClickListener(this); btnSend=(Button)findViewById(R.id.btnSend); btnSend.setOnClickListener(this); edtIpAddr=(EditText)findViewById(R.id.txtIp); edtPort=(EditText)findViewById(R.id.txtPort); edtSend=(EditText)findViewById(R.id.txtSend); tvIpPort=(TextView)findViewById(R.id.tvIPPort); tvRecv=(TextView)findViewById(R.id.tvRecv); edtIpAddr.setText("192.168.1.2"); edtPort.setText("26000"); edtSend.setText("Hello!"); String str=new String(); str=String.format("本地IP地址:"+getLocalIpAddress()); tvIpPort.setText(str); } public String getLocalIpAddress() { try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { return inetAddress.getHostAddress().toString(); } } } } catch (SocketException ex) { Log.e("WifiPreference IpAddress", ex.toString()); } return null; } public void onClick(View v) { // TODO Auto-generated method stub if(v.getId()==R.id.btnConnect) { try { if (false == isConnected) { InetAddress serverAddr = InetAddress.getByName(edtIpAddr.getText().toString());// TCPServer.SERVERIP int port=Integer.valueOf(edtPort.getText().toString()); SocketAddress my_sockaddr = new InetSocketAddress(serverAddr, port); //client = new Socket(serverAddr, port); client = new Socket(); client.connect(my_sockaddr,5000); btnConnect.setText("断开"); outputStream = client.getOutputStream(); inputStream = client.getInputStream(); isConnected = true; thread_read_flag = true; //new Thread(new InputStreamThread()).start(); new Thread(new TCPServerThread()).start(); } else { btnConnect.setText("连接"); //tvRecv.setText(""); //edtSend.setText(""); client.close(); isConnected = false; thread_read_flag = false; } } catch (NumberFormatException e) { // TODO Auto-generated catch block Log.d(TAG, e.getMessage()); } catch (IOException e) { // TODO Auto-generated catch block Log.d(TAG, e.getMessage()); } } else if(v.getId()==R.id.btnSend) { // TODO Auto-generated method stub //Log.d(TAG, "sendText:"+edit_send.getText().toString()); try { outputStream.write(edtSend.getText().toString().getBytes()); outputStream.flush(); } catch (NumberFormatException e) { // TODO Auto-generated catch block // Log.d(TAG, e.getMessage()); } catch (IOException e) { // TODO Auto-generated catch block // Log.d(TAG, e.getMessage()); } } } public class TCPServerThread extends Thread { public TCPServerThread() { } public void run() { //tvRecv.setText("start"); byte[] buffer = new byte[1024]; final StringBuilder sb = new StringBuilder(); while (true) { try { int readSize = inputStream.read(buffer); Log.d(TAG,"readSize:"+readSize); //Server is stoping if(readSize == -1) { inputStream.close(); break; } //Update the receive editText else if(readSize>0) { sb.append(new String(buffer,0,readSize)); runOnUiThread(new Runnable() { public void run() { // TODO Auto-generated method stub tvRecv.setText(sb); } }); } } catch (IOException e) { e.printStackTrace(); //tvRecv.setText("error"); } } } // tvRecv.setText("end"); } @Override protected void onDestroy() { // TODO Auto-generated method stub Log.d(TAG,"onDestroy"); /*try { client.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }*/ super.onDestroy(); } }
评论
    相关推荐
    • android udpsocket
      手机之间udp通信的例子
    • android udp通信示例
      功能:PC上运行一个UDP服务程序,android手机上app在局域网内发送UDP广播的方式实现远程关机操作,一个android UDP收发示例。
    • android udp传输实例
      实现了androidudp的应用,比较好的资料
    • android UDP模拟
      androidUDP示例,多个runnable,在模拟器里可以运行
    • android UDP通讯
      基于udp协议的android 通信 1 将两台手机连上同一个wifi 2 分别将两个应用装到两台手机上,启动 3 将server界面上显示的ip地址输入到sender界面的editText内 4 在sender的输入框内输入文字,点击发送 5 server会...
    • android udp
      android 调用c语言的udp服务器端
    • android udp
      androidudp调试助手。支持16进制收发
    • AndroidUDP
      AndroidUDP数据的接收与发送,可以实际运用哦
    • android UDP
      简单实现手机与电脑的UDP通讯,供初学者使用!
    • GaussDB_100_1.0.1-DATABASE-REDHAT-64bit.tar.gz
      guassdb100在redhat上安装包,单机部署的包,安装步骤请看我的文中介绍,经过大量实验搭建总结出来的文档