2010-06-01 36 views
1

我正在處理客戶端向cpp服務器發送文件的文件傳輸應用程序。在客戶端,我可以給服務器的域名,但不是IP地址因爲它可能會有所不同。所以任何人都可以告訴我如何通過它的域名獲得服務器的IP地址。我必須將這個邏輯應用於空中應用。謝謝。如何通過我的flex air應用程序的域名查找我的服務器的IP地址?

+0

你應該能夠做到使用域名文件傳輸。當您嘗試這樣做時,您是否遇到錯誤?你需要什麼IP地址? – 2010-06-03 20:54:56

回答

0

您可能會認爲NativeProcess API是您的問題的潛在解決方案。

打開該頁面並滾動到底部;注意他們正在調用Python腳本。您可以通過NativeProcess API調用所需的任何終端/控制檯應用程序,並且您可以將該進程的任意數量的參數作爲Vector.<string>()提供。基本上,我建議你可以嘗試撥打cmd.exe並通過ping www.your_unique_server.com。然後,您可以收到通過nativeProcess.standardOutput返回的回覆,並且Flex/AIR應用將解析IP。這裏有一個簡單的AIR應用程序,我寫這樣做:

<?xml version="1.0" encoding="utf-8"?> 
<s:WindowedApplication 
    xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx"> 

<s:layout> 
    <s:VerticalLayout paddingTop="20" paddingLeft="20" 
     paddingRight="20" paddingBottom="20"/> 
</s:layout> 

<s:Label text="Enter a Domain Name:"/> 
<s:TextInput id="domainTI" width="100%" text="www.google.com"/> 
<s:Spacer height="20"/> 
<s:TextArea id="pingsTA" width="100%" height="100%" 
     text="Provide a domain name and click 'Ping!'"/> 
<s:Button label="Ping!" click="initializeAndPing();"/> 

<fx:Script> 
<![CDATA[ 
private var nativeProcess:NativeProcess; 

// called by the button. 
private function initializeAndPing():void 
{ 
    if(NativeProcess.isSupported) 
    { 
    // make NativeProcess ready for use. 
    nativeProcess = new NativeProcess(); 

    nativeProcess.addEventListener(
     ProgressEvent.STANDARD_OUTPUT_DATA, onPingResult); 
    nativeProcess.addEventListener(
     ProgressEvent.STANDARD_ERROR_DATA, onStdError); 
    nativeProcess.addEventListener(
     IOErrorEvent.STANDARD_INPUT_IO_ERROR, onStdInError); 

    pingTheHost(); 
    } 
} 

private function pingTheHost():void 
{ 
    pingsTA.text=""; 

    var cmdFile:File = new File("C:\\Windows\\System32\\cmd.exe"); 

    var startInfo:NativeProcessStartupInfo; 
    startInfo = new NativeProcessStartupInfo(); 
    startInfo.executable = cmdFile; 

    // The \n special chars are necessary 
    // for the command to be executed. 
    var ping:String = "ping " + domainTI.text + "\n" ; 

    nativeProcess.start(startInfo); 
    nativeProcess.standardInput.writeUTFBytes(ping); 
} 

private function onPingResult(e:ProgressEvent):void 
{ 
    // you would need to parse the IP from the text string 
    // captured here to make it available as a variable. 

    pingsTA.text += 
    nativeProcess.standardOutput.readUTFBytes(
      nativeProcess.standardOutput.bytesAvailable); 
} 

private function onStdError(e:ProgressEvent):void 
{ 
    trace("StdError: " + 
    nativeProcess.standardError.readUTFBytes(
      nativeProcess.standardError.bytesAvailable)); 
} 

private function onStdInError(e:IOErrorEvent):void 
{ 
    trace("StdInError: " + e.toString()); 
} 

]]> 
</fx:Script> 

</s:WindowedApplication> 

要使用的NativeProcess,如在上面的應用程序,你需要一個AIR V2 + SDK(你可以overlay a new AIR SDK,如果你有一個< 2 SDK)和您還需要啓用extendedDesktop輪廓在您的應用程序,descriptor.xml文件:

<!-- uncomment this node and remove all but "extendedDesktop" --> 
<supportedProfiles>extendedDesktop</supportedProfiles> 

最後一個念頭:我相信的NativeProcess API需要您的AIR應用程序安裝爲本地應用程序(即用安裝。 exe文件)。但正如你在屏幕截圖中看到的,使用Flash Builder 4+很容易完成。

enter image description here

但是,如果你沒有Flash Builder 4中,則可以隨時write a build script for ADT,其中附帶的AIR SDK。 Rich Tretola撰寫的這篇文章在封裝基礎知識方面做得很好。

最後,這裏就是我的小應用程序看起來像在使用中:

enter image description here

相關問題