要用android实现点对点通信,可以使用socket完成,使用socket需要知道对方的IP地址以及端口号,这里我省略了服务器端(编写正规的通信软件还是得需要服务器做中转,用服务器得知A的IP地址,然后让A做服务器端,B用A的Ip地址去获取数据),只是展示相关原理,首先看服务器端
package polly.liu; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.ServerSocket; import java.net.Socket; import java.net.SocketException; import java.util.Enumeration; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.widget.TextView; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.ServerSocket; import java.net.Socket; import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.widget.TextView; public class ServerActivity extends Activity { ServerSocket ss = null; String mClientMsg = ""; Thread myCommsThread = null; protected static final int MSG_ID = 0x1337; public static final int SERVERPORT = 6000; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView tv = (TextView) findViewById(R.id.TextView01); tv.setText("Nothing from client yet"); this.myCommsThread = new Thread(new CommsThread()); this.myCommsThread.start(); } @Override protected void onStop() { super.onStop(); try { // 确保你退出时要关闭socket连接 ss.close(); } catch (IOException e) { e.printStackTrace(); } } Handler myUpdateHandler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case MSG_ID: TextView tv = (TextView) findViewById(R.id.TextView01); tv.setText(mClientMsg); break; default: break; } super.handleMessage(msg); } }; class CommsThread implements Runnable { public void run() { Socket s = null; try { ss = new ServerSocket(SERVERPORT ); } catch (IOException e) { e.printStackTrace(); } while (!Thread.currentThread().isInterrupted()) { Message m = new Message(); m.what = MSG_ID; try { if (s == null) s = ss.accept(); BufferedReader input = new BufferedReader(new InputStreamReader(s.getInputStream())); String st = null; st = input.readLine(); mClientMsg = st; myUpdateHandler.sendMessage(m); } catch (IOException e) { e.printStackTrace(); } } } } }
服务器端和平时在PC上的代码差不多,只不过现在把在PC上的代码转到了手机端,注意耗时间的操作要放在子线程上去做,再来看看客户端代码:
package polly.liu; import java.io.BufferedWriter; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.net.InetAddress; import java.net.Socket; import java.net.UnknownHostException; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.view.View; public class ClientActivity extends Activity { private Button bt; private TextView tv; private Socket socket; private String serverIpAddress = "192.168.1.104"; private static final int REDIRECTED_SERVERPORT = 6000; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); bt = (Button) findViewById(R.id.myButton); tv = (TextView) findViewById(R.id.myTextView); try { InetAddress serverAddr = InetAddress.getByName(serverIpAddress); socket = new Socket(serverAddr, REDIRECTED_SERVERPORT); } catch (UnknownHostException e1) { e1.printStackTrace(); } catch (IOException e1) { e1.printStackTrace(); } bt.setOnClickListener(new OnClickListener() { public void onClick(View v) { try { EditText et = (EditText) findViewById(R.id.EditText01); String str = et.getText().toString(); PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true); out.println(str); Log.d("Client", "Client sent message"); } catch (UnknownHostException e) { tv.setText("Error1"); e.printStackTrace(); } catch (IOException e) { tv.setText("Error2"); e.printStackTrace(); } catch (Exception e) { tv.setText("Error3"); e.printStackTrace(); } } }); } }
注意要在manifest中加入权限
<uses-permission android:name="android.permission.INTERNET" > </uses-permission> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" > </uses-permission>
IP地址也需要根据情况去改