支持HW团队,就支付宝领取下面的红包吧!(2018年3月31前,就几毛,也会几块,可以和其他红包叠加使用),你领取消费,HW有奖励。红包使用无条件限制,有条件请注意是不是有病毒。

小伙伴们,给大家发红包喽!人人可领,领完就能用。祝大家领取的红包金额大大大!#吱口令#长按复制此消息,打开支付宝就能领取!er1OEj73Uj

登入 注册 | 验证
| 搜索
HelloWorld论坛 : > 3D虚拟与实时渲染> 特别奉献> flex入门与研究> [转]利用Socket实现C++与Flex通信
 
 
 
 
 
 
 
 
 
 
类别:其他 阅读:3680 评论:0 时间:三月 19, 2012, 6:04 a.m. 关键字:

 

 来源:

http://c.chinaitlab.com/ccjq/801820.html

 

 一、C++服务器

  (用的是Visual Studio 2008(05,03的应该也可以,VC++的可能需要稍微改动))

  代码如下:

  #include <winsock2.h>

  #include <stdio.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=1300;//端口号

  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);

  }

  }

  运行效果如下:

 

二、Flex客户端(Flex Builder)

  代码如下

  <?xml version="1.0" encoding="utf-8"?>

  <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"

  creationComplete="initApp();">

  <mx: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",1300);

  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,"GB2312");

  socket.flush();

  }

  private function socketDataHandler(event:ProgressEvent):void

  {

  trace("socketDataHandler: " + event);

  var str:String=socket.readMultiByte(socket.bytesAvailable,"GB2312");

  ReceiveStr.text = str;

  }

  ]]>

  </mx:Script>

  <mx:Text text="发送" y="11" x="18" fontWeight="bold" color="#ffffff"/>

  <mx:TextAreaFONT-SIZE: 10pt; COLOR: #990000; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt">ReceiveStr" x="53" y="68"/>

  <mx:Text text="接收" y="69" x="18" fontWeight="bold" color="#ffffff"/>

  <mx:TextAreaFONT-SIZE: 10pt; COLOR: #990000; FONT-FAMILY: 'Courier New'; mso-font-kerning: 0pt">SendStr" y="10" x="53"/>

  <mx:Button label="Start" click="StartTest()" x="53" y="120"/>

  <mx:Button label="Close" click="CloseTest()" x="126" y="120"/>

  </mx:WindowedApplication>

  三、运行

  先运行C++服务器端,再运行Flex客户端,成功后如下图所示

 

[挂载人]初学MPEG [审核人]初学MPEG 推荐

个人签名--------------------------------------------------------------------------------

Please Login (or Sign Up) to leave a comment