Thread: problem in non blocking code

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    183

    problem in non blocking code

    the code keeps crashing i tried to know whish state it crashes in but didnt know
    Code:
    #include "inc.h"
    void connect()
    {
    	u_long iMode = 1;
    	SOCKET SITE;
    	SOCKADDR_IN serverinfo;
    	LPHOSTENT hostEntry;
    	hostEntry = gethostbyname("www.yahoo.com");	// Specifying the server by its name;
    	SITE=socket(AF_INET,//we gonna use internet
    		       SOCK_STREAM,//best effor socks
    			   IPPROTO_UDP);//we gonna use tcp protocol
    	//next is our strctre of stuff   
    
    	serverinfo.sin_family=AF_INET;
    
    	serverinfo.sin_addr = *((LPIN_ADDR)*hostEntry->h_addr_list);
    
    	serverinfo.sin_port=htons(80);
    
    	if(ioctlsocket(SITE,FIONBIO,&iMode)==INVALID_SOCKET)//we begin using our non blocking sockets
    	{
    		MessageBox(NULL,"FUNCTION FAILED","FAILURE",MB_OK);
    		exit(0);
    	}
    	connect(SITE,(SOCKADDR*)&serverinfo,sizeof serverinfo);
    }
    int WINAPI WinMain(IN HINSTANCE hInstance, IN HINSTANCE hPrevInstance, IN LPSTR lpCmdLine, IN int nShowCmd )
    {
    	connect();
    }

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Uhh, not sure if this is related to your error, but you have created a function (connect) that is already defined in winsock2.h, which I assume you have included.

    Also saw this while glancing over it:
    Code:
     IPPROTO_UDP);//we gonna use tcp protocol
    ya....

    Also since you made your socket nonblocking, you will never even know if you connected as you have nothing to block after you call connect(), your program will just terminate.
    Last edited by carrotcake1029; 03-19-2009 at 09:40 PM.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    i changed that to tcp but now how can i connect after i got it intilised?

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    also i thought that will just connect it wont like send or recv something ? but program just crash after running

  5. #5
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    404
    Put in some printf's or something to figure out where exactly the code is crashing. Then you will know immediately after which line of code there is a problem, and from there you can diagnose it yourself, or ask if you can't figure it out.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Where's the call to WSAStartup()?

    I'm betting that your call to gethostbyname() is returning NULL since you haven't called WSAStartup(). This is causing a crash when you try to do hostEntry->h_addr_list.

  7. #7
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    i did in main thats y before it crashed coz i didnt intilised it before

  8. #8
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    srry 1 more question does recv in non blocking same as blocking i mean it returns smaller or = to zero if it failed ?

  9. #9
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    The return value is the same. Keep in mind that recv will fail if the call would block.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 06:39 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 01:05 PM
  5. Replies: 5
    Last Post: 12-03-2003, 05:47 PM