socket 计算机和真机连接不上

socket 电脑和真机连接不上
我的程序是在wifi下使得手机和pc能做socket通信用的是安卓4.0.3  但是 在模拟机上运行的话 没有任何问题 一到真机测试就不行 连接不上 这是为什么?

hostip = getLocalIpAddress();  //获取本机IP
      
               
                tv.setText("HostIP:" + hostip ); 

        if (hostip != null) Log.d("GetIPMAC", hostip);   
        else Log.d("GetIPMAC", "null");   
              
               new socketThread().start();

 public void ShowDialog(String msg) {
        new AlertDialog.Builder(this).setTitle("notification").setMessage(msg).setPositiveButton("ok", new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub

                    }
                }).show();
    }

    public class socketThread extends Thread {
     @Override
    public void run() {
        try {
         content="";
         socket = new Socket(host, PORT);
                in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
                
            while (true) {
                if (socket.isConnected()) {
                    if (!socket.isInputShutdown()) {
                    
                        if ((content = in.readLine()) != null) {
                            
                        
                           content += "\n";
                            
                            mHandler.sendMessage(mHandler.obtainMessage());
                        } else {

                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            ShowDialog("login exception:" + e.getMessage());
        }
    }
    }
    public static String getLocalIpAddress() {  
        try {  
            for (Enumeration<NetworkInterface> en = NetworkInterface  
                            .getNetworkInterfaces(); en.hasMoreElements();) {  
                        NetworkInterface intf = en.nextElement();  
                       for (Enumeration<InetAddress> enumIpAddr = intf  
                                .getInetAddresses(); enumIpAddr.hasMoreElements();) {  
                            InetAddress inetAddress = enumIpAddr.nextElement();  
                            if (!inetAddress.isLoopbackAddress() && !inetAddress.isLinkLocalAddress()) {  
                            return inetAddress.getHostAddress().toString();  
                            }  
                       }  
                    }  
                } catch (SocketException ex) {  
                    Log.e("WifiPreference IpAddress", ex.toString());  
                }  
        
        
             return null;  

   
    
private String intToIp(int paramInt) {
return (paramInt & 0xFF) + "." + (0xFF & paramInt >> 8) + "." + (0xFF & paramInt >> 16) + "."
+ (0xFF & paramInt >> 24);
}

    public Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            tv_msg.setText(content);
   

            

        
            

            
        
           
------解决思路----------------------
LZ想要自动获取电脑IP是吧。
1,如果wifi热点是电脑发出来的,手机 可以采用下面方式去做。
WifiManager mWifi;
mWifi = (WifiManager)ctx.getSystemService(Context.WIFI_SERVICE);
DhcpInfo dhcpinfo = mWifi.getDhcpInfo();
this.ip = Formatter.formatIpAddress(dhcpinfo.gateway);

因为电脑是主机,可以访问 wifimanager 去得到主机IP或网关IP,通常这两个是一样的。

2,如果电脑不是主机,即手机和电脑都是通过连接路由器组成的局域网,这时候想要获得其中某一台的电脑IP的话,最好的方法就是 手机开 广播接受server,电脑开个 广播client,这样才有一个通讯的过程,才能拿到IP,你要是不想在电脑上面加任何东西,光靠手机去实现拉取IP的任务,我劝你还是采用 第一种方式,不然 基本不可能实现。