winsock2 's recvfrom and recv both return extended error 10045 for bound UDP socket - why?

%3CLINGO-SUB%20id%3D%22lingo-sub-2197198%22%20slang%3D%22en-US%22%3Ewinsock2%20's%20recvfrom%20and%20recv%20both%20return%20extended%20error%2010045%20for%20bound%20UDP%20socket%20-%20why%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2197198%22%20slang%3D%22en-US%22%3E%3CP%3EI%20have%20some%20C%20code%20using%20winsock2%20%3A%3CBR%20%2F%3E%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20int%20fd%20%3D%20socket%20(%20AF_INET%2C%20SOCK_DGRAM%2C%20IPPROTO_UDP%20)%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20struct%20sockaddr_in%20sin%20%3D%20%7B%20...%20%7D%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20if%20(fd%20%3D%3D%20-1)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20_ERR_(%20...%20)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20...%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20if%20(%20bind%20(%20fd%2C%20(struct%20sockaddr*)%26amp%3Bsin%2C%20sizeof(struct%20sockaddr_in))%20!%3D%200%20)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20_ERR_(%20...%20)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20...%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20fd_set%20s_fds%2C%20s_e_fds%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20UL_t%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20bytes_available%3D0%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20register%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20U32_t%20s_n_fds%20%3D%200%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20do%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7B%20bytes_available%3D0%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20FD_ZERO(%26amp%3Bs_fds)%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20FD_SET(fd%2C%26amp%3Bs_fds)%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20FD_ZERO(%26amp%3Bs_e_fds)%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20FD_SET(fd%2C%26amp%3Bs_e_fds)%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20struct%20timeval%20tv%20%3D%20%7B%200%2C%201000%20%7D%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20s_n_fds%20%3D%20select(%201%2C%20%26amp%3Bs_fds%2C%20NULL%2C%20%26amp%3Bs_e_fds%2C%20%26amp%3Btv)%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20if%20(s_n_fds%20%26gt%3B%201)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20_ERR_(%20ERROR_SYSTEM%20%7C%20ERROR_FATAL%20%7C%20ERROR_LOG%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%2C%20ERR_ACT_RETURN%2C%20GetLastError()%20%2C%201%20%2C%20%22select%20failed%22%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20)%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20else%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20if%20(%20s_n_fds%20%3D%3D%201%20)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7B%20if%20(%20FD_ISSET(%20fd%2C%20%26amp%3Bs_fds%20)%20)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7Bmore_bytes%3A%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20r%20%3D%20ioctlsocket(%20fd%2C%20FIONREAD%2C%20%26amp%3Bbytes_available%20)%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20if%20(%20r%20!%3D%200%20)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%20_ERR_(%20ERROR_SYSTEM%20%7C%20ERROR_FATAL%20%7C%20ERROR_LOG%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%2C%20ERR_ACT_RETURN%2C%20GetLastError()%2C%201%2C%20%22ioctlsocket%20failed%22%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%26nbsp%3B%26nbsp%3B%20)%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20say(%22RD%3A%20%25u%22%2Cbytes_available)%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20if(%20bytes_available%20%26gt%3B%204)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7B%20struct%20sockaddr_in%20snsin%3D%7B0%7D%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20U32_t%20sin_len%20%3D%20sizeof(struct%20sockaddr_in)%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20r%20%3D%20recvfrom((SOCKET)%20fd%2C%20(char*)%20%26amp%3B_iob_.b%5B0%5D%2C%20bytes_available%2C%20MSG_WAITALL%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20(struct%20sockaddr*)%26amp%3Bsnsin%2C%20%26amp%3Bsin_len%20)%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20say(%22RCV%3A%20%25u%20%25u%20WSAE%3A%20%25u%22%2Cfd%2Cr%2CWSAGetLastError())%3B%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20if%20(%20r%20%26lt%3B%200%20)%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20_ERR_(...)%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20....%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%7D%20while%20(fd%20!%3D%20-1%20)%20%3B%3CBR%20%2F%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EThe%20above%20code%20prints%3A%3CBR%20%2F%3E%24%20.%5C%24%7BMY_EXE%7D%3CBR%20%2F%3E%24%7BMY_EXE%7D%5B9692%5D%3A%20RD%3A%20121.%3CBR%20%2F%3E%24%7BMY_EXE%7D%5B9692%5D%3A%20RCV%3A%20440%204294967295%20WSAE%3A%2010045.%3CBR%20%2F%3Esrc%5Clogio.exe%5B9692%5D%20%3A%20logio.c%3A115%40(main)%20%3A%20Error%3A%2010045%3A'The%20attempted%20operation%20is%20not%20supported%20for%20the%20type%20of%20object%20referenced.%3CBR%20%2F%3E'%20-%20recv%20failed.%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%3CBR%20%2F%3ESo%2C%20for%20the%20bound%20UDP%20socket%20'fd'%2C%20the%20select()%20succeeded%2C%20the%20ioctlsocket()%20succeeded%2C%20returning%3CBR%20%2F%3Ea%20'bytes_available'%20of%20121%20bytes%2C%20but%20any%20attempt%20to%20then%20call%20either%20'recv'%20or%20'recvfrom'%20with%20that%20'fd'%3CBR%20%2F%3Esocket%20then%20returns%20SOCKET_ERROR%20with%20extended%20error%20code%2010045%2C%20'Operation%20Not%20Supported'.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CP%3EPlease%2C%20can%20anyone%20enlighten%20me%20what%20the%20'Supported'%20way%20of%20reading%20a%20UDP%20packet%20is%20on%20Windows%2C%20if%20neither%20winsock2's%20'recv'%20nor%20its%20'recvfrom'%20can%20be%20used%20%3F%26nbsp%3B%20I%20would%20like%20to%20know%20the%20sender's%20address%2C%3CBR%20%2F%3Eso%20was%20using%20'recvfrom'%20.%20What%20method%20am%20I%20meant%20to%20be%20using%20to%20receive%20a%20single%20packet%20from%20a%20bound%20UDP%3CBR%20%2F%3Esocket%20%2C%26nbsp%3B%20and%20the%20address%20it%20was%20sent%20from%20%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2197283%22%20slang%3D%22en-US%22%3ERe%3A%20winsock2%20's%20recvfrom%20and%20recv%20both%20return%20extended%20error%2010045%20for%20bound%20UDP%20socket%20-%20why%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2197283%22%20slang%3D%22en-US%22%3EI%20have%20tried%20using%20recv%2C%20recvfrom%2C%20and%20ReadFile.%20recv%20and%20recvfrom%20return%20SOCKET_ERROR%20%2B%20Extended%20Error%2010045%2C%20while%20ReadFile%20returns%20error%2087%2C%3CBR%20%2F%3E'The%20parameter%20is%20incorrect'.%3CBR%20%2F%3EIt%20appears%20that%20somehow%20ioctlsocket(%20fd%2C%20FIONREAD%2C%20%26amp%3Bbytes_available)%20%2C%20though%20it%20returns%200%20(success)%20%2C%20and%20returns%20a%20positive%20number%20in%20'bytes_available'%2C%20is%20somehow%20making%20'fd'%20an%20invalid%20file%20handle%20%3F%3CBR%20%2F%3EAre%20there%20any%20known%20workarounds%2C%20or%20a%20good%20open-source%20replacement%20for%20WinSock2.DLL%20%3F%3C%2FLINGO-BODY%3E
Occasional Contributor

I have some C code using winsock2 :

     int fd = socket ( AF_INET, SOCK_DGRAM, IPPROTO_UDP );
     struct sockaddr_in sin = { ... };
     if (fd == -1)
       _ERR_( ... )
     ...
     if ( bind ( fd, (struct sockaddr*)&sin, sizeof(struct sockaddr_in)) != 0 )
      _ERR_( ... )
     ...
     fd_set s_fds, s_e_fds;
     UL_t
     bytes_available=0;
     register
     U32_t s_n_fds = 0;
     do
     { bytes_available=0;
       FD_ZERO(&s_fds);
       FD_SET(fd,&s_fds);
       FD_ZERO(&s_e_fds);
       FD_SET(fd,&s_e_fds);
       struct timeval tv = { 0, 1000 };
       s_n_fds = select( 1, &s_fds, NULL, &s_e_fds, &tv);
       if (s_n_fds > 1)
         _ERR_( ERROR_SYSTEM | ERROR_FATAL | ERROR_LOG
                , ERR_ACT_RETURN, GetLastError() , 1 , "select failed"
               );

       else

       if ( s_n_fds == 1 )
       { if ( FD_ISSET( fd, &s_fds ) )
         {more_bytes:
          r = ioctlsocket( fd, FIONREAD, &bytes_available );
          if ( r != 0 )
            _ERR_( ERROR_SYSTEM | ERROR_FATAL | ERROR_LOG
                    , ERR_ACT_RETURN, GetLastError(), 1, "ioctlsocket failed"
                  );
          say("RD: %u",bytes_available);
          if( bytes_available > 4)
          { struct sockaddr_in snsin={0};
            U32_t sin_len = sizeof(struct sockaddr_in);
            r = recvfrom((SOCKET) fd, (char*) &_iob_.b[0], bytes_available, MSG_WAITALL,

                        (struct sockaddr*)&snsin, &sin_len );
            say("RCV: %u %u WSAE: %u",fd,r,WSAGetLastError());
            if ( r < 0 )
             _ERR_(...);

           ....
     } while (fd != -1 ) ;


 

 


The above code prints:
$ .\${MY_EXE}
${MY_EXE}[9692]: RD: 121.
${MY_EXE}[9692]: RCV: 440 4294967295 WSAE: 10045.
src\logio.exe[9692] : logio.c:115@(main) : Error: 10045:'The attempted operation is not supported for the type of object referenced.
' - recv failed.
     
So, for the bound UDP socket 'fd', the select() succeeded, the ioctlsocket() succeeded, returning
a 'bytes_available' of 121 bytes, but any attempt to then call either 'recv' or 'recvfrom' with that 'fd'
socket then returns SOCKET_ERROR with extended error code 10045, 'Operation Not Supported'.

Please, can anyone enlighten me what the 'Supported' way of reading a UDP packet is on Windows, if neither winsock2's 'recv' nor its 'recvfrom' can be used ?  I would like to know the sender's address,
so was using 'recvfrom' . What method am I meant to be using to receive a single packet from a bound UDP
socket ,  and the address it was sent from ?

4 Replies
I have tried using recv, recvfrom, and ReadFile. recv and recvfrom return SOCKET_ERROR + Extended Error 10045, while ReadFile returns error 87,
'The parameter is incorrect'.
It appears that somehow ioctlsocket( fd, FIONREAD, &bytes_available) , though it returns 0 (success) , and returns a positive number in 'bytes_available', is somehow making 'fd' an invalid file handle ?
Are there any known workarounds, or a good open-source replacement for WinSock2.DLL ?
Aha! I have verified this is definitely a bug with 'ioctlsocket' .

I modified the example here :

https://www.binarytides.com/udp-socket-programming-in-winsock/

to simply call 'ioctlsocket(s, FIONREAD, &bytes_available) before calling recvfrom with
the positive 'bytes_available' result, and that call also then generates extended error 10045, whereas without the call to 'ioctlsocket' , and receiving BUFLEN, with a call to recvfrom, it works fine.

There is no documentation on either the 'recvfrom' page or on the 'ioctlsocket' pages stating that a call to ioctlsocket with FIONREAD with a bound UDP socket will make the socket parameter into an invalid socket - this is a BUG.
It is a BUG that ioctlsocket() cannot be used before a call to 'recv' or 'recvfrom'().

I will raise these as microsoft bugs (somehow), but is there any better (preferably open-source) alternative to using WinSock2 for BSD sockets programming on WIndows ? The WinSock2 library does not seem to be coded very reliably .

 

/*
	Simple UDP Server
*/

#include<stdio.h>
#include<winsock2.h>

#pragma comment(lib,"ws2_32.lib") //Winsock Library

#define BUFLEN 512	//Max length of buffer
#define PORT 8888	//The port on which to listen for incoming data

int main()
{
	SOCKET s;
	struct sockaddr_in server, si_other;
	int slen , recv_len;
	char buf[BUFLEN];
	WSADATA wsa;

	slen = sizeof(si_other) ;
	
	//Initialise winsock
	printf("\nInitialising Winsock...");
	if (WSAStartup(MAKEWORD(2,2),&wsa) != 0)
	{
		printf("Failed. Error Code : %d",WSAGetLastError());
		exit(EXIT_FAILURE);
	}
	printf("Initialised.\n");
	
	//Create a socket
	if((s = socket(AF_INET , SOCK_DGRAM , 0 )) == INVALID_SOCKET)
	{
		printf("Could not create socket : %d" , WSAGetLastError());
	}
	printf("Socket created.\n");
	
	//Prepare the sockaddr_in structure
	server.sin_family = AF_INET;
	server.sin_port = htons( 60514 );
        server.sin_addr.s_addr =htonl((127<<24)|1);
	//Bind
	if( bind(s ,(struct sockaddr *)&server , sizeof(server)) == SOCKET_ERROR)
	{
		printf("Bind failed with error code : %d" , WSAGetLastError());
		exit(EXIT_FAILURE);
	}
	puts("Bind done");

	//keep listening for data
        unsigned int bytes_available = 0;

	while(1)
	{
		printf("Waiting for data...");
		fflush(stdout);
		
		//clear the buffer by filling null, it might have previously received data
		memset(buf,'\0', BUFLEN);

                bytes_available = 0;
                if ( ioctlsocket(s, FIONREAD, &bytes_available) != 0 )
                {
			printf("ioctlsocket() failed with error code : %d" , WSAGetLastError());
			exit(EXIT_FAILURE);
                }

                if ( bytes_available > 0 )
                {
                  //try to receive some data, this is a blocking call
                  if ((recv_len = recvfrom(s, buf, bytes_available, MSG_WAITALL, (struct sockaddr *) &si_other, &slen)) == SOCKET_ERROR)
                  {
			printf("recvfrom() failed with error code : %d" , WSAGetLastError());
			exit(EXIT_FAILURE);
                  }
                  //print details of the client/peer and the data received
                  printf("Received packet from %s:%d\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port));
                  printf("Data: %s\n" , buf);
                }else
                  Sleep(1000);
	}

	closesocket(s);
	WSACleanup();
	
	return 0;
}

@JVD66 

This program prints:

Compiled with normal $INCLUDE and $LIB set from vcvarsall.bat :

$ cl winsock_ex.c /link /NODEFAULTLIB:MSVCRT shell32.lib Ws2_32.lib kernel32.lib
Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29337 for x86
Copyright (C) Microsoft Corporation. All rights reserved.

winsock_ex.c
Microsoft (R) Incremental Linker Version 14.28.29337.0
Copyright (C) Microsoft Corporation. All rights reserved.

/out:winsock_ex.exe
/debug
/NODEFAULTLIB:MSVCRT
Synchronization.lib
shell32.lib
Ws2_32.lib
kernel32.lib
winsock_ex.obj


$ ./winsock_ex.exe

Initialising Winsock...Initialised.
Socket created.
Bind done
Waiting for data...Waiting for data...Waiting for data...Waiting for data...Waiting for data...Waiting for data...Waiting for data...Waiting for data...Waiting for data...Waiting for data...Waiting for data...Waiting for data...Waiting for data...recvfrom() failed with error code : 10045

Commenting out the bit where it calls ioctlsocket() allows it to recvfrom() OK.
Looks like a bug to me.