Click here to Skip to main content
15,887,676 members
Please Sign up or sign in to vote.
4.20/5 (2 votes)
I am now trying to use Windows P2P native functions in my application to connect instances of it over the internet. For the testing, I've setup one application that uses PeerGraphCreate to establish a P2P graph and then registers some peer name using PeerPnrpRegister. I they register for messages using PeerGraphRegisterEvent and enter a loop while the application is listening for events in a thread. This side seems to work fine.

In the second application I open the graph using PeerGraphOpen which succeeds. I then resolve the peer name from the first app using PeerPnrpResolve. It returns two ipv6 addresses. However, when I feed any of those to the PeerGraphConnect function, it returns a HRESULT reading "Requested address is not valid in its context. I have no idea what's wrong, anyone would be so nice to provide a clue?

Here is the code of the second application for reference:
C++
HGRAPH hGraph;
HRESULT hr = PeerGraphOpen( L"TestP2PGraph", L"DebugPeer", L"TestPeerDB", NULL, 0, NULL, &hGraph );
if( hr == S_OK || hr == PEER_S_GRAPH_DATA_CREATED )
{
    // Connect to PNRP
    if( SUCCEEDED( PeerPnrpStartup( PNRP_VERSION ) ) )
    {
        ULONG numEndpoints = 1;
        PEER_PNRP_ENDPOINT_INFO* endpointInfo;
        hr = PeerPnrpResolve( L"0.TestBackgroundPeer", L"Global_", &numEndpoints, &endpointInfo );
        if( SUCCEEDED( hr ) )
        {

            PEER_ADDRESS addr;
            addr.dwSize = sizeof( PEER_ADDRESS );
            addr.sin6 = *((SOCKADDR_IN6*)endpointInfo->ppAddresses[1]);
            ULONGLONG connection;
            hr = PeerGraphConnect( hGraph, NULL, &addr, &connection );
            ^^ this reads "Requested address is not valid in its context


I would really appreciate any hint of what can be wrong. The networking side of things is really not my strongest one so I may be missing something basic.
Thanks

UPDATE: I got past this phase (somehow) but hit another roadblock - I describe it here: Failed to connect peer - invalid port settings?[^]. It is as well possible that the problem is not solved at all, sadly. For reference the HRESULT code of the error described here is 0x80072741 but occurs only with wPort being set to zero in the original peer registration (as I describe in the linked question).
Posted
Updated 30-Mar-13 1:25am
v2
Comments
Matthew Faithfull 28-Mar-13 17:08pm    
Interesting, I dodn't even know this API existed, can't believe I missed it unless its new. Anyhow I guess that's why no one has a clue so far. I notice 2 things though.

1. The error you're reporting doesn't seem to match the listed return values from the PeerGraphConnect. It should be one of:-

E_INVALIDARG One of the parameters is not valid.
E_OUTOFMEMORY There is not enough memory to perform a specified operation.
PEER_E_ALREADY_EXISTS A neighbor connection to a specified node already exists.
PEER_E_INVALID_GRAPH The handle to a peer graph is invalid.
PEER_E_NOT_INITIALIZED A graph must be initialized with a call to PeerGraphStartup before using this function.

If it's one of these then which one is important, if not then something is badly wrong.
I'd check that PeerPnrpStartup is a valid alternative to thelisted requirement to call PeerGraphStartup before calling PeerGraphConnect. If not then this call is missing and that seems like the most likely problem.
Ananta Dasa 28-Mar-13 17:39pm    
Hey Matthew.
Thanks for your reply.
Yes, this API is actually there since Win Vista I think, so its not completely new, but not very much known still.

As far as the return code goes, it is not any of those listed, which is strange. I read on MSDN that this kind of message HRESULT is returned from some WinSock functions but with no specific reasons attached (at least not connected to my problem here). I don't understand how he can successfully resolve a peer, return an address to it and then fail to connect.

And yes, I am calling the PeerGraphStartup function earlier in my code, this is just a snippet. Sorry I missed to mention that.

Regards
Matthew Faithfull 28-Mar-13 19:39pm    
OK in that case I'm not sure I can be of any further help but I suggest posting the actual hResult that is returned in hex form e.g. 0x08004500 so that if anyone does know what underlying socket issue can cause this then they may be able to help.
Given that the MSDN documentation is clearly missing at least one error case you may also want to try and get Microsoft support on this or at least comment on the MSDN page at

http://msdn.microsoft.com/en-us/library/windows/desktop/aa371100(v=vs.85).aspx
Ananta Dasa 29-Mar-13 4:49am    
Thanks for advice, I will try to find further help at Microsoft then. I will also update this question with the hex code of the HRESULT once I am at my computer as you suggested.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900