#164872 - Cluster - Fri Nov 28, 2008 10:24 am
Hi. Sorry, my english is not so good :\
I can't understand how to use connect() function with non-blocking sockets.
connect() returns control immediately, becouse sock is non-blocking. But how can I know connected it or not? I tryed to use select() function, but failed.
When I'm using blocking sockets:
My program stops at connect() function until socket is connected... or until it can't connect. Sometimes it takes a very long time.
Function gethostbyname() can froze program too.
Usually i'm using blocking socket to connect to server, then i'm switching to non-blocking mode. Under windows i'm using WSAWait API functions. But how to code it correctly with Berkley sockets?
I can't understand how to use connect() function with non-blocking sockets.
Code: |
int sock;
int i = 1; sock = socket(AF_INET, SOCK_STREAM, 0); // Creating socket ioctl(IRC_Servers[ServerNum].sock, FIONBIO, &i); // switching to non-blocking mode connect(sock, (struct sockaddr *) &servaddr, sizeof(servaddr)); // Connecting to server |
connect() returns control immediately, becouse sock is non-blocking. But how can I know connected it or not? I tryed to use select() function, but failed.
When I'm using blocking sockets:
Code: |
int sock;
sock = socket(AF_INET, SOCK_STREAM, 0); // Creating socket connect(sock, (struct sockaddr *) &servaddr, sizeof(servaddr)); // Connecting to server... Program stops here for a while |
My program stops at connect() function until socket is connected... or until it can't connect. Sometimes it takes a very long time.
Function gethostbyname() can froze program too.
Usually i'm using blocking socket to connect to server, then i'm switching to non-blocking mode. Under windows i'm using WSAWait API functions. But how to code it correctly with Berkley sockets?