简体   繁体   中英

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. At client side I can give the server's domain name but not IP address cuz it may vary. So any one can tell me how can I get my server's IP address through it's domain name. I have to put this logic into air application. Thank you.

You might consider the NativeProcess API as a potential solution to your problem.

Open that page and scroll to the bottom; notice they're calling a Python script. 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>() . Basically, I'm suggesting you might try calling cmd.exe and passing it 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. Here's a simple AIR app I wrote to do this:

<?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:

<!-- 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). But as you see in the screenshot, this is easy to accomplish with 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. That post by Rich Tretola does a nice job of encapsulating the basics.

Finally, here's what my little app looks like in use:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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