环境:xp sp3,vs2008 c++控制台,flex 4.6
一、C++服务器端
#include <stdio.h> #include <Winsock2.h> #pragma comment(lib, "ws2_32.lib") #define BUFFSIZE 1024 void main() { WORD wVersionRequested; WSADATA wsaData; int err; wVersionRequested = MAKEWORD( 1, 1 ); err = WSAStartup( wVersionRequested, &wsaData ); if ( err != 0 ) { return; } if ( LOBYTE( wsaData.wVersion ) != 1 || HIBYTE( wsaData.wVersion ) != 1 ) { WSACleanup( ); return; } SOCKET sockSrv=socket(AF_INET,SOCK_STREAM,0); SOCKADDR_IN addrSrv; addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY); addrSrv.sin_family=AF_INET; addrSrv.sin_port=htons(8190); bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));// 绑定端口 listen(sockSrv,5); SOCKADDR_IN addrClient;// 连接上的客户端ip地址 int len=sizeof(SOCKADDR); char rbuf[128]= "<cross-domain-policy> <allow-access-from domain=\"*\" to-ports=\"*\"/> </cross-domain-policy> ";//套接字策略文件 while(1) { SOCKET sockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len);// 接受客户端连接,获取客户端的ip地址 while(1) { char recvBuf[BUFFSIZE+1]; memset(recvBuf, 0, BUFFSIZE+1); int nr = recv(sockConn,recvBuf,BUFFSIZE,0);// 接受客户端消息 printf("%s\n",recvBuf); if(nr==0 || nr==SOCKET_ERROR) { DWORD err = GetLastError(); break; } if (0 == strcmp(recvBuf,"<policy-file-request/>")) { //发送数据 if(send(sockConn,rbuf,strlen(rbuf)+1,0)==SOCKET_ERROR) { printf("发送数据失败!"); } break; } else { send(sockConn,"hello flex",strlen("hello flex")+1,0); } } closesocket(sockConn); } }二、Flex客户端(Flex Builder)
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="initApp()"> <fx:Script> <![CDATA[ import flash.net.Socket; private var socket:Socket; private function initApp():void { } private function StartTest():void { socket = new Socket("127.0.0.1",8190); socket.addEventListener(Event.CONNECT, connectHandler); socket.addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler); } private function CloseTest():void { if(socket!=null && socket.connected) { trace("socket.connected:"+socket.connected); socket.close(); trace("socket.connected:"+socket.connected); } } private function connectHandler(event:Event):void { trace("connectHandler: " + event); socket.writeMultiByte(SendStr.text,"UTF-8"); socket.flush(); } private function socketDataHandler(event:ProgressEvent):void { trace("socketDataHandler: " + event); var str:String=socket.readMultiByte(socket.bytesAvailable,"GB2312"); ReceiveStr.text = str; } ]]> </fx:Script> <fx:Declarations> <!-- 将非可视元素(例如服务、值对象)放在此处 --> </fx:Declarations> <mx:Text text="发送" y="11" x="18" fontWeight="bold" color="#ffffff"/> <mx:TextArea id="ReceiveStr" x="53" y="68"/> <!-- FONT-SIZE: 10pt; COLOR: #990000; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"--> <mx:Text text="接收" y="69" x="18" fontWeight="bold" color="#ffffff"/> <mx:TextArea id="SendStr" y="10" x="53"/> <!--FONT-SIZE: 10pt; COLOR: #990000; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt"--> <mx:Button label="Start" click="StartTest()" x="53" y="120"/> <mx:Button label="Close" click="CloseTest()" x="126" y="120"/> </s:Application>参考:http://c.chinaitlab.com/ccjq/801820_2.html