2011-03-15 98 views
3

我正在編寫一個代碼,需要每秒從Android移動設備向臺式計算機(linux服務器)發送數據。由於數據經常發送,通過Http命中無法實現(因爲會消耗時間),所以Tcp通信似乎是更好的選擇,因爲android手機的數據可以通過此套接字編程快速發送。 客戶端的Android手機上的代碼是:Android和Linux服務器之間的TCP連接

import java.io.IOException; 
import java.io.OutputStream; 
import java.io.PrintWriter; 
import java.net.InetAddress; 
import java.net.Socket; 
import java.net.UnknownHostException; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 


public class GetWebPage extends Activity { 

    //Handler h; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     final EditText eText = (EditText) findViewById(R.id.address); 
     final TextView tView = (TextView) findViewById(R.id.pagetext); 

     final Button button = (Button) findViewById(R.id.ButtonGo); 
     button.setOnClickListener(new Button.OnClickListener() { 
      public void onClick(View v) { 
       try { 
         Log.v("Tcp","Clicked the button"); 
          InetAddress serveraddress=InetAddress.getByName("67.23.14.156"); 
          Log.v("Tcp", "Got the InetAddress"); 
         Socket s = new Socket(serveraddress,4447); 
         Log.v("Tcp","Got the Socket address"); 
         OutputStream out = s.getOutputStream(); 
         PrintWriter output = new PrintWriter(out); 
         output.println("Hello Android!"); 
         out.close(); 

       } catch (UnknownHostException e) { 
        tView.setText(e.toString()); 
        Log.v("Tcp",e.toString()); 
       } catch (IOException e) { 
        tView.setText(e.toString()); 
        Log.v("Tcp",e.toString()); 
       }catch (Exception e) { 
        tView.setText(e.toString()); 

       } 
      } 
     });   
    } 
} 

服務器端代碼:

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.net.ServerSocket; 
import java.net.Socket; 

public class ListenIncomingTcpConnection { 

    public static void main(String[] args) { 
     ServerSocket serverSocket=null; 
     Socket client=null; 
     try { 
      System.out.println("Creating the server object..."); 
      serverSocket = new ServerSocket(4447); 
      System.out.println("Waiting for the connection..."); 
     } catch (IOException e1) { 
      System.out.println(e1); 

     } 

     while (true) { 

      try { 
       client = serverSocket.accept(); 
       System.out.println("Reading the content..."); 
      } catch (IOException e1) { 
       System.out.println(e1); 
       e1.printStackTrace(); 
      } 
      try { 
       BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream())); 
       String str = in.readLine(); 
       System.out.println("Reading the content....."); 
      } catch(Exception e) { 
       System.out.println(e); 
      } finally { 
       try{ 
        client.close(); 
       }catch(Exception e){ 
        System.out.println(e); 
       } 
      } 
     }//while 
    }//PSVM 

} 

清單文件的代碼是:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.spce" android:versionCode="1" android:versionName="1.0"> 
    <uses-sdk android:minSdkVersion="4" /> 


    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity android:name="GetWebPage"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

    </application> 
<uses-permission android:name="android.permission.INTERNET"></uses-permission> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> 
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission> 
</manifest> 

我已經執行了服務器端的代碼在linux機器上通過putty上的「java」命令。它在此行執行並停止「client = serverSocket.accept();」 當我執行的Android手機客戶端,它說:

單擊該按鈕 得到InetAddress是否 java.net.SocketException異常:沒有到主機的路由

我不能夠發現的這種情況的原因沒有路由到主機。

請幫忙解決問題。

回答

5

貌似你在一個IP地址做DNS查找

serveraddress=InetAddress.getByName("67.23.14.156")

嘗試直接使用的IP地址。

new Socket("67.23.14.156",4447);