本文整理汇总了C++中CSocket::Receive方法的典型用法代码示例。如果您正苦于以下问题:C++ CSocket::Receive方法的具体用法?C++ CSocket::Receive怎么用?C++ CSocket::Receive使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CSocket
的用法示例。
在下文中一共展示了CSocket::Receive方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: OnGetSource
void CTcpClientDlg::OnGetSource()
{
CSocket s;
if(!s.Create()) {
AfxMessageBox("소켓 생성 실패");
return;
}
if(!s.Connect("www.google.com", 80)) {
AfxMessageBox("접속 실패!");
return ;
}
//GET 명령으로 데이터를 얻어옴
//GET 명령 형식 : GET 대상URL HTTP/1.0 <enter><enter>
char * cmd = "GET / HTTP/1.0\r\n\r\n";
s.Send(cmd, strlen(cmd));
char buf[100];
ZeroMemory(buf, 100);
while(s.Receive(buf, 1000)) {
AfxMessageBox(buf);
ZeroMemory(buf, 1000);
}
}
示例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: set_up_parameters
uint32_t set_up_parameters(role_type role, uint32_t myneles, uint32_t* mybytelen,
uint8_t** elements, uint8_t** pelements, CSocket& sock, crypto* crypt) {
uint32_t pneles, nintersections, offset;
//Exchange meta-information and equalize byte-length
sock.Send(&myneles, sizeof(uint32_t));
sock.Receive(&pneles, sizeof(uint32_t));
if(role == SERVER) {
sock.Send(mybytelen, sizeof(uint32_t));
} else {
sock.Receive(mybytelen, sizeof(uint32_t));
}
*elements = (uint8_t*) malloc(myneles * *mybytelen);
*pelements = (uint8_t*) malloc(pneles * *mybytelen);
crypt->gen_rnd(*elements, myneles * *mybytelen);
//Exchange elements for later check
if(role == SERVER) {
sock.Send(*elements, myneles * *mybytelen);
sock.Receive(*pelements, pneles * *mybytelen);
} else { //have the client use some of the servers values s.t. the intersection is not disjoint
sock.Receive(*pelements, pneles * *mybytelen);
nintersections = rand() % min(myneles, pneles);
offset = myneles / nintersections;
for(uint32_t i = 0; i < nintersections; i++) {
memcpy(*elements + i * offset * *mybytelen, *pelements + i * *mybytelen, *mybytelen);
}
sock.Send(*elements, myneles * *mybytelen);
}
return pneles;
}
示例5: RecvData
BOOL CServerSocket::RecvData(CSocket& client,
BYTE* pData, long nLen)
{
BYTE* pTmpData=pData;
long nLeft=nLen;//剩余的还没有的接收的数据的长度
while (nLeft>0)
{
int nRecv=client.Receive(pTmpData,nLeft);
if (nRecv==SOCKET_ERROR)
{
return FALSE;
}
nLeft-=nRecv;
pTmpData+=nRecv;
}
return TRUE;
}
示例6: main
void main()
{
AfxSocketInit();
CSocket sock;
int n = sock.Create(PORT1);
if(!n)
{
cout << "创建句柄失败:" << GetLastError() << endl;
return;
}
sock.Listen();
while (true)
{
CSocket socka;
n = sock.Accept(socka);
if(!n)
{
cout << "接受对方连接失败:" << GetLastError() << endl;
return;
}
while (true)
{
char s[256];
n = socka.Receive(s,sizeof(s));
if(SOCKET_ERROR == n)
{
cout << "接收数据失败:" << GetLastError() << endl;
break;
}
if(!n)
{
cout << "结束接收数据,句柄关闭!" << endl;
break;
}
cout << s <<endl;
}
socka.Close();
}
}
示例7: RecvData
BOOL CServerSocket::RecvData(CSocket & client,
BYTE* pData,DWORD nLen)
{
LPBYTE pTmpData=pData;//当前要存放数据的缓冲区地址
DWORD nLeft=nLen;//当前未接收的数据的长度
while(nLeft>0)
{
//接收剩余数据,并返回当次接收的实际数据长度
int nRecv=client.Receive(pTmpData,nLeft);
if (nRecv==SOCKET_ERROR)
{
return FALSE;
}
//重新计算剩余的数据长度
nLeft=nLeft-nRecv;
//将数据指针偏移已经接受的数据量
pTmpData=pTmpData+nRecv;
}
return TRUE;
}
示例8: Resolve
boolean CDNSClient::Resolve (const char *pHostname, CIPAddress *pIPAddress)
{
assert (pHostname != 0);
if ('1' <= *pHostname && *pHostname <= '9')
{
return FALSE;
}
assert (m_pNetSubSystem != 0);
CIPAddress DNSServer (m_pNetSubSystem->GetConfig ()->GetDNSServer ()->Get ());
CSocket Socket (m_pNetSubSystem, IPPROTO_UDP);
if (Socket.Connect (DNSServer, 53) != 0)
{
return FALSE;
}
u8 Buffer[DNS_MAX_MESSAGE_SIZE];
memset (Buffer, 0, sizeof Buffer);
TDNSHeader *pDNSHeader = (TDNSHeader *) Buffer;
u16 nXID = s_nXID++;
pDNSHeader->nID = le2be16 (nXID);
pDNSHeader->nFlags = BE (DNS_FLAGS_OPCODE_QUERY | DNS_FLAGS_RD);
pDNSHeader->nQDCount = BE (1);
u8 *pQuery = Buffer + sizeof (TDNSHeader);
char Hostname[MAX_HOSTNAME_SIZE];
strncpy (Hostname, pHostname, MAX_HOSTNAME_SIZE-1);
Hostname[MAX_HOSTNAME_SIZE-1] = '\0';
char *pSavePtr;
size_t nLength;
char *pLabel = strtok_r (Hostname, ".", &pSavePtr);
while (pLabel != 0)
{
nLength = strlen (pLabel);
if ( nLength > 255
|| (int) (nLength+1+1) >= DNS_MAX_MESSAGE_SIZE-(pQuery-Buffer))
{
return FALSE;
}
*pQuery++ = (u8) nLength;
strcpy ((char *) pQuery, pLabel);
pQuery += nLength;
pLabel = strtok_r (0, ".", &pSavePtr);
}
*pQuery++ = '\0';
TDNSQueryTrailer QueryTrailer;
QueryTrailer.nQType = BE (DNS_QTYPE_A);
QueryTrailer.nQClass = BE (DNS_QCLASS_IN);
if ((int) (sizeof QueryTrailer) > DNS_MAX_MESSAGE_SIZE-(pQuery-Buffer))
{
return FALSE;
}
memcpy (pQuery, &QueryTrailer, sizeof QueryTrailer);
pQuery += sizeof QueryTrailer;
int nSize = pQuery - Buffer;
assert (nSize <= DNS_MAX_MESSAGE_SIZE);
unsigned char RecvBuffer[DNS_MAX_MESSAGE_SIZE];
int nRecvSize;
unsigned nTry = 1;
do
{
if ( nTry++ > 3
|| Socket.Send (Buffer, nSize, 0) != nSize)
{
return FALSE;
}
CScheduler::Get ()->MsSleep (1000);
nRecvSize = Socket.Receive (RecvBuffer, DNS_MAX_MESSAGE_SIZE, MSG_DONTWAIT);
assert (nRecvSize < DNS_MAX_MESSAGE_SIZE);
}
while (nRecvSize < (int) (sizeof (TDNSHeader)+sizeof (TDNSResourceRecordTrailerAIN)));
pDNSHeader = (TDNSHeader *) RecvBuffer;
if ( pDNSHeader->nID != le2be16 (nXID)
|| (pDNSHeader->nFlags & BE ( DNS_FLAGS_QR
| DNS_FLAGS_OPCODE
| DNS_FLAGS_TC
| DNS_FLAGS_RCODE))
!= BE (DNS_FLAGS_QR | DNS_FLAGS_OPCODE_QUERY | DNS_RCODE_SUCCESS)
|| pDNSHeader->nQDCount != BE (1)
|| pDNSHeader->nANCount == BE (0))
{
return FALSE;
//.........这里部分代码省略.........
示例9: 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);
}
示例10: SendPaymentData
//.........这里部分代码省略.........
BYTE cHeader[30];
int nOffset = 0;
AscToBcd(cHeader + nOffset,(unsigned char *)szTPDU.GetBuffer(),szTPDU.GetLength());
nOffset += szTPDU.GetLength() / 2;
AscToBcd(cHeader + nOffset,(unsigned char *)szHeader.GetBuffer(),szHeader.GetLength());
nOffset += szHeader.GetLength() / 2;
cHeader[nOffset] = 0x02;
nOffset += 1;
cHeader[nOffset] = 0x00;
nOffset += 1;
BYTE cData[512];
memset(cData,0,512);
unsigned int nLen = package->GetData(cData,512,cHeader,nOffset,false);
delete package;
char cLen[5] = {'\0'};
itoa_hex(nLen,cLen,2);
unsigned char cDataLen[3] = {'\0'};
AscToBcd(cDataLen,(unsigned char *)cLen,4);
BYTE cPackageData[512];
memset(cPackageData,0,sizeof(cPackageData));
memcpy(cPackageData,cDataLen,2);
memcpy(cPackageData + 2,cData,nLen);
TCHAR szCurPath[MAX_PATH];
GetCurrentDirectory(sizeof(szCurPath),szCurPath);
_tcscat(szCurPath,TEXT("\\Reversal.ini"));
CSocket client;
if( client.InitSocket() == FALSE)
{
szPayState = "E7";
return -1;
}
if( client.Connect(szServerIP,atoi(szPort)) == false)
{
szPayState = "E7";
return -1;
}
gLog.Log(cPackageData,nLen + 2);
client.Send(cPackageData,nLen + 2);
BYTE RecvData[1024];
memset(RecvData,0,1024);
nLen = client.Receive(RecvData,1024);
if(nLen != 0)
{
gLog.Log(RecvData,nLen);
CIso8583Parse *receivePackage = new CIso8583Parse();
char cResult[128];
memset(cResult,0,128);
Read8583Package(receivePackage,RecvData,39,2,(BYTE *)cResult);
delete receivePackage;
LONG lProCode = atol(szTraceCode.GetBuffer()) + 1;
unsigned char cAsciiBuff[12];
memset(cAsciiBuff,0,sizeof(cAsciiBuff));
BinToAsc(cAsciiBuff,lProCode,6);
WritePrivateProfileString(TEXT("TraceCode"),TEXT("TraceCode"),(char *)cAsciiBuff,szCurPath);
if(strcmp(cResult,"A0") == 0) //根据银联网络的说法,MAC错误也需要冲正
{
WritePrivateProfileString(TEXT("Reversal"),TEXT("Ack"),"A0",szCurPath);
WritePrivateProfileString(TEXT("Reversal"),TEXT("Flag"),"0",szCurPath);//0 -- 未处理
CReversalTransaction transaction;
transaction.SendReversalTransactionData();//冲正处理
}
else if( strcmp(cResult,"05") == 0)
{//认证失败
szPayState = "E2";
return -1;
}
else if( strcmp(cResult,"51") == 0)
{
szPayState = "E5";
return -1;
}
else if( strcmp(cResult, "55") == 0)
{
szPayState = "E2";
return -1;
}
else if( strcmp(cResult, "75") == 0)
{
szPayState = "E1";
return -1;
}
szPayState = "1";//交易成功
}
else
{//网络原因,消费冲正
szPayState = "E6";
WritePrivateProfileString(TEXT("Reversal"),TEXT("Ack"),"98",szCurPath);//POS终端在时限内未能收到POS中心的应答消息而引发
WritePrivateProfileString(TEXT("Reversal"),TEXT("Flag"),"0",szCurPath);//0 -- 未处理
CReversalTransaction transaction;
transaction.SendReversalTransactionData();//冲正处理
}
return 1;//交易成功
}
示例11: 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));
//.........这里部分代码省略.........
示例12: SendReversalTransactionData
int CReversalTransaction::SendReversalTransactionData(char *pAccount,LONG Money,char *cTraceCode,char *cAck)
{
//组包
CIso8583Package *package = new CIso8583Package();
int iRes = BuildPackage(package,pAccount,Money,cTraceCode,cAck);
if (iRes != 0) return iRes; //组包不成功,返回错误码
BYTE cHeader[30];
int nOffset = 0;
AscToBcd(cHeader + nOffset,(unsigned char *)szTPDU.GetBuffer(),szTPDU.GetLength());
nOffset += szTPDU.GetLength() / 2;
AscToBcd(cHeader + nOffset,(unsigned char *)szHeader.GetBuffer(),szHeader.GetLength());
nOffset += szHeader.GetLength() / 2;
cHeader[nOffset] = 0x04;
nOffset += 1;
cHeader[nOffset] = 0x00;
nOffset += 1;
BYTE cData[512];
memset(cData,0,512);
unsigned int nLen = package->GetData(cData,512,cHeader,nOffset,false);
delete package;
char cLen[5] = {'\0'};
itoa_hex(nLen,cLen,2);
unsigned char cDataLen[3] = {'\0'};
AscToBcd(cDataLen,(unsigned char *)cLen,4);
BYTE cPackageData[512];
memset(cPackageData,0,sizeof(cPackageData));
memcpy(cPackageData,cDataLen,2);
memcpy(cPackageData + 2,cData,nLen);
CSocket client;
if( client.InitSocket() == FALSE) return -1;
if( client.Connect(szServerIP,atoi(szPort)) == false) return -1;
gLog.Log(cData,nLen);
client.Send(cData,nLen);
BYTE RecvData[1024];
memset(RecvData,0,1024);
nLen = client.Receive(RecvData,1024);
if(nLen != 0)
{
gLog.Log(RecvData,nLen);
CIso8583Parse *receivePackage = new CIso8583Parse();
char cResult[128];
memset(cResult,0,128);
Read8583Package(receivePackage,RecvData,39,2,(BYTE *)cResult);
delete receivePackage;
if(strcmp(cResult,"00") == 0)
{
TCHAR szCurPath[MAX_PATH];
GetCurrentDirectory(sizeof(szCurPath),szCurPath);
_tcscat(szCurPath,TEXT("\\Reversal.ini"));
WritePrivateProfileString(TEXT("Reversal"),TEXT("Account"),"",szCurPath);
WritePrivateProfileString(TEXT("Reversal"),TEXT("TraceCode"),"",szCurPath);
WritePrivateProfileString(TEXT("Reversal"),TEXT("Money"),"",szCurPath);
WritePrivateProfileString(TEXT("Reversal"),TEXT("Ack"),"",szCurPath);
WritePrivateProfileString(TEXT("Reversal"),TEXT("Flag"),"1",szCurPath);//1 -- 已处理
}
}
return 1;
}
示例13: _tmain
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
HMODULE hModule = ::GetModuleHandle(NULL);
if (hModule != NULL)
{
// initialize MFC and print and error on failure
if (!AfxWinInit(hModule, NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
cout << "\t\t\t################# << CHIEC NON KY DIEU >> ################# " << endl << endl;
KNClient x;
x.InitChat();
x.RegisterName();
cout << "\n\n\t\t ***************** < Luat Choi > *****************\n";
cout << "1. Nguoi choi phai trai qua cac vong choi tuong duong voi so cau hoi cua chuong trinh.\n";
cout << "2. Khi den luot choi :\n \tdoan dung 1 chu cai -> nhan duoc so diem tuong duong so diem quay duoc.\n ";
cout << "\tdoan dung o chu bi mat -> nhan duoc so diem x3.\n";
cout << "\n\t -----> nhan phim bat ky de bat dau tro choi : ";
char k;
cin >> k;
fflush(stdin);
system("cls");
cout << "\t\t\t################# << CHIEC NON KY DIEU >> ################# " << endl << endl;
CSocket Client;
char *str_r = new char[200];
Client.Receive(str_r, 200, 0);
str_r[strlen(str_r)] = 0;
if (strstr(str_r, "Exit"))
{
Client.Close();
return 0;
}
else
{
do
{
if (x.recieve()) //nếu trả về true mới được trl
x.Answer();
else
break;
} while (1);
}
}
}
else
{
示例14: GetTime
unsigned CNTPClient::GetTime (CIPAddress &rServerIP)
{
assert (m_pNetSubSystem != 0);
CSocket Socket (m_pNetSubSystem, IPPROTO_UDP);
if (Socket.Connect (rServerIP, 123) != 0)
{
return 0;
}
u8 NTPPacket[NTP_PACKET_SIZE];
memset (NTPPacket, 0, sizeof NTPPacket);
NTPPacket[0] = 0xE3; // leap indicator: unknown, version: 4, mode: client
NTPPacket[1] = 0; // stratum: unspecified
NTPPacket[2] = 10; // poll: 1024 seconds
NTPPacket[3] = 0; // precision: 1 second
memcpy (NTPPacket+12, "XCIR", 4);
unsigned char RecvPacket[NTP_PACKET_SIZE];
unsigned nTry;
for (nTry = 1; nTry <= 3; nTry++)
{
if (Socket.Send (NTPPacket, sizeof NTPPacket, 0) != sizeof NTPPacket)
{
CLogger::Get ()->Write (FromNTPClient, LogError, "Send failed");
return 0;
}
CScheduler::Get ()->MsSleep (1000);
int nResult = Socket.Receive (RecvPacket, sizeof RecvPacket, MSG_DONTWAIT);
if (nResult < 0)
{
CLogger::Get ()->Write (FromNTPClient, LogError, "Receive failed");
return 0;
}
if (nResult >= 44)
{
break;
}
}
if (nTry > 3)
{
CLogger::Get ()->Write (FromNTPClient, LogError, "Invalid or no response");
return 0;
}
unsigned nSecondsSince1900;
nSecondsSince1900 = (unsigned) RecvPacket[40] << 24
| (unsigned) RecvPacket[41] << 16
| (unsigned) RecvPacket[42] << 8
| (unsigned) RecvPacket[43];
if (nSecondsSince1900 < SEVENTY_YEARS)
{
return 0;
}
unsigned nTime = nSecondsSince1900 - SEVENTY_YEARS;
return nTime;
}