Why "recieved_bytes" is always smaller than 0?
Code:
#include <iostream>
#include <windows.h>
#pragma comment(lib, "wsock32.lib")
void main()
{
WSADATA WsaData;
if(WSAStartup(MAKEWORD(2,2),&WsaData) != NO_ERROR)
{
return;
}
int handle = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if(handle <= 0)
{
return;
}
sockaddr_in address;
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons((unsigned short)"44405");
if(bind(handle, (const sockaddr*) &address, sizeof(sockaddr_in)) < 0)
{
return;
}
DWORD nonBlocking = 1;
if(ioctlsocket(handle, FIONBIO, &nonBlocking) != 0)
{
return;
}
while(true)
{
unsigned char packet_data[256];
unsigned int maximum_packet_size = sizeof(packet_data);
typedef int socklen_t;
sockaddr_in from;
socklen_t fromLength = sizeof(from);
int received_bytes = recvfrom(handle, (char*)packet_data, maximum_packet_size, 0, (sockaddr*)&from, &fromLength);
if(received_bytes < 0)
{
break;
}
unsigned int from_address = ntohl( from.sin_addr.s_addr );
unsigned int from_port = ntohs( from.sin_port );
}
WSACleanup();
}

