我正在处理文件传输应用程序,其中客户端将文件发送到cpp服务器。在客户端,我可以提供服务器的域名,但不能提供IP地址,因为它可能会有所不同。所以任何人都可以告诉我如何通过它的域名获取服务器的IP地址。我必须把这个逻辑用于空中应用。谢谢。
答案 0 :(得分: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(如果您有&lt; 2 SDK,则可以overlay a new AIR SDK)并且您还需要启用{{ 1}} app-descriptor.xml文件中的配置文件:
extendedDesktop
最后一个想法:我相信NativeProcess API要求将AIR应用程序安装为本机应用程序(即由.exe文件安装)。但正如您在屏幕截图中看到的,使用Flash Builder 4 +很容易实现。
但是,如果您没有Flash Builder 4,则可以随身携带AIR SDK附带的write a build script for ADT。 Rich Tretola的这篇文章很好地封装了基础知识。
最后,这是我的小应用程序在使用中的样子: