WSAsocket客户端请求服务器示例

本文介绍了一个使用Windows Socket API (WSA)实现的函数,该函数用于向指定服务器请求验证码。通过建立Socket连接并发送XML格式的验证码请求,然后接收并解析服务器返回的数据来获取验证码结果。
摘要由CSDN通过智能技术生成
DWORD WSARequestServerVerifyCode(const std::string server_ip, int server_port, const std::string& request_verify_code_xml_)
{
	if (request_verify_code_xml_.length() <= 0) 
		return -1;

	USES_CONVERSION;
	unsigned short port = server_port;//8866
	int socket_type = SOCK_STREAM;
	sockaddr_in	server;
	SOCKET sockConn;

	char recvBuffer[128] = {0};
	WSABUF wsabuf;
	wsabuf.buf=recvBuffer;
	wsabuf.len=sizeof(recvBuffer);
	DWORD NoBR;
	DWORD flags=0;

	int nErr = -1;
	//wsaevetn
	WSAEVENT hWsaEvent = WSACreateEvent();
	if (WSA_INVALID_EVENT == hWsaEvent)
	{
		nErr = WSAGetLastError();
		return nErr;
	}

	HANDLE hEvents[1];
	hEvents[0] = hWsaEvent;
	WSAOVERLAPPED wsaOverlap;
	ZeroMemory(&wsaOverlap, sizeof(wsaOverlap));
	wsaOverlap.hEvent = hWsaEvent;
	//start up socket
	server.sin_addr.s_addr = inet_addr(server_ip.c_str());   
	server.sin_family = AF_INET;
	server.sin_port = htons(port);//
	//startup wsasocket
	sockConn = WSASocket(AF_INET, socket_type,0, NULL, 0, WSA_FLAG_OVERLAPPED );
	if (INVALID_SOCKET == sockConn)
	{
		nErr = WSAGetLastError();
		WSACloseEvent(hWsaEvent);
		return nErr;
	}

	//connect
	if (connect(sockConn,(SOCKADDR*)&server,sizeof(server)) == SOCKET_ERROR)
	{
		nErr = WSAGetLastError();
		closesocket(sockConn);
		WSACloseEvent(hWsaEvent);
		return nErr;
	}

	//head
	ProtoHead ProtoHead;
	memcpy(ProtoHead.szID, "PROTOHEAD", 9);
	ProtoHead.dwOption = 0;

	ProtoHead.dwLen = (DWORD) request_verify_code_xml_.size(); //  request_verify_code_xml_
	int nTotalLen = (int) (sizeof(ProtoHead) + request_verify_code_xml_.size() );
	char *pSendBuf = (char*)malloc(nTotalLen);
	int nLeftLen = nTotalLen;
	int nSentLen = 0;
	memcpy(pSendBuf, &ProtoHead, sizeof(ProtoHead));
	memcpy(pSendBuf + sizeof(ProtoHead), request_verify_code_xml_.c_str(), request_verify_code_xml_.size());
	char *ptr = pSendBuf;
	while(nLeftLen > 0)
	{
		nSentLen = send(sockConn, ptr, nLeftLen, 0);
		if (SOCKET_ERROR == nSentLen)
		{
			nErr = WSAGetLastError();
			closesocket(sockConn);
			WSACloseEvent(hWsaEvent);
			free(pSendBuf);
			return nErr;
		}
		nLeftLen = nTotalLen - nSentLen;
		ptr+=nSentLen;
	}
	free(pSendBuf);
	//recv
	int nRecvLen = WSARecv(sockConn, &wsabuf, 1,&NoBR ,&flags, &wsaOverlap,NULL);
	if (SOCKET_ERROR == nRecvLen)
	{
		nErr = WSAGetLastError();
		if (WSA_IO_PENDING != nErr)//other wrong reseaons
		{
			closesocket(sockConn);
			WSACloseEvent(hWsaEvent);
			return nErr;
		}
	}
	//WaitForMultipleObjects
	DWORD dwRet = WaitForMultipleObjects(1,hEvents, FALSE, 15*1000 );
	if (WSA_WAIT_FAILED == dwRet || WSA_WAIT_TIMEOUT == dwRet)
	{
		closesocket(sockConn);
		WSACloseEvent(hWsaEvent);
		return nErr;
	}
	else if (WAIT_OBJECT_0 + 1 == dwRet)
	{
		//exit event;
		closesocket(sockConn);
		WSACloseEvent(hWsaEvent);
	}

	//handle
	DWORD dwTransferedLen = 0;
	if (WSAGetOverlappedResult(sockConn, &wsaOverlap, &dwTransferedLen, FALSE, &flags) == FALSE)
	{
		nErr = WSAGetLastError();
		closesocket(sockConn);
		WSACloseEvent(hWsaEvent);
		return nErr;
	}

	closesocket(sockConn);
	WSACloseEvent(hWsaEvent);
	if (dwTransferedLen == 0) //peer closed
	{
		nErr = WSAGetLastError();
		return nErr;
	}

	if (_strnicmp(recvBuffer,"PROTOHEAD",strlen("PROTOHEAD"))==0)
	{
		ProtoHead *pProtoHead = (ProtoHead*)recvBuffer;
		const char *pXml = recvBuffer + sizeof(ProtoHead);
		std::string strXml(pXml, pProtoHead->dwLen);
		TiXmlDocument doc;
		doc.Parse(strXml.c_str(), 0 , TIXML_ENCODING_LEGACY);
		TiXmlElement *root = doc.RootElement();
		int nResultCode = -1;
		root->Attribute("result", &nResultCode);
		if ( 1 != nResultCode)
		{
			return nResultCode;
		}
	}
	return -1;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值