当前位置: 首页>>代码示例>>C++>>正文


C++ CSocket::Attach方法代码示例

本文整理汇总了C++中CSocket::Attach方法的典型用法代码示例。如果您正苦于以下问题:C++ CSocket::Attach方法的具体用法?C++ CSocket::Attach怎么用?C++ CSocket::Attach使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在CSocket的用法示例。


在下文中一共展示了CSocket::Attach方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: Accept

bool CServerWorkFlow::Accept(SOCKET hListenSock)
{
	CSocket socket;
	SOCKET hOldSock = socket.Detach();
	socket.Attach(hListenSock);
	BOOL bAccept = socket.Accept(*m_pServerSocket);
	socket.Detach();
	socket.Attach(hOldSock);
	return (TRUE == bAccept);
}
开发者ID:tylor1,项目名称:repo,代码行数:10,代码来源:ServerWorkFlow.cpp

示例2: Run

	void CServerHandshake::Run()
	{
		VERIFY(AfxSocketInit());

		CSocket socket; // führen Handshake innerhalb des Threads synchron aus
		socket.Attach(m_hSocket);

		clock_t end = clock() + HANDSHAKE_TIMEOUT * CLOCKS_PER_SEC;

		// (1.) auf eingehenden Identifizierungs-String (HANDSHAKE_CLIENTID) + Versionsnummer
		// vom Client warten
		UINT nCount = strlen(HANDSHAKE_CLIENTID) + 1 + sizeof(UINT) + 1;
		if (!RequestData(socket, nCount, end))
		{
			DEBUG_MSG("client timed out, disconnected");
			SetExitCode(1);
			return;
		}

		// Daten in Puffer übertragen
		BYTE *lpBuf = new BYTE[nCount];
		int nDone = socket.Receive(lpBuf, nCount);
		if (nDone == SOCKET_ERROR) goto sockerror;
		else if (nDone < nCount) goto error;

		// Pufferinhalt prüfen
		if (memcmp(lpBuf, HANDSHAKE_CLIENTID, strlen(HANDSHAKE_CLIENTID)) != 0
			|| lpBuf[strlen(HANDSHAKE_CLIENTID)] != 0
			|| lpBuf[nCount - 1] != 0)
		{
			DEBUG_MSG("client is not a BotE client, disconnected");
			delete[] lpBuf;
			SetExitCode(1);
			return;
		}

		// Puffer erfolgreich geprüft, Versionsnummer extrahieren
		UINT nClientVersion;
		memcpy(&nClientVersion, lpBuf + (strlen(HANDSHAKE_CLIENTID) + 1), sizeof(UINT));
		TRACE("server: client has version %u\n", nClientVersion);
		delete[] lpBuf;
		lpBuf = NULL;

		// abbrechen, wenn andere Versionsnummer
		if (nClientVersion != m_nServerVersion)
		{
			DEBUG_MSG("client has incompatible version, disconnected");
			SetExitCode(1);
			return;
		}

		// (2.) Server-Identifizierung + Versionsnummer senden
		nDone = socket.Send(HANDSHAKE_SERVERID, strlen(HANDSHAKE_SERVERID));
		if (nDone == SOCKET_ERROR) goto sockerror;
		else if (nDone < strlen(HANDSHAKE_SERVERID)) goto error;

		lpBuf = new BYTE[nCount = 1 + sizeof(UINT) + 1];
		lpBuf[0] = lpBuf[nCount - 1] = 0;
		memcpy(&lpBuf[1], &m_nServerVersion, sizeof(UINT));

		nDone = socket.Send(lpBuf, nCount);
		if (nDone == SOCKET_ERROR) goto sockerror;
		else if (nDone < nCount) goto error;

		delete[] lpBuf;
		lpBuf = NULL;

		// (3.a) Bestätigung vom Client abwarten
		int nLen = 3 + sizeof(UINT);
		if (!RequestData(socket, nLen, end))
		{
			DEBUG_MSG("client timed out, disconnected");
			SetExitCode(1);
			return;
		}

		// Bestätigung prüfen
		lpBuf = new BYTE[3];
		nDone = socket.Receive(lpBuf, 3);
		if (nDone == SOCKET_ERROR) goto sockerror;
		else if (nDone < 3) goto error;

		if (memcmp(lpBuf, "OK\0", 3) != 0)
		{
			DEBUG_MSG("client did not confirm, disconnected");
			delete[] lpBuf;
			SetExitCode(1);
			return;
		}

		delete[] lpBuf;

		// Länge des Benutzernamens ermitteln
		lpBuf = new BYTE[sizeof(UINT)];
		nDone = socket.Receive(lpBuf, sizeof(UINT));
		if (nDone == SOCKET_ERROR) goto sockerror;
		else if (nDone < sizeof(UINT)) goto error;

		UINT nStrLen = 0;
		memcpy(&nStrLen, lpBuf, sizeof(UINT));
//.........这里部分代码省略.........
开发者ID:IcyX,项目名称:bote,代码行数:101,代码来源:ServerHandshake.cpp


注:本文中的CSocket::Attach方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。