需求:
Android的apk获取手机信息,把结果发给PC client
注意事项:
1.android默认手机端的IP为“127.0.0.1”
2.要想联通PC与android手机的sokcet,一定要用adb forward 来作下端口转发才能连上socket.
3.使用socket通信,需要在mainfest.xml中添加permission: android.permission.INTERNET
- Runtime.getRuntime().exec("adbforwardtcp:12580tcp:10086");
- Thread.sleep(3000);
Android作为服务端:
- importjava.io.BufferedInputStream;
- importjava.io.BufferedOutputStream;
- importjava.io.IOException;
- importjava.net.InetAddress;
- importjava.net.ServerSocket;
- importjava.net.Socket;
- publicclassTcpConnectimplementsRunnable{
- privatefinalintSERVER_PORT=10086;
- privateServerSocketmServerSocket;
- privateSocketmClient;
- privateStringmDeviceId;
- privateStringmDeviceType;
- publicTcpConnect(StringaDeviceId,StringaDeviceType){
- this.mDeviceId=aDeviceId;
- this.mDeviceType=aDeviceType;
- try{
- Stringip=InetAddress.getLocalHost().getHostAddress();
- System.out.println("ip地址是:"+ip);
- //System.out.println(aDeviceId+"型号:"+aDeviceType);
- mServerSocket=newServerSocket(SERVER_PORT);
- System.out.println("TcpConnect"+"建立Socket");
- //listen();
- }catch(IOExceptione){
- //TODOAuto-generatedcatchblock
- //e.printStackTrace();
- System.out.println("TcpConnect"+e.getMessage());
- }
- }
- publicvoidlisten(){
- while(true){
- try{
- mClient=mServerSocket.accept();
- //Log.e("TcpConnect","在积极的监听");
- }catch(IOExceptione){
- //TODOAuto-generatedcatchblock
- //e1.printStackTrace();
- System.out.println("TcpConnect"+e.getMessage());
- }
- }
- }
- @Override
- publicvoidrun(){
- //TODOAuto-generatedmethodstub
- //if(mClient.isConnected()){
- BufferedOutputStreamout=null;
- System.out.println("TcpConnect"+"开始监听");
- while(true){
- try{
- //Log.e("TcpConnect","开始监听");
- mClient=mServerSocket.accept();
- //if(mClient.isConnected()){
- System.out.println("TcpConnect"+"检测到有连接");
- out=newBufferedOutputStream(mClient.getOutputStream());
- StringrecordStr=mDeviceId+"|"+mDeviceType;
- out.write(recordStr.getBytes("utf-8"));
- //intlength=recordStr.getBytes().length;
- //byte[]b=recordStr.getBytes();
- //out.writeInt(length);
- //out.write(b);
- out.flush();
- //Log.e("TcpConnect",recordStr);
- //out.flush();
- //}
- }catch(Exceptione){
- System.out.println("TcpConnect"+e.getMessage());
- }finally{
- if(out!=null){
- try{
- out.close();
- }catch(IOExceptione){
- //TODOAuto-generatedcatchblock
- System.out.println("TcpConnect"+e.getMessage());
- }
- }
- if(mServerSocket!=null){
- try{
- mServerSocket.close();
- }catch(IOExceptione){
- //TODOAuto-generatedcatchblock
- System.out.println("TcpConnect"+e.getMessage());
- }
- }
- //}
- }
- }
- }
- publicstaticvoidmain(String[]args){
- newThread(newTcpConnect("2366578546946","T959")).start();
- }
- }
C#作为客户端,在客户端进行绑定端口
- Processp=newProcess();//实例一个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(@"adbforwardtcp:12580tcp:10086");
- //Thread.Sleep(3000);
- SocketClientclient=newSocketClient();
- MessageBox.Show("收到的数据为:"+client.listen());
C#的Socket客户端view plain
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Text;
- usingSystem.Net;
- usingSystem.Net.Sockets;
- namespacePreInstaller.IO
- {
- classSocketClient
- {
- publicstringlisten()
- {
- Socketclient=newSocket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
- IPAddressmyIP=IPAddress.Parse("127.0.0.1");
- IPEndPointEPhost=newIPEndPoint(myIP,int.Parse("12580"));
- client.Connect(EPhost);
- byte[]t_data=newbyte[1024];
- stringdata=null;
- inti=0;
- while((i=client.Receive(t_data))!=0)
- {
- data=Encoding.UTF8.GetString(t_data,0,i);
- }
- client.Close();
- returndata;
- }
- }
- }