本文整理汇总了C++中socket_t::setsockopt方法的典型用法代码示例。如果您正苦于以下问题:C++ socket_t::setsockopt方法的具体用法?C++ socket_t::setsockopt怎么用?C++ socket_t::setsockopt使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类socket_t
的用法示例。
在下文中一共展示了socket_t::setsockopt方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
//.........这里部分代码省略.........
}
if (listeningArgs.proto.compare("tcp")==0) {
//get the listener
if(listener.init(AF_INET, SOCK_STREAM, 0) == -1) {
makeLog(LOG_ERR,"Listener: Unable to create socket");
exit(1);
}
listener.peer.set_in(listeningArgs.host,listeningArgs.port);
if (listener.connect() < 0) {
makeLog(LOG_ERR,"Listener: Cannot connect: %s;",strerror(errno));
reconnector.add(&listener,reconnectListener,NULL);
} else {
// add the listener to the master set
listener.updatefdmax(&fdmax);
makeLog(LOG_INFO,"Listener: Connected to %s:%d;",listeningArgs.host.c_str(),listeningArgs.port);
}
FD_SET(listener.getHandle(), &master);
} else {
const bool mcast=((inet_addr(listeningArgs.host.c_str()) & MCAST_MASK) == MCAST_ADDR);
//get the listener
if(listener.init(AF_INET, SOCK_DGRAM, 0) == -1) {
makeLog(LOG_ERR,"Listener: Server-socket() error lol!");
terminate();
}
makeLog(LOG_INFO,"Listener: Server-socket() is OK...");
//"address already in use" error message
if(listener.setsockopt(SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
{
makeLog(LOG_ERR,"Listener: Setsockopt error: %s",strerror(errno));
terminate();
}
makeLog(LOG_INFO,"Listener: Setsockopt() is OK...");
// bind
listener.local.set_in(listeningArgs.host,listeningArgs.port);
if(listener.bind() == -1)
{
makeLog(LOG_ERR,"Listener: Unable to bind: %s",strerror(errno));
terminate();
}
if (mcast) {
ip_mreq imreq;
imreq.imr_multiaddr.s_addr = inet_addr(listeningArgs.host.c_str());
imreq.imr_interface.s_addr = INADDR_ANY; // use DEFAULT interface // JOIN multicast group on default interface
int status = listener.setsockopt(IPPROTO_IP, IP_ADD_MEMBERSHIP, (const void *)&imreq, sizeof(struct ip_mreq));
makeLog(LOG_INFO,"Listener: Joining multicast %s",listeningArgs.host.c_str());
}
makeLog(LOG_INFO,"Listener: ready udp://%s:%d",listeningArgs.host.c_str(),listeningArgs.port);
// add the listener to the master set
FD_SET(listener.getHandle(), &master);
listener.updatefdmax(&fdmax);
}
if(control.init(AF_INET, SOCK_DGRAM, 0) == -1) {
makeLog(LOG_ERR,"Control: Unable to create socket: %s",strerror(errno));