关于UDP通信中的10045错误

 在做UDP通信的过程中,服务器端绑定IP和PORT:

 m_stLocalAddr.sin_family =      AF_INET;
 m_stLocalAddr.sin_addr.s_addr = htonl(INADDR_ANY); 

 m_stLocalAddr.sin_port =        htons( 6000);

 m_hSocket = socket( PF_INET, SOCK_DGRAM, 0 );

if (SOCKET_ERROR == bind (m_hSocket, (struct sockaddr *)(&m_stLocalAddr), sizeof (m_stLocalAddr)))
  return FALSE;

 

发送数据:

  SOCKADDR_IN m_stServiceAddr;
  m_stServiceAddr.sin_family = AF_INET;
  m_stServiceAddr.sin_port = htons( 6000 );

  long nIP = inet_addr("192.168.1.1");  
  m_stServiceAddr.sin_addr.S_un.S_addr = htonl(nIP);

 

接收数据

   nBufLen = recvfrom( m_hSocket, (char*)lpDataBuf, nBufLen, 0, (struct sockaddr*)(&stRemoteAddr),            &addr_size );

 

结果总是返回-1,socket错误为10054,

 

其实是自己的错误,

m_stServiceAddr.sin_addr.S_un.S_addr = htonl(nIP);

应该为

m_stServiceAddr.sin_addr.S_un.S_addr = nIP;

不细心啊!

你可能感兴趣的:(socket,struct,服务器)