gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

DS development > DsWiFiLib + non-blocking sockets connection problem

#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.

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?

#164874 - Quirky - Fri Nov 28, 2008 1:26 pm

You have to check the return value and errno. If connect returns -1, and errno is EINPROGRESS, then it is trying to connect asynchronously. Here's the relevant bit from the man page...

Quote:

If the connection cannot be established immediately and O_NONBLOCK is set for the file descriptor for the socket, connect() shall fail and set errno to [EINPROGRESS], but the connection request shall not be aborted, and the connection shall be established asynchronously. Subsequent calls to connect() for the same socket, before the connection is established, shall fail and set errno to [EALREADY].


EDIT: I should mention that that is from the Linux man page for connect. The dswifi connect is not *exactly* the same, but sticks pretty close to that description in practice.

#164879 - Cluster - Sat Nov 29, 2008 6:46 am

Great... I'll try it :)