WinSock recvfrom



  • For some reason I'm getting a crash inside of recvfrom. This code worked before it was ported from a gcc project to an MSVC one. Anyone got any solutions? The socket seems to be valid and there is plenty of room in the buffer...



    EDIT: Wow that did not work. Time for pastebin



    http://pastebin.com/tS5sd4YZ



    Added code into the post. --Ling
    int NetUDPSocket_Create(uint16_t port)
    {
    	int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    	sockaddr_in address;	
    		unsigned long nonBlocking = 1;
    	if (sock < 0)
    	{
    		DDrConsole_PrintF("could not create socket");
    		return -1;
    	}
    
    memset(&client_address, 0, sizeof(sockaddr_in));
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = htonl(INADDR_ANY);
    address.sin_port =  htons(port);
    
    if (bind(sock, (sockaddr*)&address, sizeof(sockaddr_in)) &lt; 0)
    {
    	DDrConsole_PrintF("could not bind port %d", port);
    	return false;
    }
    
    
    if (ioctlsocket(sock, FIONBIO, &nonBlocking))
    {
    	DDrConsole_PrintF("failed to set non-blocking socket");
    	return false;
    }
    client_sock = sock;
    client_address = address;
    return sock;
    

    }

    bool NetUDPServer_Listen(uint16_t port, bool (packet_callback)(char data, int datalen, int from))
    {
    sockaddr_in address;
    UDPServer_Socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

    if (UDPServer_Socket &lt; 0)
    {
    	DDrConsole_PrintF("could not create socket");
    	return false;
    }
    
    
    memset(&address, 0, sizeof(sockaddr_in));
    address.sin_family = AF_INET;
    address.sin_addr.s_addr = htonl(INADDR_ANY);
    address.sin_port = htons(port);
    
    if (bind(UDPServer_Socket, (sockaddr*)&address, sizeof(sockaddr_in)) &lt; 0)
    {
    	DDrConsole_PrintF("could not bind port %d", port);
    	closesocket(UDPServer_Socket);
    	return false;
    }
    else {
    	char data[65537] = {0};
    	sockaddr_in from;
    	int recvlen;
    
    	for (;;)
    	{
    		int fromlen = sizeof(sockaddr_in);
    		memset(&from, 0, sizeof(sockaddr_in));
    

    //This line crashes 😞 ************************************
    recvlen = recvfrom(UDPServer_Socket, data, 65537 - 1, 0, (sockaddr
    )&from, (void
    )&fromlen);
    //
    ****************************************************
    if (!packet_callback(data, recvlen, ntohl(from.sin_addr.s_addr)))
    break;
    }
    closesocket(UDPServer_Socket);
    return true;
    }
    }



  • Well, it seems that multithreading has gotten the best of me. It was a separate thread crashing apparently.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.