Nonblocking socket...blocking?

This is a discussion on Nonblocking socket...blocking? within the Networking/Device Communication forums, part of the General Programming Boards category; So far as I know, I've set up my nonblocking socket properly, but it still seems to block...or at least ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    1

    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!_KiFastSystemCallRet@0()
    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)...is there any kind of error or timeout state that would cause it to block?

    Code:
    class MJClient{
    protected:
    	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;
    public:
    	~MJClient()
    	{
    		for(int i=0; i<MSG_MAX; i++)
    			delete[] messages[i];
    		delete[] messages;
    	}
    	int setup(const char* addr="127.0.0.1")
    	{
    		printf("Connecting\n");
    
    		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");
    			exit(-1);
    		}
    
    
    		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");
    		if(connectMe())
    			exit(0);
    		
    		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();
    			if(error!=WSAEWOULDBLOCK)
    				printf("WSA Error %i", WSAGetLastError());
    			return;
    		}
            //NEVER GETS THIS FAR
         }
    };
    
    class TCPClient:public MJClient
    {
    protected:
    	int connectMe()
    	{
    		int err = connect(sockfd,(struct sockaddr*) res->ai_addr, sizeof(*(res->ai_addr)));
    		if(err==-1)
    		{
    			printf("connect failed %i\n", err);
    			err = WSAGetLastError();
    			printf("socket fd %i stoSockAddrlen %i\n", sockfd, sizeof(*(res->ai_addr))); 
    		}
    		return err;
    	};
    public:
    	TCPClient()
    	{
    		sock_type = SOCK_STREAM;
    	}
    //Other stuff
    };

  2. #2
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    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
    Posts
    137
    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, 01: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, 07:03 AM
  3. recv on nonblocking socket
    By l2u in forum Networking/Device Communication
    Replies: 4
    Last Post: 05-20-2006, 08:02 PM
  4. How to make a nonblocking receive socket?
    By Aidman in forum Windows Programming
    Replies: 3
    Last Post: 04-30-2003, 05:28 PM
  5. Socket Blocking Trouble!
    By LearningMan in forum Windows Programming
    Replies: 6
    Last Post: 01-09-2003, 10:09 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21