Thread: unix network programming

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    14

    Arrow unix network programming

    i tried calling the connect() function but the problem is if there is no response then it just hangs. i was wondering if there is any way to set a timeout . if theirs not maybe someone knows a better function then connect(), which will try to set up a tcp connection, and has a timeout option. thanks.
    peace out

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Beej is your friend.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    14
    Originally posted by quzah
    Beej is your friend.

    Quzah.
    ive allready read it. i cant find any info on timeouts.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well spend a couple more seconds with your favourite search engine and you'll get something like this.

    Keywords used: C socket programming "time out"

    Not real hard.

    Basicly you use select().

    After all, that's what that final parameter is there for.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    9
    You could also setup a signal handler for SIGALRM using signal(2)

    and then use alarm(2) to send an alarm signal at a specified time.

    This will cause connect() to return with EINTR (INTERRUPTED)
    Last edited by jabrams; 08-05-2002 at 11:14 PM.

  6. #6
    Im back! shaik786's Avatar
    Join Date
    Jun 2002
    Location
    Bangalore, India
    Posts
    345
    See the manual pages on 'setsockopt()', under that there is an option for this TIMEOUT.

  7. #7
    Registered User quagsire's Avatar
    Join Date
    Jun 2002
    Posts
    60
    In section 6.2 of BeeJ's Guide to Network Programming you can find info on how to use timeouts with select.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by quagsire
    In section 6.2 of BeeJ's Guide to Network Programming you can find info on how to use timeouts with select.
    Yeah, but they "already read [all] that".

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Jul 2002
    Posts
    9
    See the manual pages on 'setsockopt()', under that there is an option for this TIMEOUT
    He requires a timeout for connect, this cannot be done by setting a socket option. I guess you're thinking of SO_RCVTIMEO and SO_SNDTIMEO -- they don't set any kind of timeouts for connect.

  10. #10
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    hi all,

    i'm trying to do the same thing.

    there is a lot of information out there for implementing a timeout on a connect by using either a select or using alarm signals.

    what are the arguments for each method i.e. portability, reliability etc.

    TIA, rotis23

  11. #11
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Timeout's on connect()'s isn't too hard.

    This book explains it well, but you have to buy/borrow it. However, the sample source code from it is available here which I suggest you download. The file to look at is connectto1.c which is contained within the zip file.

    Basically, it creates a socket, then sets it to non-blocking and calls connect(). If connect() fails, with errno == EINPROGRESS the connect is still happening, so you can use select() to wait a certain amount of time for the connect to finish. I've used this approx on *nix and Cygwin on Windows and it's work just fine.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  12. #12
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    thanks hammer, but i've gone for the alarm signal approach.

    its pretty simple (as i know signals) and seems less of a hack.

    Code:
            struct sigaction action;
    
            void timed_out(){}
    
            .....
            s = socket(PF_INET,SOCK_STREAM,0); //create socket
            if(s == -1)
                    return 
    
            //initialise server struct
            memset(&adr_srvr,0,sizeof adr_srvr);
            adr_srvr.sin_family = AF_INET;
            adr_srvr.sin_port = htons(atoi(srvr_port));
    	adr_srvr.sin_addr.s_addr = inet_addr(srvr_addr);
    
            //verify staright ip address or resolved ip address
    	if(adr_srvr.sin_addr.s_addr == INADDR_NONE)
    	{
    		hp = gethostbyname(ip);
    		if(!hp)
    			return "0 - Hostname does not exist.";
    		if(hp->h_addrtype == AF_INET)
    			adr_srvr.sin_addr.s_addr = inet_addr(inet_ntoa(*(struct in_addr *) hp->h_addr_list[0]));
    			if(adr_srvr.sin_addr.s_addr == INADDR_NONE)
    				return "0 - Bad IP address";
    	}
    	
            len_inet = sizeof adr_srvr;
    
            //initialise signal handler
    	bzero(&action, sizeof(action));
    	action.sa_handler = timed_out;
    	action.sa_flags = 0;
    
    	sigaction(SIGALRM, &action, 0);
    
    	alarm(5); //start alarm count - 5 secs
    
            z = connect(s,&adr_srvr, len_inet); //attempt to connect
    	alarm(0); //turn off alarm
    	if(z == -1)
            	return "0 - Cannot connect to host.";
    Last edited by rotis23; 11-22-2002 at 04:46 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to program in unix
    By Cpro in forum Linux Programming
    Replies: 21
    Last Post: 02-12-2008, 10:54 AM
  2. Setting up a Unix box
    By @nthony in forum Tech Board
    Replies: 6
    Last Post: 07-22-2007, 10:22 PM
  3. 3D Network Analysis Tool
    By durban in forum Projects and Job Recruitment
    Replies: 1
    Last Post: 11-08-2005, 06:33 PM
  4. WinXP Network Connections pop-up
    By DavidP in forum Tech Board
    Replies: 1
    Last Post: 10-02-2002, 05:36 PM
  5. About Unix Programming - Making a career desision
    By null in forum C Programming
    Replies: 0
    Last Post: 10-14-2001, 07:37 AM