轉自:http://blog.csdn.net/linweidong/article/details/6273507java
需求:android
Android的apk獲取手機信息,把結果發給PC clientsocket
注意地方:tcp
1.android默認手機端的IP爲「127.0.0.1」ide
2.要想聯通PC與android手機的sokcet,必定要用adb forward 來做下端口轉發才能連上socket.this
3.使用socket通訊,須要在mainfest.xml中添加permission: android.permission.INTERNETspa
Runtime.getRuntime().exec("adb forward tcp:12580 tcp:10086");
Thread.sleep(3000);
Android做爲服務import java.io.BufferedInputStream;import java.io.BufferedOutputStream;.net
import java.io.IOException; import java.net.InetAddress; import java.net.ServerSocket; import java.net.Socket; public class TcpConnect implements Runnable{ private final int SERVER_PORT = 10086; private ServerSocket mServerSocket; private Socket mClient; private String mDeviceId; private String mDeviceType; public TcpConnect(String aDeviceId, String aDeviceType){ this.mDeviceId= aDeviceId; this.mDeviceType = aDeviceType; try { String ip = InetAddress.getLocalHost().getHostAddress(); System.out.println("ip地址是: " + ip); //System.out.println(aDeviceId + " 型號: " + aDeviceType); mServerSocket = new ServerSocket(SERVER_PORT); System.out.println("TcpConnect" + "創建Socket"); // listen(); } catch (IOException e) { // TODO Auto-generated catch block //e.printStackTrace(); System.out.println("TcpConnect" + e.getMessage()); } } public void listen(){ while(true){ try { mClient = mServerSocket.accept(); // Log.e("TcpConnect", "在積極的監聽"); } catch (IOException e) { // TODO Auto-generated catch block //e1.printStackTrace(); System.out.println("TcpConnect" + e.getMessage()); } } } @Override public void run() { // TODO Auto-generated method stub // if(mClient.isConnected()){ BufferedOutputStream out = null; System.out.println("TcpConnect" + "開始監聽"); while(true){ try{ // Log.e("TcpConnect", "開始監聽"); mClient = mServerSocket.accept(); // if(mClient.isConnected()){ System.out.println("TcpConnect" + "檢測到有鏈接"); out = new BufferedOutputStream(mClient.getOutputStream()); String recordStr = mDeviceId + "|" + mDeviceType; out.write(recordStr.getBytes("utf-8")); // int length = recordStr.getBytes().length; // byte[] b = recordStr.getBytes(); // out.writeInt(length); // out.write(b); out.flush(); // Log.e("TcpConnect", recordStr); // out.flush(); // } } catch(Exception e){
System.out.println("TcpConnect" + e.getMessage()); }finally{ if(out != null){ try { out.close(); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("TcpConnect" + e.getMessage()); } } if(mServerSocket != null){ try { mServerSocket.close(); } catch (IOException e) { // TODO Auto-generated catch block System.out.println("TcpConnect" + e.getMessage()); } } // } } } } public static void main(String[] args){ new Thread(new TcpConnect("2366578546946", "T959")).start(); } }
C#做爲客戶端,在客戶端進行綁定端口:code
Process p = new Process(); //實例一個Process類,啓動一個獨立進程 p.StartInfo.FileName = "cmd.exe"; //設定程序名 p.StartInfo.UseShellExecute = false; //關閉Shell的使用 p.StartInfo.RedirectStandardInput = true; //重定向標準輸入 p.StartInfo.RedirectStandardOutput = true; //重定向標準輸出 p.StartInfo.RedirectStandardError = true; //重定向錯誤輸出 p.StartInfo.CreateNoWindow = true; // 設置不顯示窗口 p.StartInfo.ErrorDialog = false; p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; p.Start(); p.StandardInput.WriteLine(@"adb forward tcp:12580 tcp:10086"); // Thread.Sleep(3000); SocketClient client = new SocketClient(); MessageBox.Show("收到的數據爲: " + client.listen());
C#的Socket客戶端:xml
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.Net.Sockets; namespace PreInstaller.IO { class SocketClient { public string listen() { Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPAddress myIP = IPAddress.Parse("127.0.0.1"); IPEndPoint EPhost = new IPEndPoint(myIP, int.Parse("12580")); client.Connect(EPhost); byte[] t_data = new byte[1024]; string data = null; int i = 0; while ((i = client.Receive(t_data)) != 0) { data = Encoding.UTF8.GetString(t_data, 0, i); } client.Close(); return data; } } }