简体   繁体   English

如何通过我的flex air应用程序通过域名来查找服务器的IP地址?

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

I'm working on file transfer application where client sends files to cpp server. 我正在开发文件传输应用程序,其中客户端将文件发送到cpp服务器。 At client side I can give the server's domain name but not IP address cuz it may vary. 在客户端,我可以提供服务器的域名,但不能提供IP地址,因为它可能有所不同。 So any one can tell me how can I get my server's IP address through it's domain name. 因此,任何人都可以告诉我如何通过域名获取服务器的IP地址。 I have to put this logic into air application. 我必须将此逻辑应用于空中应用程序。 Thank you. 谢谢。

You might consider the NativeProcess API as a potential solution to your problem. 您可能认为NativeProcess API是解决问题的潜在解决方案。

Open that page and scroll to the bottom; 打开该页面并滚动到底部; notice they're calling a Python script. 注意他们正在调用Python脚本。 You can call any Terminal/Console app you want through the NativeProcess API, and you can feed the process any number of arguments as a Vector.<string>() . 您可以通过NativeProcess API调用所需的任何Terminal / Console应用程序,并且可以将任何数量的参数作为Vector.<string>()送入进程。 Basically, I'm suggesting you might try calling cmd.exe and passing it ping www.your_unique_server.com . 基本上,我建议您尝试调用cmd.exe并将其通过ping www.your_unique_server.com传递。 Then, you can then catch the responses coming back through nativeProcess.standardOutput and you're Flex/AIR app will have the resolved IP to work with. 然后,您可以捕获通过nativeProcess.standardOutput返回的响应,并且您的Flex / AIR应用程序将具有解析的IP。 Here's a simple AIR app I wrote to do this: 这是我为此编写的一个简单的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>

To use NativeProcess, such as in the app above, you'll need an AIR v2+ SDK (you can overlay a new AIR SDK if you have a <2 SDK) and you'll also need to enable the extendedDesktop profile in your app-descriptor.xml file: 要使用NativeProcess(例如在上面的应用程序中),您需要AIR v2 + SDK(如果您使用的是<2 SDK,则可以覆盖新的AIR SDK),并且还需要在应用程序中启用extendedDesktop配置文件- descriptor.xml文件:

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

One last thought: I believe NativeProcess API requires your AIR app be installed as a native application (ie installed by an .exe file). 最后一个想法:我相信NativeProcess API要求将AIR应用程序作为本机应用程序安装(即通过.exe文件安装)。 But as you see in the screenshot, this is easy to accomplish with Flash Builder 4+. 但是,正如您在屏幕截图中所看到的那样,使用Flash Builder 4+可以轻松完成此操作。

在此处输入图片说明

However, if you don't have Flash Builder 4, you can always write a build script for ADT , which ships with the AIR SDK. 但是,如果您没有Flash Builder 4,则始终可以 AIR SDK附带的ADT编写构建脚本 That post by Rich Tretola does a nice job of encapsulating the basics. Rich Tretola的那篇文章很好地概括了基础知识。

Finally, here's what my little app looks like in use: 最后,这是我的小应用程序的使用情况:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM