最近在做一个flash小游戏,
众所周知,flash连接服务器socket的时候,需要向服务器请求策略文件。
以下是一个简单的策略文件服务器的代码c++:
1 #include <Winsock2.h> 2 #include <stdio.h> 3 #pragma comment(lib,"WS2_32.lib") 4 #define _CRT_SECURE_NO_WARNINGS 5 void main() 6 { 7 WSADATA wsd; 8 SOCKET server; //服务器socket 9 SOCKADDR_IN addrSrv; 10 SOCKADDR_IN addrClient; 11 SOCKET client; //连接的客户端socket 12 char szPolicy[1024] = ""; 13 strcat_s(szPolicy, 1024, "<?xml version='1.0'?>"); 14 strcat_s(szPolicy, 1024, "<cross-domain-policy>"); 15 strcat_s(szPolicy, 1024, "<site-control permitted-cross-domain-policies='all'/>"); 16 strcat_s(szPolicy, 1024, "<allow-access-from domain='*' to-ports='*'/>"); 17 strcat_s(szPolicy, 1024, "</cross-domain-policy>"); 18 int len; 19 if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0) 20 { 21 printf("start up failed! "); 22 return; 23 } 24 server = socket(AF_INET, SOCK_STREAM, 0); //创建socket 25 addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY); //设置地址 26 addrSrv.sin_family = AF_INET; 27 addrSrv.sin_port = htons(843); //设置端口号 28 bind(server, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR)); //绑定 29 printf("flash策略文件服务器已启动! "); 30 31 listen(server, 5); //设置最多连接数 32 int count = 0; 33 len = sizeof(SOCKADDR); 34 while (1) 35 { 36 client = accept(server, (SOCKADDR*)&addrClient, &len); //接收客户端连接 37 char buff[1024] = ""; 38 recv(client, buff, 1024, 0); 39 szPolicy[strlen(szPolicy)] = '