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

 

 

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

转载于:https://www.cnblogs.com/fleetwgx/archive/2009/08/04/1539083.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值