- package com.example.udp;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.InetAddress;
- import android.os.Bundle;
- import android.os.Message;
- import android.app.Activity;
- import android.view.Menu;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import android.os.Handler;
- public class MainActivity extends Activity implements
- android.view.View.OnClickListener {
- public static final String SERVERIP = "127.0.0.1";
- public static final int SERVERPORT = 4444;
- public TextView text1;
- public EditText input;
- public Button btn;
- public boolean start;
- public Handler Handler;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_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");
- }
- }
- }
- public void updatetrack(String s) {
- Message msg = new Message();
- String textTochange = s;
- msg.obj = textTochange;
- Handler.sendMessage(msg);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- if(v.getId()==R.id.button1)
- start = true;
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <EditText
- android:id="@+id/editText1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" >
- <requestFocus />
- </EditText>
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Send" />
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Communication History:"
- android:textAppearance="?android:attr/textAppearanceLarge" />
- </LinearLayout>
AndroidManifest.xml
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.udp"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="16" />
- <uses-permission android:name="android.permission.INTERNET" />
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.example.udp.MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>