If i understand it right, it's pretty nice, but the question is do i really understand the idea...
Printable View
If i understand it right, it's pretty nice, but the question is do i really understand the idea...
What part don't you understand?
I want to know if I got it right?!Quote:
Originally Posted by Devil Panther
You don't threads. That's too complicated. Look at those two links I posted on page 2.
On a blocking socket Connect will take up to 1 minute only when no response is received from the server machine. If the server machine is online but not running a server on that port it will respond with a connection refused reply ending the connect attempt. Connect reaches the timeout when no machine exists at the target IP or a firewall is blocking the Connection refused response.Quote:
Originally Posted by Yasir_Malik
Yes, because when the server is running but not listening on a specific port it will send a SYN | RST back to the client to quit the connection attempt, which makes connect to return a value.
So how can I simulate your situation?
try connecting to a none existant IP. On the internet you shouldn't send out connection attempts to some random IP but if you're on a local network, connect to an IP on your subnet that isn't assigned to anyone.
Do I have to be connect()ed (pun intended) to the Internet in order for connect() to block (I don't have an Internet connection at home)?
I think without any kind of network connection connect() returns imedatly with an error indicating the host can't be reached. Without a network connection I'm not sure how you can get connect() to wait the full timeout. Depending on your OS is may be possible to install firewall software to stop your machine from sending the connection refused reply on the loopback adapter and then use 12.0.0.1
Well, I lied: I have Internet on my Windows desktop, but not my NetBSD laptop (I have dialup). I sshed into my school server. Here's what I did. It's very simple:
All I'm doing is following that example in the link I posted. I didn't call signal() and alarm() again in alarm_handler() because I don't want another alarm and a signal to handle it.Code:#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <signal.h>
#include <setjmp.h>
#define SA struct sockaddr
#define MAX_SIZE 256
jmp_buf env;
void alarm_handler(int dummy)
{
printf("It's taking too long\n");
longjmp(env, 1);
}
int main()
{
struct sockaddr_in serv;
int sockfd;
char request[MAX_SIZE];
serv.sin_family = AF_INET;
serv.sin_addr.s_addr = inet_addr("123.123.123.123");
serv.sin_port = htons(7734);
if((sockfd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
{
perror("socket error");
exit(1);
}
signal(SIGALRM, alarm_handler);
alarm(5);
if(setjmp(env) != 0)
{
printf("Can't connect to server\n");
exit(1);
}
else if(connect(sockfd, (const SA *) &serv, sizeof(serv)) < 0)
{
perror("connect error");
exit(1);
}
return 0;
}
just fixing a typo, it should be 127.0.0.1, or any other 127.x.x.x, it's all the sameQuote:
loopback adapter and then use 12.0.0.1