zoukankan      html  css  js  c++  java
  • winsock编程学习1

    WSA prefix

    WSAStarup()

    WSACleanup()

    WSARecvEx()

    WSAGetLastError()

    How to Add  Winsock link library WS2_32.lib to the Visual C++ project?

    enter :Visual .NET doc 1 and Visual .NET doc 2 articles

    Initializing  Winsock

    int WSAStarup( WORD  wVersionRequested, LPWSADATA  lpWSAData );

    typedef struct WSAData

    {

       WORD  wVersion;

       WORD  wHighVersion;

       char  szDescription[WSADESCRIPTION_LEN+1];

       char  szSystemStatus[WSASYS_STATUS_LEN+1];

       unsigned short iMaxSockets;

       unsigned short iMaxUdpDg;

      char FAR *lpVendorInfo;

    } WSADATA,*LPWSADATA;

    To find the maximum number of concurrent sockets,you should query the protocol information through WSAEnumProtocols()

    int WSACleanup(void)

    Error Checking and Handling

    int WSAGetLastError(void)

    code:

    #include<stdio.h>
    #include <winsock2.h>
    #include <mswsock.h>
    int main(void)
    {
        WSADATA wsaData;
        int RetCode;
        //initialize Winsock version 2.2
        if((RetCode=WSAStartup(MAKEWORD(2,2),&wsaData))!=0)
        {
            printf( "WSAStartup failed with error %d\n",RetCode);
            return 1;
        }
        else
        {
            printf("The Winsock dll found!\n");
            printf("The current status is:%s.n",wsaData.szSystemStatus);
        }
        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));
            //when your application is finished call WSACLeanup
            WSACleanup();

            //and exit
            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));
            //When your application is finished call WSACleanup
            if(WSACleanup()==SOCKET_ERROR)
                printf("WSACleanup failed with error%d\n",WSAGetLastError());
            //and exit
            return 1;
        }
    }

    编译时候,出现一些错误是因为写错,

    程序执行结果:

    image

    Well,after completing this exercise ,you should be familiar with the steps to create an empty Win32 console application project.

    Those steps will be repeated fot almost all the Winsock2 projects in this tutorial .

    …………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………….

    1 Tutorials on 'Advanced' Winsock 2 Network Programming

    C and Winsock2 Topics

    net-gold

    4 VC中Socket初始化以及释放,WSAStartup函数,WSACleanup函数

    5 VC++网络编程

    6 c++ 初识winsock 基本操作---创建

    ……………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………..

  • 相关阅读:
    Django 常见问题
    post和get的区别
    Django 基础学习笔记二
    Django 中的分页器
    Python 微服务框架 Nameko 微服务通信(RabbitMQ)
    《大数据白皮书 2020.12》解读
    练习Div+Css
    利用JAVAScript调用WebService
    统计在线人数和历史访问人数
    自己写的一个DBHelper
  • 原文地址:https://www.cnblogs.com/fleetwgx/p/1539083.html
Copyright © 2011-2022 走看看