Thread: How select() works

  1. #31
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    If i understand it right, it's pretty nice, but the question is do i really understand the idea...
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  2. #32
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    What part don't you understand?

  3. #33
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    Quote Originally Posted by Devil Panther
    hmmm since connect blocks, the program "stops" on that call to connect. I'm guessting i need to use more than one thread / process to do that, no?
    I want to know if I got it right?!
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  4. #34
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    You don't threads. That's too complicated. Look at those two links I posted on page 2.

  5. #35
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by Yasir_Malik
    I would like to try it, but on my machine, connect() returns immediately when there isn't a server to connect to.
    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.

  6. #36
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    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.
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

  7. #37
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    So how can I simulate your situation?

  8. #38
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    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.

  9. #39
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    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)?
    Last edited by Yasir_Malik; 04-01-2005 at 08:25 PM.

  10. #40
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    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
    Last edited by Quantum1024; 04-01-2005 at 08:44 PM.

  11. #41
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    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:
    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;
    }
    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.
    Last edited by Yasir_Malik; 04-01-2005 at 09:09 PM.

  12. #42
    * Death to Visual Basic * Devil Panther's Avatar
    Join Date
    Aug 2001
    Posts
    768
    loopback adapter and then use 12.0.0.1
    just fixing a typo, it should be 127.0.0.1, or any other 127.x.x.x, it's all the same
    "I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe

    http://www.Bloodware.net - Developing free software for the community.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem in select() in a Multiprotocol client
    By zealot in forum Networking/Device Communication
    Replies: 2
    Last Post: 04-09-2009, 12:45 PM
  2. select and EINTR
    By mynickmynick in forum C Programming
    Replies: 6
    Last Post: 07-29-2008, 05:03 AM
  3. What would I use in place of select()
    By Overworked_PhD in forum Linux Programming
    Replies: 3
    Last Post: 06-21-2008, 12:50 PM
  4. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 04:42 PM
  5. scandir select function
    By dsl24 in forum C Programming
    Replies: 3
    Last Post: 04-12-2002, 10:58 AM