#include
#pragma comment(lib, "ws2_32.lib")
int WSAStartup(
__in WORD wVersionRequested,
__out LPWSADATA lpWSAData
);
wVersionRequested
调用程序使用windows socket的最高版本。 高字节指定小的版本号,低字节指定高的版本号。
lpWSAData
指向WSADATA数据结构体指针,接收Windows Socket的实现细节。
返回值
如果成功,WSAStartup函数返回0。否则,返回下面列表显示的错误码之一。
WSAStartup函数直接在返回值中返回扩展的错误码,不再需要调用WSAGetLastError函数。
错误码 | 解释 |
WSASYSNOTREADY | 网络通信中下层的网络子系统没准备好 |
WSAVERNOTSUPPORTED | Socket实现提供版本和socket需要的版本不符 |
WSAEINPROGRESS | 一个阻塞的Socket操作正在进行 |
WSAEPROCLIM | Socket的实现超过Socket支持的任务数限制 |
WSAEFAULT | lpWSAData参数不是一个合法的指针 |
WORD MAKEWORD(
BYTE bLow, //指定新变量的低字节序;
BYTE bHigh //指定新变量的高字节序;
);
MAKEWORD 宏 平台:SDK这个宏创建一个无符号16位整型,通过连接两个给定的无符号参数。(注:typedef unsigned short WORD;)
宏的原始定义:
#define MAKEWORD(a, b) ((WORD)(((BYTE)(((DWORD_PTR)(a)) & 0xff)) | ((WORD)((BYTE)(((DWORD_PTR)(b)) & 0xff))) << 8))
返回值:一个无符号16位整形数。
BOOL WinSockInit()
{
WSADATA wsaData = {0};
if (WSAStartup(MAKEWORD(2, 2), &wsaData) == NO_ERROR) {
return TRUE;
}else{
return FALSE;
}
}
void WinSockUnLoad()
{
WSACleanup();
}
SOCKET WSAAPI socket(
_In_ int af, //地址描述
_In_ int type, //套接字类型
_In_ int protocol //协议类型
);
domain:协议族,
常用的有AF_INET、AF_INET6、AF_LOCAL、AF_ROUTE其中AF_INET代表使用ipv4地址
AF_INET,指定so_pcb中的地址要采用ipv4地址类型
AF_INET6,指定so_pcb中的地址要采用ipv6的地址类型
AF_LOCAL/AF_UNIX,指定so_pcb中的地址要使用绝对路径名
type:
socket类型,常用的socket类型有,SOCK_STREAM、SOCK_DGRAM、SOCK_RAW、SOCK_PACKET、SOCK_SEQPACKET等
SOCK_STREAM是基于TCP的,数据传输比较有保障。SOCK_DGRAM是基于UDP的,专门用于局域网,基于广播SOCK_STREAM 是数据流,一般是tcp/ip协议的编程,SOCK_DGRAM分是数据抱,是udp协议网络编程
protocol:协议。
常用的协议有,IPPROTO_TCP、IPPTOTO_UDP、IPPROTO_SCTP、IPPROTO_TIPC等
IPPROTO_TCP,TCP协议
IPPROTO_UDP,UPD协议
0,如果指定为0,表示由内核根据so_type指定默认的通信协议
成功时,返回非负数的socket描述符;失败是返回-1。socket描述符是一个指向内部数据结构的指针
使用:
//创建套接字
SOCKET serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket == INVALID_SOCKET)
{
cout << "serverSocket = socket(AF_INET, SOCK_STREAM, 0) execute failed!" << endl;
return -1;
}
int bind(
_In_ SOCKET s, //套接字句柄
_In_ const struct sockaddr *name, //要关联的本地地址
_In_ int namelen //地址的长度
);
使用:
//初始化服务器地址族变量
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(6000); //端口号
//绑定
iRet = bind(serverSocket, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
if (iRet == SOCKET_ERROR)
{
cout << "bind(serverSocket, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR)) execute failed!" << endl;
return -1;
}
int listen(
_In_ SOCKET s, //套接字句柄
_In_ int backlog //监听队列中允许保持的尚未处理的最大连接数量
);
使用:
//监听
iRet = listen(serverSocket, CONNECT_NUM_MAX);
if (iRet == SOCKET_ERROR)
{
cout << "listen(serverSocket, 10) execute failed!" << endl;
return -1;
}
SOCKET accept(
_In_ SOCKET s, //套接字句柄
_Out_ struct sockaddr *addr, //一个指向 sockaddr_in 结构的指针,用于取得对方的地址信息
_Inout_ int *addrlen //一个指向地址长度的指针
);
使用:
//等待连接_接收_发送
SOCKADDR_IN clientAddr;
int len = sizeof(SOCKADDR);
SOCKET connSocket = accept(serverSocket, (SOCKADDR*)&clientAddr, &len);
if (connSocket == INVALID_SOCKET)
{
cout << "accept(serverSocket, (SOCKADDR*)&clientAddr, &len) execute failed!" << endl;
return -1;
}
#include
#include
#pragma comment(lib, "ws2_32.lib")
#define CONNECT_NUM_MAX 10
using namespace std;
int main()
{
//加载套接字库
WSADATA wsaData;
int iRet = 0;
iRet = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iRet != 0)
{
cout << "WSAStartup(MAKEWORD(2, 2), &wsaData) execute failed!" << endl;;
return -1;
}
if (2 != LOBYTE(wsaData.wVersion) || 2 != HIBYTE(wsaData.wVersion))
{
WSACleanup();
cout << "WSADATA version is not correct!" << endl;
return -1;
}
//创建套接字
SOCKET serverSocket = socket(AF_INET, SOCK_STREAM, 0);
if (serverSocket == INVALID_SOCKET)
{
cout << "serverSocket = socket(AF_INET, SOCK_STREAM, 0) execute failed!" << endl;
return -1;
}
//初始化服务器地址族变量
SOCKADDR_IN addrSrv;
addrSrv.sin_addr.S_un.S_addr = htonl(INADDR_ANY);
addrSrv.sin_family = AF_INET;
addrSrv.sin_port = htons(6000);
//绑定
iRet = bind(serverSocket, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR));
if (iRet == SOCKET_ERROR)
{
cout << "bind(serverSocket, (SOCKADDR*)&addrSrv, sizeof(SOCKADDR)) execute failed!" << endl;
return -1;
}
//监听
iRet = listen(serverSocket, CONNECT_NUM_MAX);
if (iRet == SOCKET_ERROR)
{
cout << "listen(serverSocket, 10) execute failed!" << endl;
return -1;
}
//等待连接_接收_发送
SOCKADDR_IN clientAddr;
int len = sizeof(SOCKADDR);
while (1)
{
SOCKET connSocket = accept(serverSocket, (SOCKADDR*)&clientAddr, &len);
if (connSocket == INVALID_SOCKET)
{
cout << "accept(serverSocket, (SOCKADDR*)&clientAddr, &len) execute failed!" << endl;
return -1;
}
char sendBuf[100];
sprintf_s(sendBuf, "Welcome %s", inet_ntoa(clientAddr.sin_addr));
send(connSocket, sendBuf, strlen(sendBuf)+1, 0);
char recvBuf[100];
recv(connSocket, recvBuf, 100, 0);
printf("%s\n", recvBuf);
closesocket(connSocket);
}
system("pause");
return 0;
}
#include
#include
using namespace std;
#pragma comment(lib, "ws2_32.lib")
int main()
{
//加载套接字库
WSADATA wsaData;
int iRet =0;
iRet = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iRet != 0)
{
cout << "WSAStartup(MAKEWORD(2, 2), &wsaData) execute failed!" << endl;
return -1;
}
if (2 != LOBYTE(wsaData.wVersion) || 2 != HIBYTE(wsaData.wVersion))
{
WSACleanup();
cout << "WSADATA version is not correct!" << endl;
return -1;
}
//创建套接字
SOCKET clientSocket = socket(AF_INET, SOCK_STREAM, 0);
if (clientSocket == INVALID_SOCKET)
{
cout << "clientSocket = socket(AF_INET, SOCK_STREAM, 0) execute failed!" << endl;
return -1;
}
//初始化服务器端地址族变量
SOCKADDR_IN srvAddr;
srvAddr.sin_addr.S_un.S_addr = inet_addr("127.0.0.1");
srvAddr.sin_family = AF_INET;
srvAddr.sin_port = htons(6000);
//连接服务器
iRet = connect(clientSocket, (SOCKADDR*)&srvAddr, sizeof(SOCKADDR));
if (0 != iRet)
{
cout << "connect(clientSocket, (SOCKADDR*)&srvAddr, sizeof(SOCKADDR)) execute failed!" << endl;
return -1;
}
//接收消息
char recvBuf[100];
recv(clientSocket, recvBuf, 100, 0);
printf("%s\n", recvBuf);
//发送消息
char sendBuf[100];
sprintf_s(sendBuf, "Hello, This is client %s", "兔子");
send(clientSocket, sendBuf, strlen(sendBuf)+1, 0);
//清理
closesocket(clientSocket);
WSACleanup();
system("pause");
return 0;
}