   Why doesn't recvfrom() block in a thread?
  I'm trying to run recvfrom() in a thread, but it doesn't seem to block! Why is this?My listener thread looks like this:#include "stdafx.h"#include "UDPListener.h"#include <iostream>void UDPListener::Run(){while(1){m_MsgSock = m_UDPListenSock;m_ErrorCode = recvfrom(m_MsgSock, m_PacketBuffer, sizeof(m_PacketBuffer), 0, (struct sockaddr*)&m_From, (int*)sizeof(m_From));std::cout << "Server: Received datagram from: " << inet_ntoa(m_From.sin_addr);}}#include <winsock2.h>#include "ActiveObject.h"class UDPListener : public ActiveObject{public:UDPListener(SOCKET *UDPListenSock, sockaddr_in *From) : ActiveObject(){m_UDPListenSock = *UDPListenSock;m_From = *From;_thread.Resume();}~UDPListener(){Kill();}private:SOCKET m_UDPListenSock, m_MsgSock;struct sockaddr_in m_From;int m_ErrorCode;//Maximum allowed UDP packet size before it must be fragmented.static const int MAX_PACKET_SIZE = 496;char m_PacketBuffer[MAX_PACKET_SIZE];void InitThread() {}void Run();void FlushThread() {}};The abstract thread interface, ActiveObject, looks like so:#include "Thread.h"#include <windows.h>class ActiveObject{public:    ActiveObject ();    virtual ~ActiveObject () {}    void Kill ();protected:    virtual void InitThread () = 0;    virtual void Run () = 0;    virtual void FlushThread () = 0;    static DWORD WINAPI ThreadEntry (void *pArg);    int   _isDying;    Thread_thread;};#include "stdafx.h"#include "ActiveObject.h"// The constructor of the derived class// should call//    _thread.Resume ();// at the end of constructionActiveObject::ActiveObject (): _isDying (0),#pragma warning(disable: 4355) // 'this' used before initialized  _thread (ThreadEntry, this)#pragma warning(default: 4355){}void ActiveObject::Kill (){    _isDying++;    FlushThread ();    // Let's make sure it's gone    _thread.WaitForDeath ();}DWORD WINAPI ActiveObject::ThreadEntry (void* pArg){    ActiveObject * pActive = (ActiveObject *) pArg;    pActive->InitThread ();    pActive->Run ();    return 0;}Finally, the Thread class itself looks like this:#include "Windows.h"class Thread{public:    Thread ( DWORD (WINAPI * pFun) (void* arg), void* pArg)    {   _handle = CreateThread (  0, // Security attributes  0, // Stack size  pFun,  pArg,  CREATE_SUSPENDED,  &_tid);    }    ~Thread () { CloseHandle (_handle); }    void Resume () { ResumeThread (_handle); }    void WaitForDeath ()    {   WaitForSingleObject (_handle, 2000);    }private:    HANDLE _handle;    DWORD  _tid;// thread id};I really don't get why recvfrom() doesn't block. :( Any help appreciated!   
  How do you create the socket?What value does m_ErrorCode get, and what does WSAGetLastError() return afterwards?   ;  I'm not really sure what the result should be, nor what exactly is wrong. But since this involves C++, threaded networking and most importantly, the word Active Object, I would strongly recommend using boost's asio. Which is basically very robust active object framework.Also, always check return codes.   ;  Hmm!When checking the return code, it appears that recvfrom() is fucking up with a value of 10014 (according to WSAGetLastError()).Not really sure what that code means, but according to this topic, it'll... uhm... 'get one recvfrom() error per ICMP error you get from a packet you've sent, it should not keep being bad unless you keep sending messages to a bad port.'Thing is, I'm not sending any packets!I'm also getting almost the same IP as the guy in that thread: 205.205.205.205What could be wrong? :(Edit: This is how I configure the socket:Server::Server(u_short TCPPort, u_short UDPPort, const char *Address){int ErrorCode = 0;// Request Winsock version 2.2if(ErrorCode = WSAStartup(0x202, &m_WSAData) != 0){std::cout << "Server: WSAStartup failed with errorcode: " << ErrorCode << std::endl;WSACleanup();}elsestd::cout << "Server: WSAStartup was called successfully!" << std::endl;m_TCPLocal.sin_family = AF_INET;//Select any available IP address if Adress was NULL (or empty?).m_TCPLocal.sin_addr.s_addr = (!Address) ? INADDR_ANY:inet_addr(Address);//Make sure the port is in Network Byte Order.m_TCPLocal.sin_port = htons(TCPPort);m_TCPListenSock = socket(AF_INET, SOCK_STREAM, 0);m_UDPLocal.sin_family = AF_INET;//Select any available IP address if Adress was NULL (or empty?).m_UDPLocal.sin_addr.s_addr = (!Address) ? INADDR_ANY:inet_addr(Address);//Make sure the port is in Network Byte Order.m_UDPLocal.sin_port = htons(UDPPort);m_UDPListenSock = socket(AF_INET, SOCK_DGRAM, 0);if(m_TCPListenSock == INVALID_SOCKET){std::cout << "Server: Couldn't initialize TCP socket! Errorcode: " << WSAGetLastError() << std::endl;WSACleanup();}else if(m_TCPListenSock != INVALID_SOCKET)std::cout << "Server: Initialized TCP socket!" << std::endl;if(m_UDPListenSock == INVALID_SOCKET){std::cout << "Server: Couldn't initialize UDP socket! Errorcode: " << WSAGetLastError() << std::endl;WSACleanup();}else if(m_UDPListenSock != INVALID_SOCKET)std::cout << "Server: Initialized UDP socket!" << std::endl;if(bind(m_TCPListenSock, (struct sockaddr*) &m_TCPLocal, sizeof(m_TCPLocal)) == SOCKET_ERROR){std::cout << "Server: bind() failed on TCP socket with errorcode: " << WSAGetLastError() << std::endl;WSACleanup();}elsestd::cout << "Server: bind() called successfully on TCP socket!" << std::endl;if(bind(m_UDPListenSock, (struct sockaddr*) &m_UDPLocal, sizeof(m_UDPLocal)) == SOCKET_ERROR){std::cout << "Server: bind() failed on UDP socket with errorcode: " << WSAGetLastError() << std::endl;WSACleanup();}elsestd::cout << "Server: bind() called successfully on UDP socket!" << std::endl;if(listen(m_TCPListenSock, 1000) == SOCKET_ERROR){std::cout << "Server: listen() failed with error: " << WSAGetLastError() << std::endl;WSACleanup();}elsestd::cout << "Server: Called listen() successfully!" << std::endl << std::endl;std::cout << "Server: TCP ready for connections on port: " << TCPPort << std::endl;std::cout << "Server: UDP ready for connections on port: " << UDPPort << std::endl;m_UDPListener = new UDPListener(&m_UDPListenSock, &m_From);};  Couple of things...Firstly, recvfrom returns the number of bytes recieved, or SOCKET_ERROR if there's an error. If you're checking for that and then calling WSAGetLastError then good, but I dont see that in your code.That constructor of yours is terrible, there'e no way of knowing after creating it if you encountered an error except by looking at the console. You should not be calling WSAStartup and WSACleanup like that either. Wrap them up in a proxy object and initialize them once at program startup...struct MyWSAInitializer {    MyWSAInitializer() {   if(ErrorCode = WSAStartup(MAKEWORD(2,2), &WSAData_) != 0)  //Should have a fallback path here actually...  throw std::runtime_error("Unable to initialize WinSock 2.2.");}    ~MyWSAInitializer() {   WSACleanup();    }    WSADATA WSAData_;};;  Hm, yeah I didn't make that clear enough in the last post.After they suggested I check for an error, I did, and got SOCKET_ERROR from recvfrom() and errorcode 10014 from WSAGetLastError() with the IP being 205.205.205.205 (which is not my LAN or internet IP, I've checked)Edit: No idea what this particular errorcode means yet though! Except some stuff I've seen indicates that it might stem from calling recvfrom() with invalid parameters, but I'm fairly sure I'm not doing that.The only thing I'm a tiny but unsure of is the m_PacketBuffer, which is declared asstatic const int MAX_PACKET_SIZE = 496;char m_PacketBuffer[MAX_PACKET_SIZE];In C#, you'd have to go char[] m_PacketBuffer = new char[MAX_PACKET_SIZE] for it to be fully initialized, but in C++ arrays seems to be initialized on the fly (?).   ;  205.205.205.205 is 0xCDCDCDCD.Aka, uninitialized memory (debug).If that's the IP you're getting from recvfrom after getting SOCKET_ERROR, then you cannot trust it to be initialized to ANYTHING. As if it encountered an error during the receive or the socket is improperly configured, then out data from the function can be considered to be in an uninitialized state.   ;Quote:Original post by Washu205.205.205.205 is 0xCDCDCDCD.Aka, uninitialized memory (debug).If that's the IP you're getting from recvfrom after getting SOCKET_ERROR, then you cannot trust it to be initialized to ANYTHING. As if it encountered an error during the receive or the socket is improperly configured, then out data from the function can be considered to be in an uninitialized state.Come again...?Are you saying that the errorcode returned from WSAGetLastError might be incorrect?;  No, you say "with the IP being 205.205.205.205 (which is not my LAN or internet IP, I've checked)".That "IP address" is 0xCDCDCDCD. Aka: Uninitialized memory. If you're attempting to find out the IP address from the structure you passed to recvfrom, and you get an error, then the structure is in an unknown state, consider it to be invalid.In fact, consider all of the out parameters (unless otherwise specified by the error code documentation) to be in an unknown state.   ;  From winerror.h:Quote://// MessageId: WSAEFAULT//// MessageText:////  The system detected an invalid pointer address in attempting to use a pointer argument in a call.//#define WSAEFAULT    10014LProbably that 0xCECECECE thing you had there being a bad pointer, or something similar that is wrong with your input parameters. Check so that the correct socket is passed etc, place a breakpoint on the recvfrom call for example.   