byteWalrus
byteWalrus

Reputation: 825

Win32 C++ sendto() Returns Error 8 : WSA_NOT_ENOUGH_MEMORY

I have a program that detects keyboard inputs on my computer and sends them to my Android phone via UDP. The Android phone then takes the UDP packet and logs every key hit in a text file, but that's besides the point. I am currently using the standard Win32 API in order to send the packets via the sendto() function and WSAStartup. However, all of a sudden sendto() keeps returning an error of 8 which, according to MSDN, means WSA_NOT_ENOUGH_MEMORY. Now I have an idea of what that means, but I don't understand how to change my program to fix it, considering I changed nothing and it just started doing it. Restarting my computer has not helped. Here is what the UDP packet send function looks like. Maybe you guys can help me. Note that this is a thread function. Aren't threads dedicated a certain amount of memory according to the programmer? Could this be the issue?

Edit: This error is received on the PC SENDING the packets, not the phone. Wireshark will not pick up the packing leaving the PC, even though it worked before getting this error.

Thank you,

DWORD msgSender(LPVOID arg)
{
//Setup
size_t pkt_length = PACKET_LENGTH;
char pkt[PACKET_LENGTH];
sockaddr_in dest;
sockaddr_in local;
char inputAddr[] = "127.0.0.1";
char destAddr[] = "192.168.1.7";
WSACleanup();
WSADATA WsaData;
WSAStartup(MAKEWORD(2,2), &WsaData);

local.sin_family = AF_INET;
local.sin_addr.s_addr = inet_addr(inputAddr);
local.sin_port = 8081; // choose any

dest.sin_family = AF_INET;
dest.sin_addr.s_addr = inet_addr(destAddr);
dest.sin_port = htons(8080);

// create the socket
s = socket( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
// bind to the local address
bind( s, (sockaddr *)&local, sizeof(local) );

while(true)
{
        WaitForSingleObject(msgEvent, INFINITE);
        copyToPacket(keyStr, pkt);
        int ret = sendto( s, pkt, pkt_length, 0, (sockaddr*)&dest, sizeof(dest));
        keyReceived = false;
}
return 1;
}

void copyToPacket(string str, char* packet)
{
for(int i = 0; i < str.length() && i < PACKET_LENGTH; i++)
{
    packet[i] = str[i];
}
}

Upvotes: 0

Views: 764

Answers (1)

Steve
Steve

Reputation: 7271

The documentation for sendto states that:

If no error occurs, sendto returns the total number of bytes sent, which can be less than the number indicated by len. Otherwise, a value of SOCKET_ERROR is returned, and a specific error code can be retrieved by calling WSAGetLastError.

From what you've described, it looks like the Windows machine is sending 8 bytes successfully.

Upvotes: 1

Related Questions