主代码:
packagecom.example.test; importjava.net.DatagramPacket; importjava.net.DatagramSocket; importjava.net.InetAddress; importandroid.os.Bundle; importandroid.os.Message; importandroid.app.Activity; importandroid.util.Log; importandroid.view.Menu; importandroid.view.View; importandroid.widget.Button; importandroid.widget.EditText; importandroid.widget.TextView; importandroid.os.Handler; public class MainActivity extends Activity implementsandroid.view.View.OnClickListener { public static final String SERVERIP = "127.0.0.1"; public static final int SERVERPORT = 4444; publicTextView text1; publicEditText input; publicButton btn; publicHandler Handler; @Override protected voidonCreate(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); Handler = newHandler() { @Override public voidhandleMessage(Message msg) { String text =(String) msg.obj; text1.append(text); } }; new Thread(newServer()).start(); new Thread(newClient()).start(); } public class Client implementsRunnable { privateDatagramPacket packet; privateDatagramSocket socket; publicClient() { } @Override public voidrun() { while(true) { if(Tools.Queue_Send.size()>0) { Log.d("Client个数", Tools.Queue_Send.size()+""); try{ InetAddress serverAddr =InetAddress.getByName(SERVERIP); updatetrack("Client: Start connecting "); socket = newDatagramSocket(); byte[] buf =Tools.Queue_Send.poll().toString().getBytes(); packet = newDatagramPacket(buf, buf.length, serverAddr, SERVERPORT); updatetrack("Client: Sending ‘" + new String(buf) + "’ "); socket.send(packet); socket.close(); updatetrack("Client: Message sent "); updatetrack("Client: Succeed! "); } catch(Exception e) { updatetrack("Client: Error! "); } } } } } public class Server implementsRunnable { @Override public voidrun() { while (true) { try{ InetAddress serverAddr =InetAddress.getByName(SERVERIP); updatetrack(" Server: Start connecting "); DatagramSocket socket = newDatagramSocket(SERVERPORT, serverAddr); byte[] buf = new byte[17]; DatagramPacket packet = newDatagramPacket(buf, buf.length); updatetrack("Server: Receiving "); socket.receive(packet); updatetrack("Server: Message received: ‘" + new String(packet.getData()) + "’ "); updatetrack("Server: Succeed! "); } catch(Exception e) { updatetrack("Server: Error! "); } } } } public voidupdatetrack(String s) { Message msg = newMessage(); String textTochange =s; msg.obj =textTochange; Handler.sendMessage(msg); } @Override public booleanonCreateOptionsMenu(Menu menu) { //Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true; } @Override public voidonClick(View v) { //TODO Auto-generated method stub if(v.getId()==R.id.button1) { Tools.Queue_Send.offer(input.getText().toString()); Log.d("加入个数", Tools.Queue_Send.size()+""); } } }
<?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 android:layout_width= "match_parent" /> </EditText> <Button android:id ="@+id/button1"android:layout_width ="wrap_content"android:layout_height ="wrap_content"android:text = "Send" /> <ScrollView android:layout_width= "wrap_content"android:layout_height= "wrap_content" > <TextView android:id= "@+id/textView1"android:layout_width= "wrap_content"android:layout_height= "wrap_content"android:scrollbars= "vertical"android:text= "Communication History:"android:textAppearance= "?android:attr/textAppearanceLarge" /> </ScrollView> </LinearLayout>
packagecom.example.test; importjava.util.ArrayList; importjava.util.LinkedList; importjava.util.Queue; public classTools { /*** 发送队列 * */ public static Queue<String> Queue_Send = new LinkedList<String>(); }