Thanks jkr,
How'd that integrate into my code? - Also, WillI still be able to return a success / failure for the connect?
Cheers,
Dave
Main Topics
Browse All TopicsHi,
I've done enough googling on this and am about to go bald so figured i'd give in and ask here.
I'd like to make this non-blocking, to speed up the process, allowing me to specify a timeout. If that's not possible i'd like to thread it as a last resort to allow multiple connection attempts.
My code is as follows:
-------------------------
/*
* Make sure we're dealing with a valid socket.
*/
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
{
WSACleanup(); // Cleanup Winsock.
perror("Socket");
exit(-1);
}
SOCKADDR_IN addr; // Socket address information.
addr.sin_family = AF_INET; // Address family internet.
addr.sin_addr = *((LPIN_ADDR)*rh->h_addr_l
addr.sin_port = htons(port); // Port to connect on.
/*
* Try and connect to the port.
*/
if ((connect(sock,(struct sockaddr *) &addr, sizeof(addr))) == 0)
{
return 0;
}
else
{
return 1;
}
/*
* Close socket.
*/
close(sock);
-------------------------
If someone could point out what I need to use to make this non-blocking that'd be great. I'd rather not use Winsock2 stuff like WSAEVENT.
Cheers,
Dave
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
In which case, perhaps i'm missing something.
When connect() is called, there's a huge delay whilst the application / tcp stack tries to connect to given port. If that port is closed, or the target system is down there's a huge delay whilst it sits there trying to connect.
How can I prevent that delay? - If it doesn't connect instantly, timeout after a short period of time.
Business Accounts
Answer for Membership
by: jkrPosted on 2008-06-05 at 07:23:55ID: 21719776
You'll need the Winsock-specific APIs for non-blocking sockets and WSA_FLAG_OVERLAPPED in particular - see http://support.microsoft.c om/kb/1816 11 ("Socket overlapped I/O versus blocking/nonblocking mode") and http://www.codeproject.com /KB/IP/Sim pleIOCPApp .aspx ("A simple application using I/O Completion Ports and WinSock"):
Select allOpen in new window