本文整理汇总了C++中SockAddr::getSockAddr方法的典型用法代码示例。如果您正苦于以下问题:C++ SockAddr::getSockAddr方法的具体用法?C++ SockAddr::getSockAddr怎么用?C++ SockAddr::getSockAddr使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SockAddr
的用法示例。
在下文中一共展示了SockAddr::getSockAddr方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: init
bool MiniWebServer::init(const string &ip, int _port) {
port = _port;
SockAddr me;
if ( ip.empty() )
me = SockAddr( port );
else
me = SockAddr( ip.c_str(), port );
sock = ::socket(me.family, SOCK_STREAM, 0);
if ( sock == INVALID_SOCKET ) {
log() << "ERROR: MiniWebServer listen(): invalid socket? " << errno << endl;
return false;
}
prebindOptions( sock );
if ( ::bind(sock, me.getSockAddr(), me.addressSize) != 0 ) {
log() << "MiniWebServer: bind() failed port:" << port << " errno:" << errno << endl;
if ( errno == 98 )
log() << "98 == addr already in use" << endl;
closesocket(sock);
return false;
}
if ( ::listen(sock, 16) != 0 ) {
log() << "MiniWebServer: listen() failed " << errno << endl;
closesocket(sock);
return false;
}
return true;
}
示例2: run
void MiniWebServer::run() {
SockAddr from;
while ( 1 ) {
int s = accept(sock, from.getSockAddr(), &from.addressSize);
if ( s < 0 ) {
if ( errno == ECONNABORTED ) {
log() << "Listener on port " << port << " aborted." << endl;
return;
}
log() << "MiniWebServer: accept() returns " << s << " errno:" << errno << endl;
sleepmillis(200);
continue;
}
disableNagle(s);
RARELY log() << "MiniWebServer: connection accepted from " << from.toString() << endl;
accepted( s, from );
closesocket(s);
}
}