再谈Winsock网络编程入门

来源:互联网 发布:送闺蜜新年礼物 知乎 编辑:程序博客网 时间:2024/06/05 07:45

虽然自己接触网络编程已经很多年,但初学者还是需要入门的,这里谈谈我的在编写Windows网络应用程序的时候,最常用的便是Winsock接口,注意它不是网络协议,你可以理解它为网络应用API。

 

Winsock简介

 

Windows下网络编程的规范-Windows Sockets是Windows下得到广泛应用的、开放的、支持多种协议的网络编程接口。从1991年的1.0版到1995年的2.0.8版,经过不断完善并在Intel、Microsoft、Sun、SGI、Informix、Novell等公司的全力支持下,已成为Windows网络编程的事实上的标准。

实际上Windows95以后的各个windows版本都支持Winsock2.2版本,但是Window CE 只支持1.1版本。Winsock2.2提供了更多的功能和API函数,而且区分两个版本的函数非常容易,2.2版本的API函数都以WSA开头的,除了WSAStartup, WSACleanup, WSARecvEx, 和 WSAGetLastError这几个函数也出现在1.1版本中。

微软Winsock 2官方介绍:
Windows Sockets 2 (Winsock) enables programmers to create advanced Internet, intranet, and other network-capable applications to transmit application data across the wire, independent of the network protocol being used. With Winsock, programmers are provided access to advanced Microsoft® Windows® networking capabilities such as multicast and Quality of Service (QOS).

Winsock follows the Windows Open System Architecture (WOSA) model; it defines a standard service provider interface (SPI) between the application programming interface (API), with its exported functions and the protocol stacks. It uses the sockets paradigm that was first popularized by Berkeley Software Distribution (BSD) UNIX. It was later adapted for Windows in Windows Sockets 1.1, with which Windows Sockets 2 applications are backward compatible. Winsock programming previously centered around TCP/IP. Some programming practices that worked with TCP/IP do not work with every protocol. As a result, the Windows Sockets 2 API adds functions where necessary to handle several protocols.

Developer Audience

Windows Sockets 2 is designed for use by C/C++ programmers. Familiarity with Windows networking is required.

Run-Time Requirements

Windows Sockets 2 can be used on all Windows platforms. Where certain implementations or capabilities of Windows Sockets 2 platform restrictions do exist, they are clearly noted in the documentation.

In This Section
Topic Description
What's New for Windows Sockets
 Information on new features for Windows Sockets.
 
Network Protocol Support in Windows
 Information on network protocol support for Windows Sockets on different versions of Windows.
 
About Winsock
 General information on Windows Sockets programming considerations, architecture, and capabilities available to developers.
 
Using Winsock
 Procedures and programming techniques used with Windows Sockets. This section includes basic Winsock programming techniques, such as Getting Started With Winsock, as well as advanced techniques useful for experienced Winsock developers.
 
Winsock Reference
 Documentation of the Windows Sockets API.


这里给一个最简单的例子,方便大家学习。使用Winsock写应用程序所需要的头文件和类库有:

// 对于1.1版本
#include<winsock.h>
#pragma comment(lib, "wsock32.lib")
// 对于2.2版本
#include<winsock2.h>
#pragma comment(lib, "ws2_32.lib")
现在我们来看看一个通用的Winsock应用程序框架:
#include<winsock2.h>  // 头文件
#pragma comment(lib, "ws2_32.lib") // 库文件加载
void main(void)
{
   WSADATA wsaData;  // WSADATA 结构体主要包含了系统所支持的Winsock版本信息         
   
   // 初始化Winsock 2.2。使用WSAStartup函数,第一个参数是所要用的Winsock版本号
   // 第二个参数就是WSADATA结构体的指针。如果初始化成功则返回0
   // 要注意任何WinsockAPI函数都必须在初始化后使用,包括错误检查函数
   // WSAGetLastError (用于查看出错详细信息)

   if( WSAStartup( MAKEWORD(2,2), &wsaData) != 0 )
   {
      printf( "WSAStartup 无法初始化!");
      return;
   } 

    // winsock 应用代码

    // 最后应该做一些清除工作
    if( WSACleanup() == SOCKET_ERROR )
        printf( "WSACleanup 出错!");

}

 

这里需要说明的有两点:
1.WSADATA结构体:
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; 


wVersion 就是对应你所要的版本,比如1.2,高位字节对应2,低位字节对应1。
wHiVersion 是系统所支持的最高版本,如果初始化失败,这个参数仍然返回。


其他成员基本都用不到,所以不必关心,另外要学网络编程最好看卡飞鸽传书(http://www.freeeim.com/)的源码,里面涉及到许多网络编程的细节,很值得学习。