簡體   English   中英

如何通過我的flex air應用程序通過域名來查找服務器的IP地址?

[英]How to find out my server's IP address through it's domain name from my flex air application?

我正在開發文件傳輸應用程序,其中客戶端將文件發送到cpp服務器。 在客戶端,我可以提供服務器的域名,但不能提供IP地址,因為它可能有所不同。 因此,任何人都可以告訴我如何通過域名獲取服務器的IP地址。 我必須將此邏輯應用於空中應用程序。 謝謝。

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

打開該頁面並滾動到底部; 注意他們正在調用Python腳本。 您可以通過NativeProcess API調用所需的任何Terminal / Console應用程序,並且可以將任何數量的參數作為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(如果您使用的是<2 SDK,則可以覆蓋新的AIR SDK),並且還需要在應用程序中啟用extendedDesktop配置文件- descriptor.xml文件:

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

最后一個想法:我相信NativeProcess API要求將AIR應用程序作為本機應用程序安裝(即通過.exe文件安裝)。 但是,正如您在屏幕截圖中所看到的那樣,使用Flash Builder 4+可以輕松完成此操作。

在此處輸入圖片說明

但是,如果您沒有Flash Builder 4,則始終可以 AIR SDK附帶的ADT編寫構建腳本 Rich Tretola的那篇文章很好地概括了基礎知識。

最后,這是我的小應用程序的使用情況:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM