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 基本操作---创建

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

  • 相关阅读:
    python中的特殊函数__call__
    python的内存机制
    tf.train.Saver()-tensorflow中模型的保存及读取
    修改过的bug
    JQuery的attr 与 val区别及使用
    多线程处理同一个List测试dome
    synchronized 使用总结
    oracle 自定义函数
    第一天写博客,分享下学习oracle存储过程的过程
    SqlServer
  • 原文地址:https://www.cnblogs.com/fleetwgx/p/1539083.html
Copyright © 2011-2022 走看看