public void startServer() throws IOException {
this.serverSocket = new ServerSocket(serverPort, backlog, bindAddress);
System.out.println(new Date() + ": Starting server.");
//The policy swith
this.policyService = new PolicyService();
this.policyService.start();
}
private class PolicyService extends Thread {
private ServerSocket policySock = null;
public void run() {
try {
this.policySock = new ServerSocket(843);
System.out.println(new Date() + ":The policy server is running on 843......");
Socket sock = null;
String request = null;
FlexStringReader br = null;
PrintWriter pw = null;
//FIXME, create another thread to handle the request.
while ((sock = this.policySock.accept()) != null) {
br = new FlexStringReader(sock.getInputStream());
pw = new PrintWriter(sock.getOutputStream());
System.out.println("Policy server:client socket=" + sock);
request = br.readLine();
System.out.println("Policy service: request=" + request);
if (request.startsWith("<policy-file-request/>")) {
pw.write("<?xml version=\"1.0\"?><cross-domain-policy><site-control permitted-cross-domain-policies=\"all\"/><allow-access-from domain=\"*\" to-ports=\"*\" /></cross-domain-policy>\0");
pw.flush();
System.out.println("Policy service:Policy file’s sent......");
//Wait until the xml’s pushed to client.
Thread.sleep(10000);
}
//FIXME, not a good way the release resources.
System.out.println("Policy service: close the client socket.");
br.close();
pw.close();
sock.close();
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
//FIXME, refactor it ..
if (this.policySock != null) {
try {
this.policySock.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
/**
* Service’s running
* @throws IOException
*/
public void service() throws IOException {
Socket clientSock = null;
Thread handler = null;
while (!this.toStopServer && (clientSock = this.serverSocket.accept()) != null) {
//FIXME, only a demo, try to control the threads using producer/consumer,threadpool in your project.
handler = new Thread(new RequestHanlder(clientSock));
handler.start();
}
this.releaseResource();
}
public void releaseResource() {
if (this.serverSocket != null)
try {
this.serverSocket.close();
this.serverSocket = null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param args
*/
public static void main(String[] args) {
CellPhoneLocator server = new CellPhoneLocator();
try {
server.startServer();
server.service();
} catch (IOException e) {
e.printStackTrace();
server.releaseResource();
}
}
/**
* @return the serverPort
*/
public int getServerPort() {
return serverPort;
}
/**
* @param serverPort the serverPort to set
*/
public void setServerPort(int serverPort) {
this.serverPort = serverPort;
}
}
麻烦的只是权限, 因为flash player只相信一个原则, SWF从哪里来, 它就相信哪个域; 也就是说SWF本地运行还有从网络下载到浏览器它的采访权限是完全不一样的, SANDBOX沙漏这个限制就好像java applet一样. 我们需要设置好policy.
1. 在本地Flash player运行, I mean 直接点击生成的SWF
不需要设置, XMLSocket可以直接发送接受服务器的信息. 不过我这里用的都是localhost, 不知道使用外部IP会如何..不确定.
2. SWF放在web 服务器下, 使用浏览器采访
(1)默认先连XMLSocket目标服务器的843端口, 发送<policy-file-request/>\0 类似这样的信息.
我们返回"<?xml version=\"1.0\"?><cross-domain-policy><site-control permitted-cross-domain-policies=\"all\"/><allow-access-from domain=\"*\" to-ports=\"*\" /></cross-domain-policy>\0"
(2) 如1失败, 则查看AS中是否
Security.loadPolicyFile( "xmlsocket://host:port" ) 有则尝试加载crossdomain.xml
(3)2失败则只能链XMLSocket的目标端口了. 还失败就不可能建立连接了.
3.需要注意的是XMLSocket未必一定要发送XML, 你也可以直接发送String, 但是flex里面的string发送过去是以\0结束的.
后台需要额外处理一下\0; 返回到flash的字符串也必须使用\0才能在在客户端接收到. 冇办法, 每个语言都有自己的规则.
private function dataHandler(event:DataEvent):void {
trace("dataHandler: " + event);
this.appendLine("Server Response:" + event.data);
//this.messageArea.text += ("Server Response:" + event.target.toLocaleString() + "\r\n");
}
之后会继续测试一下Flex 的binary socket和其他IO 。
责任编辑:小草