C++ 网络编程之正确使用UDP广播及多播(2)

// 绑定套接字
 err = bind(connect_socket, (SOCKADDR*)&sin, sizeof(SOCKADDR));
 if(SOCKET_ERROR == err)
 {
  err = WSAGetLastError();
  printf("/"bind/" error! error code is %d/n", err);
  return -1;
 }

int nAddrLen = sizeof(SOCKADDR);
 char buff[MAX_BUF_LEN] = "";
 int nLoop = 0;
 while(1)
 {
  // 接收数据
  int nSendSize = recvfrom(connect_socket, buff, MAX_BUF_LEN, 0, (SOCKADDR*)&sin_from, &nAddrLen);
  if(SOCKET_ERROR == nSendSize)
  {
   err = WSAGetLastError();
   printf("/"recvfrom/" error! error code is %d/n", err);
   return -1;
  }
  buff[nSendSize] = '/0';
  printf("Recv: %s/n", buff);
 }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/129827c2460e7977ba73fd723374e731.html