windows socket api 封装
来源:互联网 发布:小说人物起名软件 编辑:程序博客网 时间:2024/05/13 04:50
#ifndef __WINSOCK_UTIL_H__#define __WINSOCK_UTIL_H__#include <WinSock2.h>#pragma comment(lib, "Ws2_32.lib")int WinSock_Startup(int nVerHigh, int nVerLow){WORD wVersionRequested;WSADATA wsaData;int nRetVal = -1;int nErrCode = -1;wVersionRequested = MAKEWORD(nVerLow, nVerHigh);nRetVal = WSAStartup(wVersionRequested, &wsaData);if (0 != nRetVal){nErrCode = WSAGetLastError();}//check if the dll version retu rned is 2.2if (LOBYTE(wsaData.wVersion) != nVerLow || HIBYTE(wsaData.wVersion) != nVerHigh){WSACleanup();nRetVal = -1;}return nRetVal;}//Socket type: SOCK_STREAM, SOCK_DGRAMSOCKET WinSock_Create(int nSockType){SOCKET sockConn = socket(AF_INET, nSockType, 0);// >0 - succeedif (INVALID_SOCKET == sockConn){//faildint nErrCode = WSAGetLastError();}return sockConn;}//if the nSrvPort is 0, windows sockets will select a port between 1024 and 5000int WinSock_Bind(SOCKET sockListen, int nSrvPort){ int nRetVal = -1;int nErrCode = -1;struct sockaddr_in servAddr;servAddr.sin_family = AF_INET;servAddr.sin_addr.s_addr = htonl(INADDR_ANY);servAddr.sin_port = nSrvPort;//0 - succeed, other - failnRetVal = bind(sockListen, (SOCKADDR*)&servAddr, sizeof(servAddr));if (SOCKET_ERROR == nRetVal){nErrCode = WSAGetLastError();}return nRetVal;}int WinSock_Listen(SOCKET sockListen){int nRetVal = -1;int nErrCode = -1;// max 5 client connections waiting in the queue at one time,// the sixth connection accurs ,return WSAECONNREFUSED to clientnRetVal = listen(sockListen, 5);if (SOCKET_ERROR == nRetVal){nErrCode = WSAGetLastError();}return nRetVal;}SOCKET WinSock_Accpet(SOCKET sockListen){int nErrCode = -1;SOCKET sockConn;struct sockaddr_in addrClient;int nAddrClientLen = sizeof(addrClient);sockConn = accept(sockListen, (SOCKADDR*)&addrClient, &nAddrClientLen);if (INVALID_SOCKET == sockConn){nErrCode = WSAGetLastError();}return sockConn;}int WinSock_Connect(SOCKET sockInit, const char *pcServIP, unsigned short usServPort){int nRetVal = -1;int nErrCode = -1;//u_long ulServIP;SOCKADDR_IN addrServ;addrServ.sin_family = AF_INET;//servAddr.sin_addr.S_un.S_addr = htonl(ulServIP);addrServ.sin_addr.s_addr = inet_addr(pcServIP);addrServ.sin_port = htons(usServPort);int nServAddrLen = sizeof(addrServ);nRetVal = connect(sockInit, (SOCKADDR*)&addrServ, nServAddrLen);if (SOCKET_ERROR == nRetVal){nErrCode = WSAGetLastError(); }return nRetVal;}int WinSock_Send(SOCKET sockConn, char *pcSendBuf, int nSendLen){int nRetVal = -1;int nErrCode = -1;if (INVALID_SOCKET == sockConn){return nErrCode;}nRetVal= send(sockConn, pcSendBuf, nSendLen, 0);if (SOCKET_ERROR == nRetVal ){nErrCode = WSAGetLastError();}return nRetVal;}int WinSock_Sendto(SOCKET sockConn, char *pcSendBuf, int nSendLen, const SOCKADDR* pToAddr, int nToAddrLen){int nRetVal = -1;int nErrCode = -1;if (INVALID_SOCKET == sockConn){return nErrCode;}nRetVal = sendto(sockConn, pcSendBuf, nSendLen, 0, pToAddr, nToAddrLen);if (SOCKET_ERROR == nRetVal ){nErrCode = WSAGetLastError();}return nRetVal;}int WinSock_Recv(SOCKET sockConn, char *pcRecvBuf, int *pnRecvLen){int nErrCode = -1;if (INVALID_SOCKET == sockConn){return nErrCode;}int nReadLen = 0;int nTotalReadLen = 0;while(1){nReadLen = recv(sockConn, pcRecvBuf, 1024, 0);if (nReadLen <0){nErrCode = WSAGetLastError();if (WSAEWOULDBLOCK == nErrCode){continue;}}if (0 == nReadLen){break;}pcRecvBuf+=nReadLen;nTotalReadLen+=nReadLen;}*pnRecvLen = nTotalReadLen;return nTotalReadLen;}int WinSock_Recvfrom(SOCKET sockConn, SOCKADDR* pAddrFrom, int *pnAddrFromLen, char *pcRecvBuf, int *pnRecvLen){int nErrCode = -1;if (INVALID_SOCKET == sockConn){return nErrCode;}int nReadLen = 0;int nTotalReadLen = 0;while(1){nReadLen = recvfrom(sockConn, pcRecvBuf, 1024, 0, pAddrFrom, pnAddrFromLen);if (nReadLen <0){nErrCode = WSAGetLastError();if (WSAEWOULDBLOCK == nErrCode){continue;}}if (0 == nReadLen){break;}pcRecvBuf+=nReadLen;nTotalReadLen+=nReadLen;}*pnRecvLen = nTotalReadLen;return nTotalReadLen;}int WinSock_Close(SOCKET sockConn){int nRetVal = -1;int nErrCode = -1;nRetVal = closesocket(sockConn);if (0 != nRetVal){nErrCode = WSAGetLastError();}return nRetVal;}/*nHow: SD_RECEIVE - recv no longerSD_SEND - send no longerSD_BOTH- send and recv no longer*/int WinSock_Shutdown(SOCKET sockConn, int nHow){int nRetVal = -1;int nErrCode = -1;nRetVal = shutdown(sockConn, nHow);if (SOCKET_ERROR == nRetVal ){nErrCode = WSAGetLastError();}return nRetVal;}//finally call int WinSock_Cleanup(){int nRetVal = -1;int nErrCode = -1;nRetVal = WSACleanup();if (SOCKET_ERROR == nRetVal){nErrCode = WSAGetLastError();}return nRetVal;}//original length:8Kint WinSock_SetSockRecvBufLen(SOCKET sock, int nMultiple){int nBufLen = 0;int nOptLen = sizeof(nBufLen);int nErrCode = -1;int nRetVal = -1;nRetVal = getsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char*)&nBufLen, &nOptLen);if (SOCKET_ERROR == nRetVal){nErrCode = WSAGetLastError();return nRetVal;}nBufLen*=nMultiple;nRetVal = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (const char*)&nBufLen, nOptLen);if (SOCKET_ERROR == nRetVal){nErrCode = WSAGetLastError();}return nRetVal;//int nNewBufLen = 0;//nErrCode = getsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char*)&nNewBufLen, &nOptLen);}int GetLocalIP(char *pcLocalIP){if(NULL == pcLocalIP){return -1;}char hostname[MAX_PATH] = {0}; WSADATA wsaData; if (WSAStartup(MAKEWORD(2,2),&wsaData)) //调用Windows Sockets DLL { WSACleanup(); return -2; } //get host name int nRet = gethostname(hostname, MAX_PATH); if (SOCKET_ERROR == nRet) { return -3; } //get host by name HOSTENT * pHostEnt = NULL; pHostEnt =gethostbyname(hostname); if (NULL == pHostEnt) { return -4; } //get ip address struct in_addr addr; memcpy(&addr, pHostEnt->h_addr_list[0], sizeof(struct in_addr)); const char* pcIPAddr = inet_ntoa(addr); if(NULL != pcIPAddr && strlen(pcIPAddr) >0) { strcpy(pcLocalIP, pcIPAddr); } WSACleanup(); return 0;}#endif
0 0
- windows socket api 封装
- windows socket简单封装
- windows API 封装
- 使用select 封装 socket api
- Windows Socket API函数
- windows socket api 说明
- 截获 Windows socket API
- Windows Socket API
- windows socket api 说明
- 截获 Windows socket API
- Windows Socket API函数
- Windows Socket API
- windows socket api
- windows socket api 函数
- Windows Socket API 小结
- Windows Socket API 使用
- Windows Socket API函数
- Windows Socket API函数
- 长靴加皮裤 韩国女星大秀美腿
- 在广州传智播客培训平面设计要学哪些软件?
- [VS2008]解决“由于应用程序的配置不正确,应用程序未能启动,重新安装应用程序可能会纠正这个问题”
- FAT文件系统工作原理
- JS判断IP合法性
- windows socket api 封装
- 广州传智播客.Net就业班课程深项目真
- js 常用正则表达式表单验证代码
- 重叠(overlapped)I/O模型
- 广州传智播客Android实战班最受欢迎
- Linux关机命令
- mapreduce实现推荐系统
- 打开和关闭wifi和移动数据链接
- VC6.0