bind 10049

场景:winsock bind 异常代号10049

winsock bind 错误代号10049
//服务器进程

#include <stdlib.h>
#include <stdio.h>
#include <WinSock2.h>

#pragma comment(lib,"Ws2_32")

#define PORT 7000

int main()
{
  WORD wVersionRequested;
  WSADATA wsaData;
  int err;

  wVersionRequested = MAKEWORD( 2, 2 );

  err = WSAStartup( wVersionRequested, &wsaData );
  if ( err != 0 ) {
  /* Tell the user that we could not find a usable */
  /* WinSock DLL. */
  return 1;
  }

  /* Confirm that the WinSock DLL supports 2.2.*/
  /* Note that if the DLL supports versions greater */
  /* than 2.2 in addition to 2.2, it will still return */
  /* 2.2 in wVersion since that is the version we */
  /* requested. */

  if ( LOBYTE( wsaData.wVersion ) != 2 ||
  HIBYTE( wsaData.wVersion ) != 2 ) {
  /* Tell the user that we could not find a usable */
  /* WinSock DLL. */
  WSACleanup( );
  return 1;
  }

  /* The WinSock DLL is acceptable. Proceed. */

  SOCKET sock_listen = socket(AF_INET,SOCK_STREAM,0);

  SOCKADDR_IN addr_server;
  addr_server.sin_addr.S_un.S_addr = inet_addr(INADDR_ANY);
  addr_server.sin_family = AF_INET;
  addr_server.sin_port = htons(PORT);

  int ret = 0;
  int error = 0;

  ret = bind(sock_listen,(LPSOCKADDR)&addr_server,sizeof(addr_server));
  if (ret==SOCKET_ERROR)
  {
#ifdef _DEBUG
  printf("Bind error:%d\n",(error=WSAGetLastError()));
#endif
  return 0;
  }

  ret = listen(sock_listen,5);
  if (ret==SOCKET_ERROR)
  {
#ifdef _DEBUG
  printf("Listen error:%d\n",(error=WSAGetLastError()));
#endif
  return 0;
  }

  int sock_len = sizeof(SOCKADDR);
  SOCKET sock_client = accept(sock_listen,(LPSOCKADDR)&addr_server,&sock_len);
  if (sock_client==SOCKET_ERROR)
  {
#ifdef _DEBUG
  printf("Accept error:%d\n",(error=WSAGetLastError()));
#endif
  return 0;
  }

  char recvBuf[100]={""};
  char tempBuf[200]={""};


  ret = recv(sock_client,recvBuf,strlen(recvBuf)+1,0);
  if (ret==SOCKET_ERROR)
  {
#ifdef _DEBUG
  printf("Recv error:%d\n",(error=WSAGetLastError()));
#endif
  return 0;
  }

  sprintf_s(tempBuf,strlen(tempBuf)+1,"%s",recvBuf);

  printf("%s\n",recvBuf);

  shutdown(sock_listen,0);

  closesocket(sock_listen);
  closesocket(sock_client);

  WSACleanup();

  return 0;

}

//客户进程
#include <stdlib.h>
#include <stdio.h>
#include <WinSock2.h>

#pragma comment(lib,"Ws2_32")

#define PORT 7000

int main()
{
  WORD wVersionRequested;
  WSADATA wsaData;
  int err;

  wVersionRequested = MAKEWORD( 2, 2 );

  err = WSAStartup( wVersionRequested, &wsaData );
  if ( err != 0 ) {
  /* Tell the user that we could not find a usable */
  /* WinSock DLL. */
  return 1;
  }

  /* Confirm that the WinSock DLL supports 2.2.*/
  /* Note that if the DLL supports versions greater */
  /* than 2.2 in addition to 2.2, it will still return */
  /* 2.2 in wVersion since that is the version we */
  /* requested. */

  if ( LOBYTE( wsaData.wVersion ) != 2 ||
  HIBYTE( wsaData.wVersion ) != 2 ) {
  /* Tell the user that we could not find a usable */
  /* WinSock DLL. */
  WSACleanup( );
  return 1;
  }

  /* The WinSock DLL is acceptable. Proceed. */

  SOCKET sock_client = socket(AF_INET,SOCK_STREAM,0);

  SOCKADDR_IN addr_client;
  addr_client.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
  addr_client.sin_family = AF_INET;
  addr_client.sin_port = htons(PORT);

  int ret = 0;
  int error = 0;
  ret = connect(sock_client,(LPSOCKADDR)&addr_client,sizeof(addr_client));
  if (ret==SOCKET_ERROR)
  {
#ifdef _DEBUG
  printf("Connect error:%d\n",(error=WSAGetLastError()));
#endif
  return 0;
  }

  char sendBuf[100]={"my name is xxxxx"};

  ret = send(sock_client,sendBuf,strlen(sendBuf),0);
  if (ret==SOCKET_ERROR)
  {
#ifdef _DEBUG
  printf("Send error:%d\n",(error=WSAGetLastError()));
#endif
  return 0;
  }

  shutdown(sock_client,0);

  closesocket(sock_client);

  WSACleanup();
   

  return 0;
}

服务器进程启动时,返回错误时10049,我刚开始学网络编程,网上找了很久,问题还是没解决,望知道得人看看,解答疑惑,感谢

------解决方案--------------------
addr_server.sin_addr.S_un.S_addr = htonl(INADDR_ANY); //可以不要htonl
----------
分析inet_addr(INADDR_ANY)
因为INADDR_ANY恰好等于0x00000000,相当于NULL,不然可能会报内存不能read之类的
而inet_addr(NULL)将返回0xFFFFFFFF(255.255.255.255)

之前见到过这种写法,
不知LZ是哪抄的代码?
------解决方案--------------------
Cannot assign requested address. 
就是地址不正确
1楼的解法正确
------解决方案--------------------
有错误码就行了,网上查下错误码的意思。