找传奇、传世资源到传世资源站!

Android手机与电脑的UDP通讯

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

本项目是一个测试安卓和电脑UDP通讯的例子源码

Android手机与电脑的UDP通讯 Android平台开发-第1张
Android手机与电脑的UDP通讯 Android平台开发-第2张
Android手机与电脑的UDP通讯 Android平台开发-第3张

package com.example.jstarudp;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 implementsandroid.view.View.OnClickListener {public static final String SERVERIP = "192.168.1.2"; public static final int SERVERPORT = 9088;public TextView text1;public EditText input;public Button btn;//public boolean start;public Handler MyHandler;@Overrideprotected 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);//new Thread(new Server()).start();/*start = false;new Thread(new Server()).start();try{Thread.sleep(500);} catch (InterruptedException e) {}new Thread(new Client()).start();*/MyHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {String text = (String) msg.obj;text1.append(text);}};}public class Client implements Runnable {@Overridepublic void run() {/* while (start == false) {} try {Thread.sleep(500); } catch (InterruptedException e1) {// TODO Auto-generated catch blocke1.printStackTrace(); }*/try {InetAddress serverAddr = InetAddress.getByName(SERVERIP);updatetrack("Client: Start connecting\n");DatagramSocket socket = new DatagramSocket(4444);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");byte[] recbuf = new byte[255];DatagramPacket recpacket = new DatagramPacket(recbuf, recbuf.length);updatetrack("Server: Receiving\n");Thread.sleep(500);socket.setSoTimeout(5);socket.receive(recpacket);updatetrack("Server: Message received: ‘" new String(recpacket.getData()) "’\n");updatetrack("Server: Succeed!\n");socket.close();} catch (Exception e) {updatetrack("Client: Error!\n");updatetrack(e.getMessage());}}}public class Server implements Runnable {@Overridepublic void run() {//while (start == false) {}while (true){ try {updatetrack("Server: Start connecting\n");DatagramSocket socket = new DatagramSocket(5555);byte[] buf = new byte[255];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");socket.close();} catch (Exception e) {updatetrack("Server: Error!\n");updatetrack(e.getMessage());}}}}public void updatetrack(String s) {Message msg = new Message();String textTochange = s;msg.obj = textTochange;MyHandler.sendMessage(msg);}@Overridepublic boolean onCreateOptionsMenu(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;}@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif(v.getId()==R.id.button1){//start = true;new Thread(new Client()).start();}}}/*import android.os.Bundle;import android.app.Activity;import android.view.Menu;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}@Overridepublic boolean onCreateOptionsMenu(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;}}*/

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复