Thread: Nonblocking socket...blocking?

  1. #1
    Registered User
    Join Date
    Jul 2009

    Question Nonblocking socket...blocking?

    So far as I know, I've set up my nonblocking socket properly, but it still seems to block...or at least when I debug it in VStudio, it never gets to the next line of code while in:
    ntdll.dll!_NtDeviceIoControlFile@40() + 0xc bytes
    mswsock.dll!_WSPRecv@36() + 0xd1 bytes
    ws2_32.dll!_recv@16() + 0x6f bytes
    ogrey2.exe!MJClient::read() Line 152 + 0x31 bytes
    Do you see anything wrong? When I write a toy program just using this code, it seems to read along happily (without the server sending anything) there any kind of error or timeout state that would cause it to block?

    class MJClient{
    	int32_t sockfd;
    	//struct sockaddr_in stSockAddr;
    	addrinfo *res;
    	int circlefront, circleend;
    	char circlebuffer[CIRCLE_SIZE+1];
    	char buffer[CIRCLE_SIZE+1];//convert to circle later
    	char** messages;//[MSG_MAX][MSG_SIZE];
    	//queue<string> messages;
    	int firstmsg;
    	int lastmsg;
    	char cantread;
    	int sock_type;
    		for(int i=0; i<MSG_MAX; i++)
    			delete[] messages[i];
    		delete[] messages;
    	int setup(const char* addr="")
    		WORD wVersionRequested = MAKEWORD(2, 2);
    		WSADATA wsaData;
    		WSAStartup(wVersionRequested, &wsaData);
    		sockfd = socket(PF_INET, sock_type, 0);
    		printf("Error %i\n", WSAGetLastError());
    		if(-1 == sockfd)
    			printf("cannot create socket");
    		addrinfo hints;
    		memset(&hints, 0, sizeof(hints));
    		hints.ai_family = PF_UNSPEC;
    		hints.ai_socktype = sock_type;
    		if(getaddrinfo(addr, "1303", &hints, &res))
    			printf("Invalid address");
    		u_long on = 1;
    		if(0!=ioctlsocket(sockfd, FIONBIO, & on))
    			//Something went wrong
    			printf("Failed to put socket into asynchronous mode\n");
    		messages=new char*[MSG_MAX];
    		for(int i=0; i<MSG_MAX; i++)
    			messages[i] = new char[MSG_SIZE];
    		circlefront = 0;
    		circleend = 0;
    		return 0;
    	void read()
    		int bytesread=recv(sockfd, buffer+circleend, CIRCLE_SIZE-circleend-1, 0);
    		if(bytesread<0)//NEVER GETS HERE
    			int error = WSAGetLastError();
    				printf("WSA Error %i", WSAGetLastError());
            //NEVER GETS THIS FAR
    class TCPClient:public MJClient
    	int connectMe()
    		int err = connect(sockfd,(struct sockaddr*) res->ai_addr, sizeof(*(res->ai_addr)));
    			printf("connect failed %i\n", err);
    			err = WSAGetLastError();
    			printf("socket fd %i stoSockAddrlen %i\n", sockfd, sizeof(*(res->ai_addr))); 
    		return err;
    		sock_type = SOCK_STREAM;
    //Other stuff

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Banks of the River Styx
    You code looks mostly fine, and when I convert this to Linux, it works fine.
    Show us the code that calls read(). I suspect you're calling read() in a loop, and I'm thinking that it's just infinitely looping -- recv() is returned EWOULDBLOCK.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  3. #3
    Registered User
    Join Date
    Apr 2006
    Definitely make sure you check the error messages if any with WSAGetLastError(), make sure you log and output everything to make sure you diagnosed the problem correctly.
    ★ Inferno provides Programming Tutorials in a variety of languages. Join our Programming Forums. ★

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to intilise a non blocking socket?
    By lolguy in forum Networking/Device Communication
    Replies: 7
    Last Post: 03-20-2009, 12:18 AM
  2. How to initialize a non blocking socket using only winsock library
    By *DEAD* in forum Networking/Device Communication
    Replies: 4
    Last Post: 01-18-2008, 06:03 AM
  3. recv on nonblocking socket
    By l2u in forum Networking/Device Communication
    Replies: 4
    Last Post: 05-20-2006, 07:02 PM
  4. How to make a nonblocking receive socket?
    By Aidman in forum Windows Programming
    Replies: 3
    Last Post: 04-30-2003, 04:28 PM
  5. Socket Blocking Trouble!
    By LearningMan in forum Windows Programming
    Replies: 6
    Last Post: 01-09-2003, 09:09 AM

Tags for this Thread