vs2008配置winsock2的C编程环境

来自http://tunps.com/config-winsock2-c-programming-dev-on-vs2008

先建立一个win32 console application,

设定为“编译为c代码”:

点project>properties

点“Additional Dependencies”,手动输入“WS2_32.lib”。也可以直接在代码头部添加以下代码:

#pragma comment(lib,"ws2_32.lib")

然后写入测试代码:

#include <stdio.h>
#include <winsock2.h>
#pragma comment(lib,"ws2_32.lib")
int main()
 
{
      WORD wVersionRequested;
      WSADATA wsaData;
      int wsaerr;
      //////////Create a socket////////////////////////
      //Create a SOCKET object called m_socket.
      SOCKET      m_socket;
      // Using MAKEWORD macro, Winsock version request 2.2
      wVersionRequested = MAKEWORD(2, 2);
      wsaerr = WSAStartup(wVersionRequested, &wsaData);
      if (wsaerr != 0)
      {
            /* Tell the user that we could not find a usable */
        /* WinSock DLL.*/
            printf("The Winsock dll not found!\n");
            return 0;
      }
      else
      {
            printf("The Winsock dll found!\n");
            printf("The status: %s.\n", wsaData.szSystemStatus);
      }
      /* 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.*/
            printf("The dll do not support the Winsock version %u.%u!\n", LOBYTE(wsaData.wVersion),HIBYTE(wsaData.wVersion));
            WSACleanup();
            return 0;
      }
      else
      {
            printf("The dll supports the Winsock version %u.%u!\n", LOBYTE(wsaData.wVersion),HIBYTE(wsaData.wVersion));
            printf("The highest version this dll can support: %u.%u\n", LOBYTE(wsaData.wHighVersion), HIBYTE(wsaData.wHighVersion));
      }
      // Call the socket function and return its value to the m_socket variable.
      // For this application, use the Internet address family, streaming sockets, and
      // the TCP/IP protocol.
      // using AF_INET family, TCP socket type and protocol of the AF_INET - IPv4
      m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
      // Check for errors to ensure that the socket is a valid socket.
      if (m_socket == INVALID_SOCKET)
      {
            printf("Error at socket(): %ld\n", WSAGetLastError());
            WSACleanup();
            return 0;
      }
      else
      {
            printf("socket() is OK!\n");
      }
      return 0;
}

参考:http://www.tenouk.com/Winsock/visualstudio2008ncnwinsock2.html

About tunpishuang

just 4 fun·····
This entry was posted in 未分类 and tagged , , . Bookmark the permalink.

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>