Re: connect() issues
Re: connect() issues
- Subject: Re: connect() issues
- From: <email@hidden>
- Date: Wed, 26 Feb 2003 15:32:41 -0800
Looks like the connect() call blocks on having the OS call pppd. The only
way to avoid this locking up the main run loop is to run the connect() in
another thread. Is threading hard? Yes and no. The thread issues are vast,
and much better explained by other material. Cocoa has good thread control I
hear.
As for detecting if the network is connected or not, I'm not sure how
exactly, but I think the SystemConfiguration.framework will do it for you.
There is an example code called 'MoreSCF' in ADC, check it out and see if it
does what you want. It should give you a good starting point.
-ryan
email@hidden said:
>
I've got a Cocoa app that needs to connect to another site over the
>
internet. I use socket() and connect() to make the connection.
>
I rely on the user making sure the system is connected to the net
>
before running this code or having selected the "automatically
>
connect" option in the PPP system preferences. So I don't do anything
>
special to make sure they are connected first and I have two problems
>
with this:
>
>
o When the "automatic connect" option is selected, the call to
>
connect() does trigger the PPP software to attempt to dial.
>
However, it hangs while it does this which locks up the app
>
since it can never get back to the event loop. I'm not
>
too familiar with threads, so is there another way to do this
>
without causing the app to lock up? Or is the thread code easy?
>
>
o After PPP attempts to dial and realizes that it can't (the
>
phone line isn't plugged in), it gives up but the connect()
>
call doesn't return right away. It doesn't give up
>
for another 20-30 seconds. Any idea why and how fix this?
>
>
I'm appending the code that I use. Pretty basic UNIX stuff. But maybe
>
it's not the way I should do it in a Cocoa app?
>
>
Also, is there a way I can ask the system whether the machine is
>
already connected to the net? I have some information that I need to
>
download periodically but it's not time critical. So I don't want
>
to dial the phone just to get it. I'd like to ask the OS if there is
>
already a connection before I call connect() in this case.
>
>
Any help is appreciated...
>
>
Devon
>
>
-----------------
>
>
int
>
getsock(char *ip)
>
{
>
int sockfd;
>
struct sockaddr_in serv_addr;
>
>
bzero((char *) &serv_addr, sizeof(serv_addr));
>
serv_addr.sin_family = AF_INET;
>
serv_addr.sin_addr.s_addr = inet_addr(ip);
>
serv_addr.sin_port = htons(80);
>
>
if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
>
perror("socket()");
>
exit(errno);
>
}
>
>
if (connect(sockfd, (struct sockaddr*) &serv_addr, sizeof(serv_addr)) <
0) {
>
perror("connect()");
>
exit(errno);
>
}
>
>
return sockfd;
>
}
>
_______________________________________________
>
cocoa-dev mailing list | email@hidden
>
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
>
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.