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);
   

            

        
            

            
        
           
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值