本文整理汇总了C++中CSocket::Detach方法的典型用法代码示例。如果您正苦于以下问题:C++ CSocket::Detach方法的具体用法?C++ CSocket::Detach怎么用?C++ CSocket::Detach使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CSocket
的用法示例。
在下文中一共展示了CSocket::Detach方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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);
}
示例2: sizeof
/**
* listens and accepts connections on the server
*/
BOOL maliciousot::ConnectionManagerServer::setup_connection() {
int num_connections = m_num_of_threads+1;
cerr << "ConnectionManagerServer->setup_connection() started." << endl;
// try to bind() and then listen
if ((!m_sockets[0].Socket()) ||
(!m_sockets[0].Bind(m_port, m_address)) ||
(!m_sockets[0].Listen()) ) {
goto listen_failure;
}
for(int i = 0; i<num_connections; i++) { //twice the actual number, due to double sockets for OT
CSocket sock;
// try: CSocket sock = accept()
if(!m_sockets[0].Accept(sock)) {
cerr << "Error in accept" << endl;
goto listen_failure;
}
// cerr << "Server: accept succeded i = " << i << endl;
// receive the other side thread id (the first thing that is sent on the socket)
UINT threadID;
sock.Receive(&threadID, sizeof(int));
// cerr << "Server: received threadID = " << threadID << endl;
// ??
if(threadID >= num_connections) {
// cerr << "Server: threadID >= num_connections, num_connections = " << num_connections << endl;
sock.Close();
i--;
continue;
}
// locate the socket appropriately
// cerr << "Server: attaching socket to threadID = " << threadID << endl;
m_sockets[threadID].AttachFrom(sock);
sock.Detach();
}
cerr << "ConnectionManagerServer->setup_connection() ended." << endl;
return TRUE;
listen_failure:
cerr << "Listen failed" << endl;
return FALSE;
}
示例3: Listen
BOOL Listen() {
#ifndef BATCH
cout << "Listening: " << m_nAddr << ":" << m_nPort
<< ", with size: " << m_nNumOTThreads << endl;
#endif
if (!m_vSocket->Socket()) {
goto listen_failure;
}
if (!m_vSocket->Bind(m_nPort, m_nAddr)) {
goto listen_failure;
}
if (!m_vSocket->Listen()) {
goto listen_failure;
}
for (int i = 0; i < 1;
i++) // twice the actual number, due to double sockets for OT
{
CSocket sock;
// cout << "New round! " << endl;
if (!m_vSocket->Accept(sock)) {
cerr << "Error in accept" << endl;
goto listen_failure;
}
UINT threadID;
sock.Receive(&threadID, sizeof(int));
if (threadID >= 1) {
sock.Close();
i--;
continue;
}
#ifndef BATCH
cout << " (" << m_nPID << ") (" << threadID << ") connection accepted"
<< endl;
#endif
// locate the socket appropriately
m_vSocket->AttachFrom(sock);
sock.Detach();
}
#ifndef BATCH
cout << "Listening finished" << endl;
#endif
return TRUE;
listen_failure:
cout << "Listen failed" << endl;
return FALSE;
}
示例4: OnAccept
void CIPServer::OnAccept(int nErrorCode)
{
CCardSwipeServiceApp* pApp = (CCardSwipeServiceApp*)AfxGetApp();
CSocket newSocket;
SOCKADDR saddr;
char ch[96];
CString sAddress;
CString sIPAddress, sError;
long nCardReaderId = -1;
int iReturn;
this->Accept(newSocket);
memset(&saddr,0,sizeof(SOCKADDR));
int iLen = sizeof(saddr);
newSocket.GetSockName(&saddr,&iLen);
// get dotted ip address
lstrcpyA(ch,inet_ntoa(((SOCKADDR_IN*)&saddr)->sin_addr));
sIPAddress = ch;
sAddress.Format("%s:%d",ch,((SOCKADDR_IN*)&saddr)->sin_port);
// is this address in the database?
OysterClassLibrary::IOysterPtr pOyster;
OysterClassLibrary::ICardReaderPtr pCardReader;
HRESULT hr = pOyster.CreateInstance(OysterClassLibrary::CLSID_Oyster);
if (hr != ERROR_SUCCESS)
{
GetLastErrorString(sError,hr);
throw CGeneralException("CIPServer::OnAccept","Could not create OysterClassLibrary object (%d): %s",hr,sError);
}
memset(ch,0,sizeof(ch));
iReturn = newSocket.Receive(ch,sizeof(ch));
if (iReturn <= 0)
{
// either closed or an error occured
GetError(sError,iReturn);
throw CGeneralException("CIPServer::OnAccept","Receive function failed (%d): %s",iReturn,sError);
}
nCardReaderId = (long)&ch[0];
pCardReader = pOyster->GetCardReaderById(nCardReaderId);
if (pCardReader)
{
CVirtualCardReaderSocket* pNewSocket = new CVirtualCardReaderSocket(nCardReaderId);
pNewSocket->Attach(newSocket.m_hSocket);
newSocket.Detach();
pNewSocket->Send(ECMD_COMMANDSTRING[ECMD_CONNECTSUCCESS],strlen(ECMD_COMMANDSTRING[ECMD_CONNECTSUCCESS]));
pApp->m_mapCardSwipeIdToCardSwipe
pApp->m_vClients.push_back(pNewSocket);
DebugTell(_T("New client connection from %s\n"),(LPCTSTR)sAddress);
}
else
{
// invalid connection, send notification
newSocket.Send(ECMD_COMMANDSTRING[ECMD_ERRORCONNECT],strlen(ECMD_COMMANDSTRING[ECMD_ERRORCONNECT]));
}
pOyster.Release();
UNREFERENCED_PARAMETER(nErrorCode);
CSocket::OnAccept(nErrorCode);
}
示例5: Run
//.........这里部分代码省略.........
// 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));
delete[] lpBuf;
// (3.b) Benutzername empfangen
if (!RequestData(socket, nStrLen, end))
{
DEBUG_MSG("client timed out, disconnected");
SetExitCode(1);
return;
}
// Benutzername lesen
lpBuf = new BYTE[nStrLen + 1];
nDone = socket.Receive(lpBuf, nStrLen);
if (nDone == SOCKET_ERROR) goto sockerror;
else if (nDone < nStrLen) goto error;
{
lpBuf[nStrLen] = 0;
CString strUserName((char *)lpBuf);
strUserName.Trim();
// dem Server einen neuen Client melden; verwenden kleinere Versionsnummer zur
// Kommunikation
server.OnNewClient(socket.Detach(), min(m_nServerVersion, nClientVersion), strUserName);
}
delete[] lpBuf;
return;
sockerror:
server.OnSocketError(socket.GetLastError());
error:
if (lpBuf) delete[] lpBuf;
SetExitCode(1);
}