C Board  

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

Reply
 
LinkBack Thread Tools Display Modes
Old 03-18-2009, 09:05 PM   #1
Registered User
 
Join Date: Dec 2008
Posts: 183
how to intilise a non blocking socket?

sorry guys for asking this stupid question but i cant find anything like how to intilise a non blocking all stuff i searched on google says some junk stuff and no code for it it would be great if someone showed a simple code with use of non blocking on it thanks .
lolguy is offline   Reply With Quote
Old 03-18-2009, 10:35 PM   #2
Registered User
 
carrotcake1029's Avatar
 
Join Date: Apr 2008
Posts: 309
You can use fcntl() to change a socket to be blocking or nonblocking.
Code:
result = fcntl(sck, F_GETFL, NULL);
//This sets the socket to nonblocking
result |= O_NONBLOCK;
Code:
result = fcntl(sck, F_GETFL, NULL);
//This sets the socket to blocking
result &= (~O_NONBLOCK);
Might also be a good idea to check if result < 0 from the call to fcntl, which indicates an error.

Edit: This is after you have actually created the socket (sck = socket())
carrotcake1029 is offline   Reply With Quote
Old 03-18-2009, 10:55 PM   #3
Registered User
 
Join Date: Dec 2008
Posts: 183
yes but i think that function is linux right ?not win?
lolguy is offline   Reply With Quote
Old 03-18-2009, 10:59 PM   #4
Registered User
 
carrotcake1029's Avatar
 
Join Date: Apr 2008
Posts: 309
So sorry, just making blind assumptions.

I think the equivalent would be basically the same thing but using ioctlsocket() with a FIONBIO flag.

msdn:
Code:
//-------------------------
// Set the socket I/O mode: In this case FIONBIO
// enables or disables the blocking mode for the 
// socket based on the numerical value of iMode.
// If iMode = 0, blocking is enabled; 
// If iMode != 0, non-blocking mode is enabled.
u_long iMode = 0;
ioctlsocket(m_socket, FIONBIO, &iMode);
carrotcake1029 is offline   Reply With Quote
Old 03-18-2009, 11:15 PM   #5
Resu Deretsiger
 
Nightowl's Avatar
 
Join Date: Nov 2008
Location: /dev/null
Posts: 185
Note that winsock is almost a direct clone of the BSD sockets library, which is what Linux uses . . .
__________________
Do as I say, not as I do . . .

Experimentation is the essence of programming. Just remember to make a backup first.

"I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

Questions posted by these guidelines are more likely to be answered.

Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.
Nightowl is offline   Reply With Quote
Old 03-19-2009, 08:13 PM   #6
Registered User
 
Join Date: Dec 2008
Posts: 183
Quote:
Originally Posted by carrotcake1029 View Post
So sorry, just making blind assumptions.

I think the equivalent would be basically the same thing but using ioctlsocket() with a FIONBIO flag.

msdn:
Code:
//-------------------------
// Set the socket I/O mode: In this case FIONBIO
// enables or disables the blocking mode for the 
// socket based on the numerical value of iMode.
// If iMode = 0, blocking is enabled; 
// If iMode != 0, non-blocking mode is enabled.
u_long iMode = 0;
ioctlsocket(m_socket, FIONBIO, &iMode);
then i can make connect normal ?
lolguy is offline   Reply With Quote
Old 03-19-2009, 08:22 PM   #7
Registered User
 
Join Date: Dec 2008
Posts: 183
anyways let me post my problem code in other thread . thanks for the help guys.
lolguy is offline   Reply With Quote
Old 03-20-2009, 12:18 AM   #8
Registered User
 
Join Date: Dec 2008
Posts: 183
oh i forgot WSAstartup stupid me
lolguy is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Nonblocking socket...blocking? zolom Networking/Device Communication 2 07-13-2009 12:06 PM
EAGAIN with write on a non blocking socket mynickmynick Networking/Device Communication 0 04-30-2009 09:59 AM
How to initialize a non blocking socket using only winsock library *DEAD* Networking/Device Communication 4 01-18-2008 07:03 AM
socket blocking problem killerbyte Linux Programming 2 05-21-2006 03:25 PM
Socket Blocking Trouble! LearningMan Windows Programming 6 01-09-2003 10:09 AM


All times are GMT -6. The time now is 04:50 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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