Пример #1
0
	void Bind( const IpEndpointName& localEndpoint )
	{
		struct sockaddr_in bindSockAddr;
		SockaddrFromIpEndpointName( bindSockAddr, localEndpoint );
		
		// KEVIN: allow for multicast
		SetAllowReuse(true);
		struct ip_mreq group;
		if (localEndpoint.IsMulticastAddress()) {
			group.imr_interface.s_addr = htonl(INADDR_ANY);
			group.imr_multiaddr.s_addr = bindSockAddr.sin_addr.s_addr;
			if (setsockopt(socket_, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&group, sizeof(group)) < 0) {
				throw std::runtime_error("error adding multicast group\n");
				closesocket(socket_); // winsock2 equivalent of close()
				exit(1);
			}
		}
		bindSockAddr.sin_addr.s_addr = INADDR_ANY;
		// KEVIN: the above line needs to be added for winsock2, otherwise bind
		// will fail with error 10049 WSAEADDRNOTAVAIL
		// solution suggested from: http://www.sockets.com/err_lst1.htm#WSAEADDRNOTAVAIL
        
        if (bind(socket_, (struct sockaddr *)&bindSockAddr, sizeof(bindSockAddr)) < 0) {
            throw std::runtime_error("unable to bind udp socket\n");
        }

		isBound_ = true;
	}
Пример #2
0
	void Connect( const IpEndpointName& remoteEndpoint )
	{
		SockaddrFromIpEndpointName( connectedAddr_, remoteEndpoint );
       
        if (connect(socket_, (struct sockaddr *)&connectedAddr_, sizeof(connectedAddr_)) < 0) {
            throw std::runtime_error("unable to connect udp socket\n");
        }

		isConnected_ = true;
	}
Пример #3
0
	IpEndpointName LocalEndpointFor( const IpEndpointName& remoteEndpoint ) const
	{
		assert( isBound_ );

		// first connect the socket to the remote server
        
        struct sockaddr_in connectSockAddr;
		SockaddrFromIpEndpointName( connectSockAddr, remoteEndpoint );
       
        if (connect(socket_, (struct sockaddr *)&connectSockAddr, sizeof(connectSockAddr)) < 0) {
            throw std::runtime_error("unable to connect udp socket\n");
        }

        // get the address

        struct sockaddr_in sockAddr;
        memset( (char *)&sockAddr, 0, sizeof(sockAddr ) );
        socklen_t length = sizeof(sockAddr);
        if (getsockname(socket_, (struct sockaddr *)&sockAddr, &length) < 0) {
            throw std::runtime_error("unable to getsockname\n");
        }
        
		if ( isConnected_ ) {
			// reconnect to the connected address
			
			if (connect(socket_, (struct sockaddr *)&connectedAddr_, sizeof(connectedAddr_)) < 0) {
				throw std::runtime_error("unable to connect udp socket\n");
			}

		} else{
			// unconnect from the remote address
		
			struct sockaddr_in unconnectSockAddr;
			SockaddrFromIpEndpointName( unconnectSockAddr, IpEndpointName() );

			if( connect(socket_, (struct sockaddr *)&unconnectSockAddr, sizeof(unconnectSockAddr)) < 0 
					&& WSAGetLastError() != WSAEADDRNOTAVAIL ){
				throw std::runtime_error("unable to un-connect udp socket\n");
			}
		}

		return IpEndpointNameFromSockaddr( sockAddr );
	}
Пример #4
0
	void Bind( const IpEndpointName& localEndpoint )
	{
		struct sockaddr_in bindSockAddr;
		SockaddrFromIpEndpointName( bindSockAddr, localEndpoint );

        if (bind(socket_, (struct sockaddr *)&bindSockAddr, sizeof(bindSockAddr)) < 0) {
            throw std::runtime_error("unable to bind udp socket\n");
        }

		isBound_ = true;
	}
Пример #5
0
    void Bind( const IpEndpointName& localEndpoint )
    {
        struct sockaddr_in bindSockAddr;
        SockaddrFromIpEndpointName( bindSockAddr, localEndpoint );

        if (bind(socket_, (struct sockaddr *)&bindSockAddr, sizeof(bindSockAddr)) < 0) {
            std::string sErr = std::string("Unable to bind UDP socket. ") + 
                    strerror(errno);
            throw std::runtime_error(sErr.c_str());
        }

        isBound_ = true;
    }
Пример #6
0
  // oscit [
  int BoundPort() const {
		struct sockaddr_in localAddr;
		SockaddrFromIpEndpointName( localAddr, IpEndpointName() );
		socklen_t addrLen = sizeof(localAddr);

    // Ask getsockname to fill in this socket's local
    // address.
    if (getsockname(socket_, (struct sockaddr *)&localAddr, &addrLen) < 0) {
      throw std::runtime_error("unable to getsockname\n");
    }

    return ntohs(localAddr.sin_port);
  }
Пример #7
0
    IpEndpointName LocalEndpointFor(const IpEndpointName &remoteEndpoint) const
    {
        assert(isBound_);

        // first connect the socket to the remote server

        struct sockaddr_in connectSockAddr;
        SockaddrFromIpEndpointName(connectSockAddr, remoteEndpoint);

        if (connect(socket_, (struct sockaddr *)&connectSockAddr, sizeof(connectSockAddr)) < 0)
        {
            throw std::runtime_error("unable to connect udp socket\n");
        }

        // get the address

        struct sockaddr_in sockAddr;
        memset((char *)&sockAddr, 0, sizeof(sockAddr));
        socklen_t length = sizeof(sockAddr);
        if (getsockname(socket_, (struct sockaddr *)&sockAddr, &length) < 0)
        {
            throw std::runtime_error("unable to getsockname\n");
        }

        if (isConnected_)
        {
            // reconnect to the connected address

            if (connect(socket_, (struct sockaddr *)&connectedAddr_, sizeof(connectedAddr_)) < 0)
            {
                throw std::runtime_error("unable to connect udp socket\n");
            }
        }
        else
        {
            // unconnect from the remote address

            struct sockaddr_in unconnectSockAddr;
            memset((char *)&unconnectSockAddr, 0, sizeof(unconnectSockAddr));
            unconnectSockAddr.sin_family = AF_UNSPEC;
            // address fields are zero
            int connectResult = connect(socket_, (struct sockaddr *)&unconnectSockAddr, sizeof(unconnectSockAddr));
            if (connectResult < 0 && errno != EAFNOSUPPORT)
            {
                throw std::runtime_error("unable to un-connect udp socket\n");
            }
        }

        return IpEndpointNameFromSockaddr(sockAddr);
    }
Пример #8
0
    void Connect( const IpEndpointName& remoteEndpoint, bool multicast )
    {
        SockaddrFromIpEndpointName( connectedAddr_, remoteEndpoint );

        if (connect(socket_, (struct sockaddr *)&connectedAddr_, sizeof(connectedAddr_)) < 0) {
            throw std::runtime_error("unable to connect udp socket\n");
        }

        if( multicast ) {
            char enable = 1;
            setsockopt( socket_, SOL_SOCKET, SO_BROADCAST, &enable, sizeof(enable) );
        }

        isConnected_ = true;
    }
Пример #9
0
    void Bind( const IpEndpointName& localEndpoint )
    {
        struct sockaddr_in bindSockAddr;
        SockaddrFromIpEndpointName( bindSockAddr, localEndpoint );
        {
            IpEndpointName temp = IpEndpointNameFromSockaddr(bindSockAddr);
            char address[30];
            temp.AddressAndPortAsString(address);
        }
        if (bind(socket_, (struct sockaddr *)&bindSockAddr, sizeof(bindSockAddr)) < 0) {
            throw std::runtime_error("unable to bind udp socket\n");
        }

        isBound_ = true;
    }
Пример #10
0
	void Bind( const IpEndpointName& localEndpoint )
	{
		struct sockaddr_in bindSockAddr;
		SockaddrFromIpEndpointName( bindSockAddr, localEndpoint );

        if (localEndpoint.IsMulticastAddress()) {
            group.imr_interface.s_addr = htonl(INADDR_ANY);
            group.imr_multiaddr.s_addr = bindSockAddr.sin_addr.s_addr;
            if (setsockopt(socket_, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *)&group, sizeof(group)) < 0) {
                throw std::runtime_error("error adding multicast group\n");
                close(socket_);
            }
        }
        
        if (bind(socket_, (struct sockaddr *)&bindSockAddr, sizeof(bindSockAddr)) < 0) {
            throw std::runtime_error("unable to bind udp socket\n");
        }

		isBound_ = true;
	}