当前位置:操作系统 > 安卓/Android >>

android在wifi下手机与电脑的socket通信

在wifi局域网下,手机可以和电脑通过socket通信。手机做服务端,电脑做客户端;也可以反过来,电脑做服务端,手机做客户端。

下面介绍的是手机作为服务端,电脑使用MFC编程作为客户端,原理是,手机建立一个ServerSocket,并获取自己的ip地址和端口port;然后电脑通过socket连接手机的ip:port。\

\


 

代码下载地址:http://download.csdn.net/detail/menghnhhuan/4050488

 

手机建立socketServer代码

[html] private ServerSocket serverSocket = null; 
private Runnable    mcreateRunnable = new Runnable()  

    public void run() 
    {                
        try { 
            serverSocket = new ServerSocket(0);//系统分配端口                  
            getLocalIpAddress();//获得手机的ip地址 
                        //方法用于等待客服连接  
                        mSocketServer = serverSocket.accept();                         
                        //接受客服端数据BufferedReader对象 
                        mBufferedReaderServer = new BufferedReader(new InputStreamReader(mSocketServer.getInputStream())); 
                        //给客服端发送数据 
                        mPrintWriterServer = new PrintWriter(mSocketServer.getOutputStream(),true); 
                     
        } catch (IOException e) { 
            // TODO Auto-generated catch block 
            e.printStackTrace(); 
        } 
    } 
}; 
private ServerSocket serverSocket = null;
private Runnable mcreateRunnable = new Runnable()
{
 public void run()
 {    
  try {
   serverSocket = new ServerSocket(0);//系统分配端口     
   getLocalIpAddress();//获得手机的ip地址
                  //方法用于等待客服连接
                  mSocketServer = serverSocket.accept();                      
                  //接受客服端数据BufferedReader对象
                  mBufferedReaderServer = new BufferedReader(new InputStreamReader(mSocketServer.getInputStream()));
                  //给客服端发送数据
                  mPrintWriterServer = new PrintWriter(mSocketServer.getOutputStream(),true);
                
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
};
 

手机上获取的ip地址代码

[html] public String getLocalIpAddress()  

    try { 
        for (Enumeration<NetworkInte易做图ce> en=NetworkInte易做图ce.getNetworkInte易做图ces(); en.hasMoreElements();)  
        { 
            NetworkInte易做图ce intf = en.nextElement(); 
            for (Enumeration<InetAddress> enumIpAddr=intf.getInetAddresses();enumIpAddr.hasMoreElements();)  
            { 
                InetAddress inetAddress = enumIpAddr.nextElement(); 
                //if (!inetAddress.isLoopbackAddress())  
                { 
                    //if(inetAddress.isSiteLocalAddress()) 
                    { 
                        String ip = "请连接IP:"+inetAddress.getHostAddress()+":" 
                                + serverSocket.getLocalPort()+ "\n";     
                    } 
                } 
            } 
        } 
    }  
    catch (SocketException ex) { 
        ex.printStackTrace();   } 

public String getLocalIpAddress()
{
 try {
  for (Enumeration<NetworkInte易做图ce> en=NetworkInte易做图ce.getNetworkInte易做图ces(); en.hasMoreElements();)
  {
   NetworkInte易做图ce intf = en.nextElement();
   for (Enumeration<InetAddress> enumIpAddr=intf.getInetAddresses();enumIpAddr.hasMoreElements();)
   {
    InetAddress inetAddress = e

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,