Пример #1
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);
    }
Пример #2
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;
    }
Пример #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 );
	}