void multicastServer() { char buffer[6]; DgramSocket sock; Address client; cout << "Dgram multicast server: Start" << endl; sock.open(); sock.setReusePort(true); sock.bind(AnyAddress(23344)); sock.join(Address("224.0.0.50")); sock.join(Address("224.0.0.52")); sock.join(Address("224.0.0.53")); sock.leave(Address("224.0.0.52")); do { sock.recvFrom(buffer,6,client); sock.sendTo(buffer,6,client); cout << client.getHost() << " " << buffer << endl; } while(strcmp(buffer,"_END_")); sock.close(); cout << "Dgram multicast server: OK" << endl; }
void broadcastServer() { char buffer[6]; DgramSocket sock; Address client; cout << "Dgram broadcast server: Start" << endl; sock.open(); sock.setReusePort(true); sock.bind(AnyAddress(23344)); sock.recvFrom(buffer,6,client); sock.sendTo(buffer,6,client); sock.close(); cout << "Dgram broadcast server: OK" << endl; }
void dgramServer() { char buffer[6]; DgramSocket sock; Address client; cout << "Dgram socket server: start" << endl; sock.open(); // reuse port. Optional and can cause problems with other apps sock.setReusePort(true); // bind socket to port 23344 sock.bind(AnyAddress(23344)); // read from client sock.recvFrom(buffer,6,client); // replay to client sock.sendTo(buffer,6,client); sock.close(); cout << "Dgram socket server: OK" << endl; }
void ClusterServer::acceptClient(void) { BinaryMessage msg; DgramSocket serviceSock; SocketAddress addr; std::string service; std::string connectionType; UInt32 readable; bool connected=false; std::string address; bool bound = false; SINFO << "Waiting for request of " << _serviceName << std::endl; try { if(!_requestAddress.empty()) { // create connection _connection = ConnectionFactory::the()->createPoint( _connectionType); if(_connection) { // set interface _connection->setInterface(_interface); // bind connection try { // bind to requested address _boundAddress = _connection->bind(_requestAddress); bound = true; } catch(...) { SINFO << "Unable to bind, use name as symbolic " << "service name" << std::endl; } } } serviceSock.open(); serviceSock.setReusePort(true); // join to multicast group if(!_serviceGroup.empty()) { SocketAddress groupAddress = SocketAddress(_serviceGroup.c_str(), _servicePort); if(groupAddress.isMulticast()) { SINFO << "wait for request on multicast:" << _serviceGroup << std::endl; serviceSock.bind(SocketAddress(SocketAddress::ANY, _servicePort)); serviceSock.join(SocketAddress(groupAddress)); } else { SINFO << "wait for request by broadcast:" << _serviceGroup << std::endl; serviceSock.bind(SocketAddress(groupAddress)); } } else { SINFO << "wait for request by broadcast" << std::endl; serviceSock.bind(SocketAddress(SocketAddress::ANY, _servicePort)); } while(!connected) { try { if(_connection) readable = serviceSock.waitReadable(.01); else readable = true; if(readable) { serviceSock.recvFrom(msg,addr); service = msg.getString(); connectionType = msg.getString(); SINFO << "Request for " << service << " " << connectionType << std::endl; if(service == _serviceName) { // remove old connection if typename missmaches if(_connection && _connection->getType()->getName() != connectionType) { delete _connection; _connection = NULL; } // try to create connection if(!_connection) { // create connection _connection = ConnectionFactory::the()->createPoint( connectionType); if(_connection) { // set interface _connection->setInterface(_interface); // bind connection _boundAddress = _connection->bind( _requestAddress); bound = true; } else { SINFO << "Unknown connection type '" << connectionType << "'" << std::endl; } } if(_connection) { msg.clear ( ); msg.putString(_serviceName ); msg.putString(_boundAddress); serviceSock.sendTo(msg, addr); SINFO << "Response " << connectionType << ":" << _boundAddress << std::endl; } } } } catch(SocketConnReset &e) { // ignore if there is a connection. This can happen, if // a client has send a request. The server has send an // answer meanwile the client has send a second request // the client gets the answer to the first request and // the server tries to send a second answer. The second // answer can not be delivered because the client has // closed its service port. This is a win-socket problem. SWARNING << e.what() << std::endl; // if there is no connection, then its a real problem if(!_connection) throw; } catch(OSG_STDEXCEPTION_NAMESPACE::exception &e) { SWARNING << e.what() << std::endl; } try { // try to accept if(bound && _connection && _connection->acceptGroup(0.2) >= 0) { connected = true; SINFO << "Connection accepted " << _boundAddress << std::endl; } } catch(OSG_STDEXCEPTION_NAMESPACE::exception &e) { SWARNING << e.what() << std::endl; } } serviceSock.close(); } catch(OSG_STDEXCEPTION_NAMESPACE::exception &) { throw; } }