本文整理汇总了C++中TCPClient::getfd方法的典型用法代码示例。如果您正苦于以下问题:C++ TCPClient::getfd方法的具体用法?C++ TCPClient::getfd怎么用?C++ TCPClient::getfd使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TCPClient
的用法示例。
在下文中一共展示了TCPClient::getfd方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main()
{
signal(SIGPIPE, SIG_IGN);
char buf[BUFSIZ];
int clientCount = 0;
try
{
TCPServer server(8001);
int listenfd = server.getfd();
Epoll epoll;
// 将监听套接字注册到epoll
epoll.addfd(server.getfd(), EPOLLIN, true);
while (true)
{
int nReady = epoll.wait();
for (int i = 0; i < nReady; ++i)
// 如果是监听套接字发生了可读事件
if (epoll.getEventOccurfd(i) == listenfd)
{
int connectfd = accept(listenfd, NULL, NULL);
if (connectfd == -1)
err_exit("accept error");
cout << "accept success..." << endl;
cout << "clientCount = " << ++ clientCount << endl;
setUnBlock(connectfd, true);
epoll.addfd(connectfd, EPOLLIN, true);
}
else if (epoll.getEvents(i) & EPOLLIN)
{
TCPClient *client = new TCPClient(epoll.getEventOccurfd(i));
memset(buf, 0, sizeof(buf));
if (client->read(buf, sizeof(buf)) == 0)
{
cerr << "client connect closed..." << endl;
// 将该套接字从epoll中移除
epoll.delfd(client->getfd());
delete client;
continue;
}
cout << buf;
client->write(buf);
}
}
}
catch (const SocketException &e)
{
cerr << e.what() << endl;
err_exit("TCPServer error");
}
catch (const EpollException &e)
{
cerr << e.what() << endl;
err_exit("Epoll error");
}
}