本文整理汇总了C++中Socket::Accept方法的典型用法代码示例。如果您正苦于以下问题:C++ Socket::Accept方法的具体用法?C++ Socket::Accept怎么用?C++ Socket::Accept使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Socket
的用法示例。
在下文中一共展示了Socket::Accept方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: Socket
int Sockets_manager::AcceptFromHost(void)
{
Socket* skt;
char ip[16];
skt = this->GetHostSocket();
if (skt != NULL)
{
int fdmax;
fd_set fdread;
struct timeval timeout;
fdmax = skt->fd + 1;
FD_ZERO(&fdread);
FD_SET(skt->fd, &fdread);
timeout.tv_sec = 0;
timeout.tv_usec = 0;
if (select(fdmax, &fdread, 0, 0, &timeout) == -1)
return -1;
if (FD_ISSET(skt->fd, &fdread))
{
skt->Accept(&fdmax, ip);
if (fdmax != -1)
{
skt = NULL;
while (skt == NULL)
skt = new Socket(fdmax, ip);
this->clientSockets.push_back(skt);
return 1;
}
}
}
return 0;
}
示例2: main
int main(int argc, char **argv)
{
print("151 - TCP Server | Centhra Engine v%s\n", getVersionString().c_str());
Socket *server = Socket::Create(Socket::IP4, Socket::Stream, Socket::TCP, true);
server->Bind(12345);
server->Listen(16);
while(true)
{
Socket *client = server->Accept();
if(client)
{
char buff[1024];
memset(buff, 0, 1024);
client->Read(buff, 1024);
print("Receiving Msg: %s\n", buff);
client->Shutdown();
delete client;
}
sleepMS(16);
}
delete server;
return 0;
}
示例3: Socket_accept
int Socket_accept(Context* ctx, Value& self)
{
Socket* socket = static_cast<Socket*>(self.val.object);
Socket* retSock = 0;
SocketAddress* retAddr = 0;
if (socket->Accept(&retSock, &retAddr)) {
ctx->Push(retSock);
ctx->Push(retAddr);
} else {
ctx->PushNull();
ctx->PushNull();
}
return 2;
}
示例4: StartJobHandler
/*
* Start job handler thread
*/
void* StartJobHandler(void* thread_args){
int listen_port = *(int*) thread_args;
std::cout << "StartJobHandler(" << listen_port << ")\n";
//
// Server socket
//
try {
Socket server;
if (!server.Create() ) {
throw SocketException ( "Could not create server socket." );
}
if (!server.Bind(listen_port)) {
throw SocketException ( "Could not bind to port." );
}
std::cout << "bind done" << std::endl;
if ( ! server.Listen() ) {
throw SocketException ( "Could not listen to socket." );
}
std::cout << "Waiting for incoming connections..." << std::endl;
while ( true ) {
Socket *client_sock = new Socket();
std::cout << "before accept\n";
//server.accept ( new_sock );
if ( ! server.Accept(*client_sock )) {
delete client_sock;
throw SocketException ( "Could not accept socket." );
}
std::cout << "Connection accepted" << std::endl;
pthread_t sniffer_thread;
//thrd_args->socket = &client_sock;
if (pthread_create(&sniffer_thread , NULL , JobHandlerConnectionHandler , (void*)client_sock) < 0) {
//perror("could not create thread");
std::cout << "could not create thread\n";
delete client_sock;
continue;
}
//Now join the thread , so that we dont terminate before the thread
//pthread_join( sniffer_thread , NULL);
std::cout <<"Handler assigned" << std::endl;
}
} catch ( SocketException& e ) {
std::cout << "Exception was caught:" << e.description() << "\nExiting.\n";
}
return NULL;
}
示例5: if
unsigned long THREAD_FUNC SocketSubsystem::ListeningThread( void* Arg )
{
SocketSubsystem* Subsystem = (SocketSubsystem*)Arg;
ThreadStartupData StartupData;
memset( &StartupData, 0, sizeof( StartupData ) );
Subsystem->mListeningMutex.Aquire();
Array<ThreadStartupData>& StartupDataList = Subsystem->mThreadStartupData;
for( size_t i = 0; i < StartupDataList.Length(); ++i )
{
if( !StartupDataList[i].IsClaimed )
{
StartupDataList[i].IsClaimed = TRUE;
StartupData = StartupDataList[i];
}
}
Subsystem->mListeningMutex.Release();
assert( StartupData.IsClaimed && StartupData.Socket != NULL && "Couldn't find startup data for socket listening thread" );
Socket* ListeningSocket = StartupData.Socket;
UBOOL ContinueListening = TRUE;
while( ContinueListening && !fPendingDestroy )
{
// Blocking call
ListeningSocket->Listen();
// If we get here, we have gotten a new connection, accept it
Socket* NewSocket = ListeningSocket->Accept();
if( NewSocket != NULL && !fPendingDestroy )
{
Subsystem->mConnectionMutex.Aquire();
Subsystem->mNewConnections.AddItem( NewSocket );
Subsystem->mConnectionMutex.Release();
}
else if( NewSocket != NULL )
{
// CLEANUP, we allocated a socket while shutting down
delete NewSocket;
}
Sleep( 10 );
}
return 0;
}
示例6: NewConnectMonitor
void EpollFrame::NewConnectMonitor()
{
#ifndef WIN32
int nCount = MAXPOLLSIZE;
epoll_event *events = new epoll_event[nCount]; //epoll事件
int i = 0;
Socket listenSock;
Socket clientSock;
while ( !m_stop )
{
nCount = MAXPOLLSIZE;
if ( !((EpollMonitor*)m_pNetMonitor)->WaitConnect( events, nCount, -1 ) ) break;
for ( i = 0; i < nCount; i++ )
{
if ( ((EpollMonitor*)m_pNetMonitor)->IsStop(events[i].data.u64) )
{
delete[]events;
return;
}
listenSock.Detach();
listenSock.Attach((SOCKET)events[i].data.u64);
while ( true )
{
listenSock.Accept( clientSock );
if ( INVALID_SOCKET == clientSock.GetSocket() )
{
clientSock.Detach();
break;
}
OnConnect(clientSock.Detach(), false);
}
if ( !m_pNetMonitor->AddAccept( listenSock.GetSocket() ) )
{
printf( "AddAccept (%d) error \n", listenSock.GetSocket() );
listenSock.Close();
}
}
}
delete[]events;
#endif
}
示例7: Open
bool SocketCom::Open()
{
if (server)
{
#ifdef TRACE
printf("Opening server\n");
#endif
Socket listener;
if (!listener.Create()) return false;
#ifdef TRACE
printf("Create OK\n");
#endif
if (!listener.Bind(port)) return false;
#ifdef TRACE
printf("Binding OK\n");
#endif
if (!listener.Listen()) return false;
#ifdef TRACE
printf("Listening OK\n");
#endif
if (!listener.Accept(*this)) return false;
#ifdef TRACE
printf("Accept OK\n");
#endif
}
else
{
#ifdef TRACE
printf("Opening client %s:%d\n",host,port);
#endif
if (!Create()) return false;
#ifdef TRACE
printf("Create OK\n");
#endif
if (!Connect(host,port)) return false;
#ifdef TRACE
printf("Connect OK\n");
#endif
SetNonBlocking(true);
}
return true;
}
示例8: WaitAcceptEvent
void* EpollMonitor::WaitAcceptEvent( void *pData )
{
#ifndef WIN32
epoll_event *events = new epoll_event[m_nMaxMonitor]; //epoll事件
Socket sock;
Socket sockClient;
SOCKET hSock;
/* 等待有事件发生 */
while ( !m_bStop )
{
int nPollCount = epoll_wait(m_hEPollAccept, events, m_nMaxMonitor, -1 );
if ( -1 == nPollCount ) break;
/* 处理所有事件,nfds 为返回发生事件数 */
int i = 0;
for ( i = 0; i < nPollCount; i++ )
{
if ( m_epollExit == events[i].data.fd )
{
::closesocket(m_epollExit);
break;
}
sock.Detach();
sock.Attach(events[i].data.fd);
while ( true )
{
sock.Accept( sockClient );
if ( INVALID_SOCKET == sockClient.GetSocket() ) break;
sockClient.SetSockMode();
hSock = sockClient.Detach();
AddMonitor(hSock);
while ( !m_acceptEvents->Push((void*)hSock) ) m_sigWaitAcceptSpace.Wait();
m_ioSignal.Notify();
}
}
}
delete[]events;
#endif
return NULL;
}
示例9: main
//
// Main function
//
int main (int argc, char **argv) {
// Get hostname
std::string hostname = SystemCommandUtils::Exec("hostname");
std::cout << "Host name: " << hostname.c_str() << std::endl;
// Redirect cout to log file <programe name>.stdout;
char* app_name = argv[0];
std::stringstream ss;
ss << app_name << kLogExtension;
std::string log_filename = ss.str();
g_log_redirector = NULL;
std::ofstream log_strm(log_filename.c_str());
g_log_redirector = new StreamRedirector(std::cout, log_strm.rdbuf());
ss.str("");
ss << app_name << kErrExtension;
std::string err_filename = ss.str();
g_err_redirector = NULL;
std::ofstream err_strm(err_filename.c_str());
g_err_redirector = new StreamRedirector(std::cerr, err_strm.rdbuf());
std::cout << "Host name: " << hostname.c_str() << std::endl;
// Create job home path
std::string homepath = getenv("HOME");
g_home_job_dir = homepath + kJobDirName;
std::cout << "Home job: " << g_home_job_dir << std::endl;
if (argc < 2) {
std::cout << "kdeskdaemon <port no>\n";
delete g_err_redirector;
delete g_log_redirector;
return 1;
}
//
// Server socket
//
try {
// Create the socket
//ServerSocket server ( 30000 );
Socket server;
g_kdesk_port = atoi(argv[1]);
if (!server.Create() ) {
throw SocketException ( "Could not create server socket." );
}
if (!server.Bind(g_kdesk_port)) {
throw SocketException ( "Could not bind to port." );
}
std::cout << "bind done" << std::endl;
if ( ! server.Listen() ) {
throw SocketException ( "Could not listen to socket." );
}
std::cout << "Waiting for incoming connections..." << std::endl;
while ( true ) {
Socket* client_sock = new Socket();
std::cout << "before accept\n";
//server.accept ( new_sock );
if ( ! server.Accept(*client_sock)) {
delete client_sock;
throw SocketException ( "Could not accept socket." );
}
std::cout << "Connection accepted" << std::endl;
//ConnectionHandler((void*)&client_sock);
//continue;
pthread_t sniffer_thread;
//thrd_args->socket = &client_sock;
if (pthread_create( &sniffer_thread , NULL , ConnectionHandler , (void*)client_sock) < 0) {
//perror("could not create thread");
std::cout << "could not create thread\n";
continue;
}
//Now join the thread , so that we dont terminate before the thread
//pthread_join( sniffer_thread , NULL);
std::cout <<"Handler assigned" << std::endl;
}
} catch ( SocketException& e ) {
std::cout << "Exception was caught:" << e.description() << "\nExiting.\n";
}
if(g_log_redirector != NULL)
delete g_log_redirector;
if(g_err_redirector != NULL)
delete g_err_redirector;
return 0;
}
示例10: LinuxIO
bool STNetEngine::LinuxIO( int timeout )
{
#ifndef WIN32
int nCount = 0;
int eventType = 0;
int i = 0;
Socket sockListen;
Socket sockClient;
SOCKET sock;
map<SOCKET,int>::iterator it;
pair<map<SOCKET,int>::iterator,bool> ret;
//没有可io的socket则等待新可io的socket
//否则检查是否有新的可io的socket,有则取出加入到m_ioList中,没有也不等待
//继续进行m_ioList中的socket进行io操作
if ( 0 >= m_ioList.size() ) nCount = m_pNetMonitor->WaitEvent( timeout );
else nCount = m_pNetMonitor->WaitEvent( 0 );
if ( 0 > nCount ) return false;
//加入到m_ioList中
for ( i = 0; i < nCount; i++ )
{
sock = m_pNetMonitor->GetSocket(i);
if ( INVALID_SOCKET == sock ) return false;//STEpoll已关闭
if ( m_pNetMonitor->IsAcceptAble(i) )//连接类型直接执行业务
{
while ( true )
{
sockListen.Detach();
sockListen.Attach(sock);
sockListen.Accept( sockClient );
if ( INVALID_SOCKET == sockClient.GetSocket() ) break;
sockClient.SetSockMode();
OnConnect(sockClient.Detach(), false);
}
continue;
}
//不是监听socket一定是io事件
//加入到io列表,统一调度
if ( m_pNetMonitor->IsWriteAble(i) ) eventType = 1|2;//recv+send事件
else eventType = 1;//recv事件
ret = m_ioList.insert(map<SOCKET,int>::value_type(sock,eventType) );//增加可io的对象
if ( !ret.second ) ret.first->second = ret.first->second|eventType;//设置新事件
}
//遍历m_ioList,执行1次io
for ( it = m_ioList.begin(); it != m_ioList.end(); it++ )
{
if ( 1&it->second ) //可读
{
if ( ok != OnData( it->first, 0, 0 ) ) //数据已读完或连接已断开
{
it->second = it->second&~1;//清除事件
}
}
if ( 2&it->second ) //可写
{
if ( ok != OnSend( it->first, 0 ) )//数据已经发送完,或socket已经断开,或socket不可写
{
it->second = it->second&~2;//清除事件
}
}
}
//将不可io的socket清除
it = m_ioList.begin();
while ( it != m_ioList.end() )
{
if ( 0 == it->second )
{
m_ioList.erase(it);
it = m_ioList.begin();
continue;
}
it++;
}
return true;
#endif
return false;
}