Example #1
0
/*................无用...............*/
BOOL CWorkThread::Accept(CListenSocket& ListenSocket)
{
	CAsyncSocket asAccept;
	if(!ListenSocket.Accept(asAccept))
		return FALSE;

	CString strOut;
	strOut.Format(_T("CPoolThread::Accept() socket :%d Accept!\n"),asAccept.m_hSocket);
	TRACE(strOut);
	
	//线程池是否已满
	if(!WorkItem::IsPoolFull())
	{
		if(fpQueueUserWorkItem != NULL)
		{
			//
			fpQueueUserWorkItem(WorkItem::ClientWorkItemThread, (LPVOID)asAccept.Detach(), WT_EXECUTELONGFUNCTION);
		}
		else
		{
			//启动WorkItem类的ClientWorkThread线程
			AfxBeginThread(WorkItem::ClientWorkThread, (LPVOID)asAccept.Detach());
		}
	}
	else
	{
		// refuse request,close socket!拒绝客户端请求,关闭socket
		asAccept.Close();
	}

	return TRUE;
}
Example #2
0
	BOOL CLANConnection::CreateSocket(int &nError)
	{
		nError = 0;
		if (m_hSocket != INVALID_SOCKET) return TRUE;

		// Socket erzeugen
		CAsyncSocket socket;
		if (!socket.Socket(SOCK_DGRAM, 0)) goto error;

/*		// mehrfaches Binden an selben Port erlauben
		BOOL bReuseAddr = TRUE;
		if (!socket.SetSockOpt(SO_REUSEADDR, &bReuseAddr, sizeof(bReuseAddr)))
			goto error; */

		// Socket binden
		if (!socket.Bind(m_nPort))	goto error;

		// Broadcast erlauben
		BOOL bBroadcast = TRUE;
		if (!socket.SetSockOpt(SO_BROADCAST, &bBroadcast, sizeof(bBroadcast)))
			goto error;

		m_hSocket = socket.Detach();
		return TRUE;

error:
		nError = socket.GetLastError();
		return FALSE;
	}
void DataSocket::OnAccept(int nErrorCode) 
{
	// Accept the connection using a temp CSocket object.
	CAsyncSocket tmpSocket;
	Accept(tmpSocket);
	
	SOCKET socket = tmpSocket.Detach();
	Close();

	Attach(socket);

	m_bConnected = TRUE;

	if (!m_bInitialized)
		SetTransferType(m_nTransferType);
	
	CAsyncSocket::OnAccept(nErrorCode);
}
	bool CCommunication::Connect(CString sServerIP,DWORD nPort)
	{
		CAsyncSocket asConnect;

		if(!AfxSocketInit())
			return false;

		if(!asConnect.Create(0, SOCK_STREAM, NULL))
			return false;

		DWORD dwFlag = 0;

		ioctlsocket(asConnect.m_hSocket, FIONBIO, &dwFlag);

		if(!asConnect.Connect(sServerIP, nPort))
			return false;

		dwFlag=1;
		ioctlsocket(asConnect.m_hSocket, FIONBIO, &dwFlag);

		m_WorkSocket = asConnect.Detach();
		return true;
	}