C Board  

Go Back   C Board > General Programming Boards > Networking/Device Communication

Reply
 
LinkBack Thread Tools Display Modes
Old 07-06-2009, 08:28 AM   #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
};
zolom is offline   Reply With Quote
Old 07-06-2009, 04:35 PM   #2
int x = *((int *) NULL);
 
Cactus_Hugger's Avatar
 
Join Date: Jul 2003
Location: Banks of the River Styx
Posts: 891
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)
Cactus_Hugger is offline   Reply With Quote
Old 07-13-2009, 12:06 PM   #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. ★
execute is offline   Reply With Quote
Reply

Tags
blocking socket winsock2

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 10:19 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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