2 succeed678 succeed678 于 2013.10.21 23:09 提问

C++socket数据接收处理问题

1.能够点了按钮之后能够一直接收发送过来的单行数据
2.能把接收的数据处理好了之后存到另外定义的数组或list中去
3.那个线程应该如何定义程序才能执行
 
SOCKET sHost;
  WSADATA wsd;
  SOCKET sServer;
  SOCKET sClient;
  SOCKADDR_IN addrServ;
  void CMyDlg::OnRev()
  {
  #define BUF_SZIE 64
  #pragma comment(lib, "ws2_32.lib")
  char buf[BUF_SZIE];
  int retVal;
  if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
  {
  printf("WSAStartup failed!\n");
  }
  sServer = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

  addrServ.sin_family = AF_INET;
  addrServ.sin_port = htons(3000);
  addrServ.sin_addr.s_addr = INADDR_ANY;

  retVal = bind(sServer, (LPSOCKADDR)&addrServ, sizeof(SOCKADDR_IN));
  retVal = listen(sServer, 10);

  sockaddr_in addrClient;
  int addrClientlen = sizeof(addrClient);
  sClient = accept(sServer,(sockaddr FAR*)&addrClient, &addrClientlen);

  ZeroMemory(buf, BUF_SZIE);
  
  while(true)
  {
  retVal = recv(sClient, buf, BUF_SZIE, 0);
  if (SOCKET_ERROR == retVal)
  {
  printf("send failed!\n");
  }
  else
  {
  ......
  }
  }
  
  closesocket(sClient);
  WSACleanup();
  }

求大神指点一二

  • List item
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!