Android Datagram/Udp Customer Example

Posted on
I posted “Java Datagram/UDP Server as well as Client, run on raspberry Pi” on my about other blogspot. It’s the Android version of the client.


MainActivity.java

package com.blogspot.android_er.androidudpclient;  import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView;  world degree MainActivity extends AppCompatActivity {      EditText editTextAddress, editTextPort;     Button buttonConnect;     TextView textViewState, textViewRx;      UdpClientHandler udpClientHandler;     UdpClientThread udpClientThread;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);          editTextAddress = (EditText) findViewById(R.id.address);         editTextPort = (EditText) findViewById(R.id.port);         buttonConnect = (Button) findViewById(R.id.connect);         textViewState = (TextView)findViewById(R.id.state);         textViewRx = (TextView)findViewById(R.id.received);          buttonConnect.setOnClickListener(buttonConnectOnClickListener);          udpClientHandler = novel UdpClientHandler(this);     }      View.OnClickListener buttonConnectOnClickListener =             novel View.OnClickListener() {                  @Override                 world void onClick(View arg0) {                      udpClientThread = novel UdpClientThread(                             editTextAddress.getText().toString(),                             Integer.parseInt(editTextPort.getText().toString()),                             udpClientHandler);                     udpClientThread.start();                      buttonConnect.setEnabled(false);                 }             };      somebody void updateState(String state){         textViewState.setText(state);     }      somebody void updateRxMsg(String rxmsg){         textViewRx.append(rxmsg + "\n");     }      somebody void clientEnd(){         udpClientThread = null;         textViewState.setText("clientEnd()");         buttonConnect.setEnabled(true);      }      world static degree UdpClientHandler extends Handler {         world static lastly int UPDATE_STATE = 0;         world static lastly int UPDATE_MSG = 1;         world static lastly int UPDATE_END = 2;         somebody MainActivity parent;          world UdpClientHandler(MainActivity parent) {             super();             this.parent = parent;         }          @Override         world void handleMessage(Message msg) {              switch (msg.what){                 instance UPDATE_STATE:                     parent.updateState((String)msg.obj);                     break;                 instance UPDATE_MSG:                     parent.updateRxMsg((String)msg.obj);                     break;                 instance UPDATE_END:                     parent.clientEnd();                     break;                 default:                     super.handleMessage(msg);             }          }     } }  

UdpClientThread.java

package com.blogspot.android_er.androidudpclient;  import android.os.Message;  import java.io.IOException; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.net.InetAddress; import java.net.SocketException; import java.net.UnknownHostException;  world degree UdpClientThread extends Thread{      String dstAddress;     int dstPort;     somebody boolean running;     MainActivity.UdpClientHandler handler;      DatagramSocket socket;     InetAddress address;      world UdpClientThread(String addr, int port, MainActivity.UdpClientHandler handler) {         super();         dstAddress = addr;         dstPort = port;         this.handler = handler;     }      world void setRunning(boolean running){         this.running = running;     }      somebody void sendState(String state){         handler.sendMessage(                 Message.obtain(handler,                         MainActivity.UdpClientHandler.UPDATE_STATE, state));     }      @Override     world void run() {         sendState("connecting...");          running = true;          endeavor {             socket = novel DatagramSocket();             address = InetAddress.getByName(dstAddress);              // transportation asking             byte[] buf = novel byte[256];              DatagramPacket packet =                     novel DatagramPacket(buf, buf.length, address, dstPort);             socket.send(packet);              sendState("connected");              // larn answer             packet = novel DatagramPacket(buf, buf.length);               socket.receive(packet);             String trouble = novel String(packet.getData(), 0, packet.getLength());              handler.sendMessage(                     Message.obtain(handler, MainActivity.UdpClientHandler.UPDATE_MSG, line));          } grab (SocketException e) {             e.printStackTrace();         } grab (UnknownHostException e) {             e.printStackTrace();         } grab (IOException e) {             e.printStackTrace();         } finally {             if(socket != null){                 socket.close();                 handler.sendEmptyMessage(MainActivity.UdpClientHandler.UPDATE_END);             }         }      } }  

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <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:padding="16dp"     android:orientation="vertical"     tools:context="com.blogspot.android_er.androidudpclient.MainActivity">      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />     <EditText         android:id="@+id/address"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="192.168."         android:hint="dstAddress" />     <EditText         android:id="@+id/port"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="4445"         android:hint="dstPort" />     <Button         android:id="@+id/connect"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="Connect"/>      <TextView         android:id="@+id/state"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="un-initiated"         android:textSize="20dp"/>      <TextView         android:id="@+id/received"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:textSize="18dp"/> </LinearLayout>  

uses-permission of “android.permission.INTERNET” is needed inwards AndroidManifest.xml

  world degree MainActivity extends AppCompatActivity  Android Datagram/UDP Client exampleDownload the files .

Next:
– Android Datagram/UDP Server example