Set the socket for non-blocking with FCNTL

xiaoxiao2021-03-06  15

first part------------------------------------------------ -------------------------------------------------- -----------------

How Would I Put My Socket in Non-Blocking Mode? From Andrew Gierth (Andrew@erlenStar.Demon.co.uk):

Technically, fcntl (soc, F_SETFL, O_NONBLOCK) is incorrect since it clobbers all other file flags. Generally one gets away with it since the other flags (O_APPEND for example) do not really apply much to sockets. In a similarly rough vein, You 10 Use Fcntl (SoC, F_Setfl, 0) to Go Back to Blocking Mode.

To do it right, use f_getfl to get the current flags, set or clear the o_nonblock flag, Then Use f_setfl to set the flags.

And Yes, The Flag Can Be Changed Either Way At Will.

From: jagadeesh code:

/ * set socket to non-blocking I / o * / sts = ioctl (ccp-> main_sock, fionbio, (char *) & one); if (sts) {setProderr (PE_TCPERROR, GEL_FATAL); Sprintf (Line, "IOCTL Main) Failed -% s ", strrror (errno)); tcpabort ();

From: Viswaroopan

Hi, I tried this and works great as non-blocking socket. I have a different problem is that, when I made it as non-blocking the accept () on the server comes out immediately with non-block error. Instead I want accept () To wait for Some Time (SET A TIMEOUT) BEFORE GIVING THAT I CAN SET The Timeout On Accept ().

Thanks in advance. Vish

From: Jonathan Rynd

This is normal for all socket nonblocking operations: if you call them, you should be prepared to handle 2 cases: 1, they succeed right away, 2, they 'fail' with the "EWOULDBLOCK" nonblocking error (it's not a real failure, it just means "we can not satisfy that right now '. You then have to create a FD_SET structure and use it as input to select () with the proper timeout. See the manpage for select. Depending on the call, when Select () Returns to indeicate success, you may need to make the call again.from:

sorry, but a little more source code would help me more cause i do know nothing about ioctl and need a nonblocking socket ... cause i want to have a read, that doesent stop the whole program and, what about this stuff with select ( ) How do I get a buffer trory a recived package and give it to me gen i ask, if there is a new one, or say no, there is no new package, if there is none

From: jesus

aehrm forgot my email ... sorry, but a little more source code would help me more cause i do know nothing about ioctl and need a nonblocking socket ... cause i want to have a read, that doesent stop the whole program and, What about this stuff with select () How do i get a buffer trory a recived package and give it to me gen i ask, if there is a new one, or say no, there is no new package, if there is none

From: Michael Lampkin Added on: 2002-06-01 00:53:57

Since this is a common question ... The folow is sample code showing setting and un-setting for non-blocking on a socket.

CODE:

Int flags;

/ * SET SOCKET to NON-Blocking * /

IF ((Flags = FCNTL (Sock_Descriptor, f_getfl, 0)) <0) {/ * handle error * /}

IF (FCNTL (Socket_Descriptor, f_setfl, flags | o_nonblock) <0) {/ * handle error * /} / * set socket to blocking * /

IF ((Flags = FCNTL (Sock_Descriptor, f_getfl, 0)) <0) {/ * handle error * /}

IF (Fcnt1 (socket_descriptor, f_setfl, flags & (~ o_nonblock) <0) {/ * Handle Error * /}

the second part----------------------------------------------- -------------------------------------------------- --------

1) Connect timeout: 1) setsockopt (); // Set the socket to non-blocking mode; 2) Connect (); 3) Determine the return value of the connection (), the general situation returns -1, then you must also judge Errors If it is einprogress, then the connection is still continuing; if the error code is not the former, then there is a problem, do not have to be executed, you must turn off the socket; stay again; 4) SELECT (); set a function function Timeout, place the read and write items in Select (), in the timeout, if the select returns 1, that is, the description word is written, then the connection is successful; if it returns 2, that is, the word is described. Read can be written, then an error; if it returns 0, then timeout; ================================== ========== 2. Network interrupt: If your program is the client. Check the status of the descriptor with the SELECT, if you can read the RECV (), based on the return value of the RECV ();

/ ************************************************************** / / **** * On the eve of the king :: ** / / **** time: 2004.04.04 * / / **** Beijing Jin Wanwei Technology http://www.gnway.com ** / / ***** ***************************************************************************************************** can even on timeout limit nTimeOut seconds * / BOOL ConnectTest (char * m_server, int m_port) {struct hostent * host = NULL; struct sockaddr_in saddr; unsigned int s = 0; BOOL ret; time_t start; int error; host = GethostByname; if (host == null) return false; saddr.sin_family = AF_INET; saddr.sin_port = htons (m_port); saddr.sin_addr = * (Struct in_addr *) host-> h_addr); if ((((((((((((((((((((((((((); S = Socket (AF_INET, SOCK_STREAM, 0) <0) {Return False;} Fcntl (S, F_SETFL, O_NONBLOCK); IF (Connect (S, Struct SockAddr *) & Saddr, Sizeof (Saddr)) == -1 ) {If (errno == einprogress) {// it is in the connection process structure Struct TIMEVAL TV; FD_SET WRITEFDS; TV.TV_SEC = m_ntimeout; TV.tv_usec = 0; fd_zero (& WriteFDS); FD_SET (S, & WriteFDS); IF (SELECT (S 1, Null, & WriteFDS, NULL, & TV)> 0) { INT LEN = SIZEOF (int); // The following sentence must be, mainly for the firewall GetSockopt (S, SOL_Socket, SO_ERROR, & ERROR, & LEN); if (Error == 0) RET = true; else return = FALSE;

转载请注明原文地址:https://www.9cbs.com/read-45725.html

New Post(0)