Socket实现C++与Flex通信
<?xml version="1.0" encoding="utf-8"?><mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="initApp()"><mx:Script> <![CDATA[ import flash.events.SecurityErrorEvent; import flash.events.IOErrorEvent; import flash.events.ProgressEvent; import flash.net.Socket; import flash.utils.ByteArray; private var socket:Socket = new Socket(); internal function initApp():void { socket.addEventListener(Event.CLOSE,closeHandler); socket.addEventListener(Event.CONNECT,connectHandler); socket.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler); socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityErrorHandler); socket.addEventListener(ProgressEvent.SOCKET_DATA,socketDataHandler); } internal function closeHandler(evt:Event):void { output("connect fail and close"); } internal function connectHandler(evt:Event):void { output("connect success"); } internal function ioErrorHandler(evt:IOErrorEvent):void { output("io error:"+evt.text); } internal function securityErrorHandler(evt:SecurityErrorEvent):void { output("security error :"+evt.text); } internal function doConnect():void { var server:String = server_txt.text; var port:Number =Number(port_txt.text); socket.connect(server,port); } internal function socketDataHandler(evt:ProgressEvent):void { var msg:String; while(socket.bytesAvailable) msg+=socket.readMultiByte(socket.bytesAvailable,"utf8"); var arr:Array = msg.split('\n'); for(var i:int=0;i<arr.length;i++) { if(arr[i].length>1) { var myPattern:RegExp=/\r/; arr[i]=arr[i].replace(myPattern,''); output(arr[i]); } } } internal function sendMessage(msg:String):void { var message:ByteArray = new ByteArray(); message.writeUTFBytes(msg+"\r\n"); socket.writeBytes(message); socket.flush(); } internal function output(msg:String):void { msg=msg+"\n"; output_txt.text+=msg; } ]]></mx:Script><mx:Panel x="368" y="25" width="360" height="336" layout="absolute" title="输出信息"> <mx:TextArea x="10" y="77" width="100%" height="213" id="output_txt"/> <mx:TextInput x="23" y="37" id="input_txt" text="输入信息" enter="sendMessage(input_txt.text)"/> <mx:Button x="246" y="37" label="发送" click="sendMessage(input_txt.text)"/> </mx:Panel> <mx:Panel x="40" y="78" width="250" height="200" layout="absolute" title="登陆窗口"> <mx:Label x="10" y="10" text="服务器地址"/> <mx:TextInput x="73" y="8" width="147" id="server_txt" text="127.0.0.1"/> <mx:Label x="10" y="51" text="端口"/> <mx:TextInput x="73" y="49" width="147" id="port_txt" text="80"/> <mx:Button x="73" y="109" label="连接" click="doConnect()"/> </mx:Panel></mx:Application>
?
下面是c++服务器端:
// server.cpp : 定义控制台应用程序的入口点。//#include "stdafx.h"#include <winsock2.h>#include <windows.h>#include <iostream>using namespace std;#pragma comment(lib,"ws2_32.lib") void main(){ WORD wVersionRequested; WSADATA wsaData; int err; short port=80;//端口号 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);//创建套接字 SOCKET sockConn;//用来和客户端通信的套接字 SOCKADDR_IN addrSrv;//用来和客户端通信的套接字地址 addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY); addrSrv.sin_family=AF_INET; addrSrv.sin_port=htons(port); bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));//绑定端口 listen(sockSrv,5);//侦听 printf("Server %d is listening......\n",port); SOCKADDR_IN addrClient; int len=sizeof(SOCKADDR); char buf[4096];//接收的数据char rbuf[100]="成功";//返回的数据 while(1) { //接受连接 sockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len); printf("Accept connection from %s\n",inet_ntoa(addrClient.sin_addr)); //接收数据 int bytes; if((bytes=recv(sockConn,buf,sizeof(buf),0))==SOCKET_ERROR){ printf("接收数据失败!\n"); exit(-1); } buf[bytes]='\0'; printf("Message from %s: %s\n",inet_ntoa(addrClient.sin_addr),buf); //发送数据 if(send(sockConn,rbuf,strlen(rbuf)+1,0)==SOCKET_ERROR){ printf("发送数据失败!"); exit(-1); } printf("Message to %s: %s\n",inet_ntoa(addrClient.sin_addr),rbuf); //清理套接字占用的资源 closesocket(sockConn); }}
?