Windows 网络通讯开发
一、Windows网络开发API
由于C++标准库中没有网络库,所以进行网络开发的时候要调用系统API。Windows通讯开发API包括以下几个基本函数及成员类型:
1. Header:Winsock.h 和 Winsock2.h(最新的)
具体Winsock2.h包含两大块内容:Socket Functions和Microsoft Windows-Specific Extension Functions
Socket Functions:
- socket:An incoming connection is acknowledged and associated with an immediately created socket. The original socket is returned to the listening state.
SOCKET socket(int af,int type,int protocol);
- bind:Assigns a local name to an unnamed socket.
- listen:Listens for incoming connections on a specified socket.(一般在服务器端使用)
- accept:An incoming connection is acknowledged(被确认) and associated with an immediately created socket. The original socket is returned to the listening state.(一般在服务器端使用)
- connect:Initiates a connection on the specified socket.(一般在客户端使用)
- send:Sends data to a connected socket.
- sendto:Sends data to either a connected or unconnected socket.
- recv:Receives data from a connected or unconnected socket.
- recvfrom:Receives data from either a connected or unconnected socket.
- htonl/htons/ntohl/ntohs:
h代表:host-byte;n代表:network-byte;l代表:32-bit;s代表:16-bit; - inet_addr:Converts a character string representing a number in the Internet standard ".'' notation to an Internet address value.
- inet-ntoa:Converts an Internet address value to an ASCII string in ".'' notation that is, "a.b.c.d''.
Microsoft Windows-Specific Extension
2. Library:Use Ws2_32.lib.
#pragma comment(lib, "ws2_32.lib")
3. 使用方法举例:
#include "stdafx.h"
#include <Winsock2.h>
#include <iostream>
using namespace std;
const int PORT = 10051;
int main(int argc, char* argv[])
{
WSADATA wd;
WSAStartup(0x202,&wd);
SOCKET sock = socket(AF_INET,SOCK_STREAM,0);//侦听套接字
if(sock == INVALID_SOCKET)
{
cout << "socket函数失败:" << WSAGetLastError() << endl;
return -1;
}
sockaddr_in sa = {AF_INET,htons(PORT)};
int n = bind(sock,(sockaddr*)&sa,sizeof(sa));
if(n == SOCKET_ERROR)
{
cout << "bind函数失败:" << WSAGetLastError() << endl;
return -1;
}
listen(sock,5);
int nLen = sizeof(sa);
SOCKET socka = accept(sock,(sockaddr*)&sa,&nLen);//如何循环等待多个客户端同时连接进来
if(socka==INVALID_SOCKET)
{
cout << "accept函数失败:" << WSAGetLastError() << endl;
return -1;
}
cout << "有人连接进来:" << inet_ntoa(sa.sin_addr) << "-" << htons (sa.sin_port)<< endl;
char s[256];
while(( n = recv(socka,s,sizeof(s)-1,0)) > 0)
{
s[n] = 0;
cout << s << endl;
}
cout << WSAGetLastError() << endl;
return 0;
}
详情参见:MSDN October 2001->Platform SDK: Windows Sockets
二、CAsyncSocket Class
- 定义:
Class CAsyncSocket encapsulates the Windows Socket Functions API, providing an object-oriented abstraction for programmers who want to use Windows Sockets in conjunction with MFC.
这个类封装了Window 套接字API,是比较底层的通讯类。 - 主要通讯流程:
To use a CAsyncSocket object, call its constructor, then call the Create function to create the underlying socket handle (type SOCKET), except on accepted sockets. For a server socket call the Listen member function, and for a client socket call the Connect member function. The server socket should call the Accept function upon receiving a connection request. Use the remaining CAsyncSocket functions to carry out communications between sockets. Upon completion, destroy the CAsyncSocket object if it was created on the heap; the destructor automatically calls the Close function. The SOCKET data type is described in the article Windows Sockets: Background.
详细使用情况参见Microsoft Help Veiwer 2.0
三、CSocket Class
Derives from CAsyncSocket and inherits its encapsulation of the Represents a higher level of abstraction of the Windows Sockets API than that of a CAsyncSocket object.
这个类是继承CAsyncSocket Class 的,它使用比其父类更加便捷。
详细使用情况参见Microsoft Help Veiwer 2.0