locked
Cannot receive UDP using winsock2 RRS feed

  • Question

  • I am a complete novice at programming in visual C++, but I am working on developing a plugin for a flight simulator that needs to receive UDP packets.  I have searched for many different examples, and followed all of them with no luck.  I really need some help understanding this.

    Basically, it seems that I set up my UDP socket right on the client side, but no matter what I cannot use the packet.  I verfied the packet is being sent with the correct data using WireShark, so I know the computer is at least receiving the packet.

    Here is my code:

    void frameRender(XPLMWindowID inWindowID, void* inRefcon)
    {
    	if(iResult != 0)
    	{
    		XPLMDrawString(color,200,700,"WSAStartup failed",(int*)50,xplmFont_Basic);
    	}
    	struct addrinfo *result = NULL, *ptr = NULL, hints;
    	ZeroMemory(&hints, sizeof(hints));
    	hints.ai_family = AF_INET;
    	hints.ai_socktype = SOCK_DGRAM;
    	hints.ai_protocol = IPPROTO_UDP;
    
    #define DEFAULT_PORT "35001"
    
    	iResult = getaddrinfo(NULL, DEFAULT_PORT, &hints, &result);
    	if(iResult != 0)
    	{
    		XPLMDrawString(color,200,700,"getaddrinfo failed",(int*)50,xplmFont_Basic);
    		WSACleanup();
    	}
    
    	SOCKET ConnectSocket = INVALID_SOCKET;
    
    	ptr = result;
    
    	ConnectSocket = socket(ptr->ai_family,ptr->ai_socktype,ptr->ai_protocol);
    
    	if(ConnectSocket == INVALID_SOCKET)
    	{
    		XPLMDrawString(color,200,700,"Error at socket() failed",(int*)50,xplmFont_Basic);
    		freeaddrinfo(result);
    		WSACleanup();
    	}
    
    	iResult = connect(ConnectSocket, ptr->ai_addr, (int)ptr->ai_addrlen);
    	if(iResult == SOCKET_ERROR)
    	{
    		closesocket(ConnectSocket);
    		ConnectSocket = INVALID_SOCKET;
    	}
    
    	freeaddrinfo(result);
    
    	if(ConnectSocket == INVALID_SOCKET)
    	{
    		XPLMDrawString(color,200,700,"Unable to connect to server!",(int*)50,xplmFont_Basic);
    		WSACleanup();
    	}
    	
    #define DEFAULT_BUFLEN 60
    
    	int recvbuflen = DEFAULT_BUFLEN;
    
    	char recvbuf[DEFAULT_BUFLEN];
    
    	iResult = recv(ConnectSocket, recvbuf, recvbuflen, 0);
    	if(iResult > 0)
    		XPLMDrawString(color,200,700,recvbuf,(int*)50,xplmFont_Basic);
    	else if(iResult == 0)
    		XPLMDrawString(color,200,700,"Connection closed",(int*)50,xplmFont_Basic);
    	else
    		XPLMDrawString(color,200,700,"recv failed",(int*)50,xplmFont_Basic);
    
    	closesocket(ConnectSocket);
    	WSACleanup();
    }

    I also have

    iResult = WSAStartup(MAKEWORD(2,2), &wsaData);

    at the start of my program.

    Please help, I have been stuck on this for several days now.

    • Moved by Helen Zhao Thursday, May 17, 2012 5:59 AM (From:Visual C++ General)
    Tuesday, May 15, 2012 9:30 PM

All replies

  • Hi cawalton,

    According to your description, I'd like to move this thread to "Windows Desktop Winsock Kernel (WSK) Forum" for more efficient responses.

    Thanks for your understanding.
    Best regards,


    Helen Zhao [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, May 17, 2012 5:58 AM