package sban.socketServer;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.URL;
/**
* sban policy server for flex/flash socket
*
* @author sban
*
*/
public class SbanSocketPolicyServer {
public SbanSocketPolicyServer() {
}
public static String readFileAsString(String url)
throws java.io.IOException {
byte[] buffer = new byte[(int) new File(url).length()];
BufferedInputStream f = new BufferedInputStream(
new FileInputStream(url));
f.read(buffer);
f.close();
return new String(buffer);
}
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket();
SocketAddress endpoint = new InetSocketAddress("127.0.0.1", 843);
server.bind(endpoint);
String path = SbanSocketPolicyServer.class.getResource( "crossdomain.xml" ).getPath();
System.out.println(path);
String policy = readFileAsString( path );
while (true) {
System.out.println("wait for client...");
Socket socket = server.accept();
BufferedReader in = new BufferedReader(new InputStreamReader(
socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream());
String s = in.readLine();
System.out.println(s);
if (s.indexOf("policy-file-request") > -1) {
out.print(policy + '\0');
out.flush();
in.close();
out.close();
System.out.println("sended policy context.");
socket.close();
}
}
}
}
crossdomain.xml文件
<cross-domain-policy>
<site-control permitted-cross-domain-policies="all"/>
<allow-access-from domain="*" to-ports="*" />
</cross-domain-policy>
沒有留言:
張貼留言