[转]Socket实现C++与Flex通信_C/C++_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > C/C++ > [转]Socket实现C++与Flex通信

[转]Socket实现C++与Flex通信

 2013/11/1 21:19:01  hszhl  程序员俱乐部  我要评论(0)
  • 摘要:下面是在flex下的客户端:Java代码<?xmlversion="1.0"encoding="utf-8"?><mx:Applicationxmlns:mx="http://www.adobe.com/2006/mxml"layout="absolute"creationComplete="initApp()"><mx:Script><![CDATA[importflash.events.SecurityErrorEvent;importflash
  • 标签:实现 c++ socket Flex

下面是在flex下的客户端:

?

Java代码 复制代码?收藏代码class="star">spinner">
  1. <?xml?version="1.0"?encoding="utf-8"?>??
  2. <mx:Application?xmlns:mx="http://www.adobe.com/2006/mxml"?layout="absolute"?creationComplete="initApp()">??
  3. ??
  4. ??
  5. <mx:Script>??
  6. ?<![CDATA[??
  7. ??import?flash.events.SecurityErrorEvent;??
  8. ??import?flash.events.IOErrorEvent;??
  9. ??import?flash.events.ProgressEvent;??
  10. ????
  11. ??import?flash.net.Socket;??
  12. ????
  13. ??import?flash.utils.ByteArray;??
  14. ????
  15. ??private?var?socket:Socket?=?new?Socket();??
  16. ??internal?function?initApp():void??
  17. ??{??
  18. ???socket.addEventListener(Event.CLOSE,closeHandler);??
  19. ???socket.addEventListener(Event.CONNECT,connectHandler);??
  20. ???socket.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);??
  21. ???socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR,securityErrorHandler);??
  22. ???socket.addEventListener(ProgressEvent.SOCKET_DATA,socketDataHandler);??
  23. ??}??
  24. ????
  25. ??internal?function?closeHandler(evt:Event):void??
  26. ??{??
  27. ???output("connect?fail?and?close");??
  28. ??}??
  29. ????
  30. ??internal?function?connectHandler(evt:Event):void??
  31. ??{??
  32. ???output("connect?success");??
  33. ??}??
  34. ????
  35. ??internal?function?ioErrorHandler(evt:IOErrorEvent):void??
  36. ??{??
  37. ???output("io?error:"+evt.text);??
  38. ??}??
  39. ????
  40. ??internal?function?securityErrorHandler(evt:SecurityErrorEvent):void??
  41. ??{??
  42. ???output("security?error?:"+evt.text);??
  43. ??}??
  44. ????
  45. ??internal?function?doConnect():void??
  46. ??{??
  47. ???var?server:String?=?server_txt.text;??
  48. ???var?port:Number?=Number(port_txt.text);??
  49. ?????
  50. ???socket.connect(server,port);??
  51. ?????
  52. ??}??
  53. ????
  54. ??internal?function?socketDataHandler(evt:ProgressEvent):void??
  55. ??{??
  56. ???var?msg:String;??
  57. ???while(socket.bytesAvailable)??
  58. ???msg+=socket.readMultiByte(socket.bytesAvailable,"utf8");??
  59. ???var?arr:Array?=?msg.split('\n');??
  60. ???for(var?i:int=0;i<arr.length;i++)??
  61. ???{??
  62. ????if(arr[i].length>1)??
  63. ????{??
  64. ?????var?myPattern:RegExp=/\r/;??
  65. ?????arr[i]=arr[i].replace(myPattern,'');??
  66. ?????output(arr[i]);??
  67. ????}??
  68. ???}??
  69. ??}??
  70. ????
  71. ??internal?function?sendMessage(msg:String):void??
  72. ??{??
  73. ???var?message:ByteArray?=?new?ByteArray();??
  74. ???message.writeUTFBytes(msg+"\r\n");??
  75. ????socket.writeBytes(message);??
  76. ????socket.flush();??
  77. ??}??
  78. ????
  79. ??internal?function?output(msg:String):void??
  80. ??{??
  81. ???msg=msg+"\n";??
  82. ???output_txt.text+=msg;??
  83. ??}??
  84. ????
  85. ?]]>??
  86. </mx:Script>??
  87. <mx:Panel?x="368"?y="25"?width="360"?height="336"?layout="absolute"?title="输出信息">??
  88. ?<mx:TextArea?x="10"?y="77"?width="100%"?height="213"?id="output_txt"/>??
  89. ?<mx:TextInput?x="23"?y="37"?id="input_txt"?text="输入信息"?enter="sendMessage(input_txt.text)"/>??
  90. ?<mx:Button?x="246"?y="37"?label="发送"?click="sendMessage(input_txt.text)"/>??
  91. ???
  92. </mx:Panel>??
  93. ?<mx:Panel?x="40"?y="78"?width="250"?height="200"?layout="absolute"?title="登陆窗口">??
  94. ??<mx:Label?x="10"?y="10"?text="服务器地址"/>??
  95. ??<mx:TextInput?x="73"?y="8"?width="147"?id="server_txt"?text="127.0.0.1"/>??
  96. ??<mx:Label?x="10"?y="51"?text="端口"/>??
  97. ??<mx:TextInput?x="73"?y="49"?width="147"?id="port_txt"?text="80"/>??
  98. ??<mx:Button?x="73"?y="109"?label="连接"?click="doConnect()"/>??
  99. ?</mx:Panel>??
  100. </mx:Application>?

下面是c++服务器端:

Cpp代码 复制代码?收藏代码
  1. //?server.cpp?:?定义控制台应用程序的入口点。??
  2. //??
  3. ??
  4. #include?"stdafx.h"??
  5. ??
  6. ??
  7. #include?<winsock2.h>??
  8. ??
  9. ??
  10. #include?<windows.h>??
  11. ??
  12. #include?<iostream>??
  13. ??
  14. using?namespace?std;??
  15. ??
  16. #pragma?comment(lib,"ws2_32.lib")??
  17. ??
  18. ???
  19. ??
  20. void?main(){???
  21. ??
  22. ?WORD?wVersionRequested;??
  23. ??
  24. ?WSADATA?wsaData;??
  25. ??
  26. ?int?err;??
  27. ??
  28. ?short?port=80;//端口号??
  29. ??
  30. ???
  31. ??
  32. ?wVersionRequested?=?MAKEWORD(?1,?1?);??
  33. ??
  34. ???
  35. ??
  36. ?err?=?WSAStartup(?wVersionRequested,?&wsaData?);//初始化套接字??
  37. ??
  38. ?if?(?err?!=?0?)??
  39. ??
  40. ?{??
  41. ??
  42. ????return;??
  43. ??
  44. ?}??
  45. ??
  46. ???
  47. ??
  48. ?if?(?LOBYTE(?wsaData.wVersion?)?!=?1?||?HIBYTE(?wsaData.wVersion?)?!=?1?)??
  49. ??
  50. ?{??
  51. ??
  52. ????WSACleanup(?);??
  53. ??
  54. ????return;??
  55. ??
  56. ?}??
  57. ??
  58. ???
  59. ??
  60. ?SOCKET?sockSrv=socket(AF_INET,SOCK_STREAM,0);//创建套接字??
  61. ??
  62. ?SOCKET?sockConn;//用来和客户端通信的套接字??
  63. ??
  64. ???
  65. ??
  66. ?SOCKADDR_IN?addrSrv;//用来和客户端通信的套接字地址??
  67. ??
  68. ???
  69. ??
  70. ?addrSrv.sin_addr.S_un.S_addr=htonl(INADDR_ANY);??
  71. ??
  72. ?addrSrv.sin_family=AF_INET;??
  73. ??
  74. ?addrSrv.sin_port=htons(port);??
  75. ??
  76. ???
  77. ??
  78. ?bind(sockSrv,(SOCKADDR*)&addrSrv,sizeof(SOCKADDR));//绑定端口??
  79. ??
  80. ???
  81. ??
  82. ?listen(sockSrv,5);//侦听??
  83. ??
  84. ???
  85. ??
  86. printf("Server?%d?is?listening......\n",port);??
  87. ??
  88. ???
  89. ??
  90. ?SOCKADDR_IN?addrClient;??
  91. ??
  92. ???
  93. ??
  94. ?int?len=sizeof(SOCKADDR);??
  95. ??
  96. ???
  97. ??
  98. char?buf[4096];//接收的数据??
  99. ??
  100. char?rbuf[100]="成功";//返回的数据??
  101. ??
  102. ???
  103. ??
  104. ?while(1)??
  105. ??
  106. ?{??
  107. ??
  108. ????????//接受连接??
  109. ??
  110. ?????????sockConn=accept(sockSrv,(SOCKADDR*)&addrClient,&len);??
  111. ??
  112. ?????????printf("Accept?connection?from?%s\n",inet_ntoa(addrClient.sin_addr));??
  113. ??
  114. ???
  115. ??
  116. ?????????//接收数据??
  117. ??
  118. ?????????int?bytes;??
  119. ??
  120. ?????????if((bytes=recv(sockConn,buf,sizeof(buf),0))==SOCKET_ERROR){??
  121. ??
  122. ??????????????printf("接收数据失败!\n");??
  123. ??
  124. ??????????????exit(-1);??
  125. ??
  126. ?????????}??
  127. ??
  128. ?????????buf[bytes]='\0';??
  129. ??
  130. ?????????printf("Message?from?%s:?%s\n",inet_ntoa(addrClient.sin_addr),buf);??
  131. ??
  132. ???
  133. ??
  134. ?????????//发送数据??
  135. ??
  136. ????????if(send(sockConn,rbuf,strlen(rbuf)+1,0)==SOCKET_ERROR){??
  137. ??
  138. ??????????????printf("发送数据失败!");??
  139. ??
  140. ??????????????exit(-1);??
  141. ??
  142. ?????????}??
  143. ??
  144. ????????printf("Message?to?%s:?%s\n",inet_ntoa(addrClient.sin_addr),rbuf);??
  145. ??
  146. ???
  147. ??
  148. ????????//清理套接字占用的资源??
  149. ??
  150. ????????closesocket(sockConn);??
  151. ??
  152. ?}??
  153. ??
  154. } ?
发表评论
用户名: 匿名