Thread: Waiting for a new connection

  1. #1
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60

    Waiting for a new connection

    Hi all, i have a little problem, here there is a function:

    Code:
    /* the struct sockaddr_un sa is a global variable */
    channel_t openConnection(const char* path) {
    	channel_t ch;
    	int  count=5;
    	ec_null1( path, EINVAL )
    	if (strlen(path) > UNIX_PATH_MAX) {
    		errno=EINVAL;
    		return SOCKNAMETOOLONG;
    	}
    	if (0!=strncmp(sa.sun_path, path, UNIX_PATH_MAX))
    		strncpy(sa.sun_path, path, UNIX_PATH_MAX);
    	if (sa.sun_family!=AF_UNIX)
    		sa.sun_family=AF_UNIX;
    	ec_neg1( (ch=socket(AF_UNIX, SOCK_STREAM, 0)), 0 )
    	while (connect((int)ch, (struct sockaddr*)&sa, sizeof(sa))==-1) {
    		if (errno==ENOENT) {
    			if (count--==0) return -1;
    			sleep(1);
    			continue;
    		}
    		else return -1;
    	}
    	return ch;
    }
    That's the code a client executes in order to connect with a server.
    I would like it to try to connect at most 5 times, but if i start the client without start the server this is what happen:

    Code:
    Error during client execution: Connection refused
    "Connection refused" it's printed by perror().
    I'm a beginner with sockets, thanks in advance!

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Why do you want to try and connect five times? Normally, one time is enough, because connect will wait until a connection is made if possible, unless you have the socket set O_NONBLOCK. Even then, if everything has been done correctly and the server can complete the connection, one try is enough. Trying again in an immediate loop 2, 5, 10 or even 100 and 1000 times will not possibly make any difference! If you really insist on using this method, you should at least try and incorporate a time delay from 1/5-2 seconds so that if there is some change that can take place , it will have time to do so.

    But generally, if your first connect attempt fails, so will all the subsequent ones.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60

    Smile

    Hi, the thing is that i am doing an assignment, it consists in a server which waits for a new connection and once it's established a new thread is activated to handle the request, but when the first connection is closed by the client, the server loops into a wile calling the function below that fails setting errno to Invalid argument.

    Code:
    #define ec_neg1(N, ERR_VAL) if (N<0) { if(ERR_VAL!=0) errno=ERR_VAL; return -1; }
    
    channel_t acceptConnection(serverChannel_t s) {
    	ec_neg1( s-1, EINVAL )
    	ec_neg1( bind(s, (struct sockaddr*)&sa, sizeof(sa)), 0 )
    	ec_neg1( listen(s, SOMAXCONN), 0 )
    	return (channel_t)accept(s, NULL, 0);
    }
    At first i thought it was the first instruction setting errno to EINVAL, but is not.
    On the other side the client tryes to connect using the function in the first post, it succeeds only for the first connection, than when the server throws Invalid argument, it waits, and not for 5 seconds, so as you sayd the connect() function waits but without any good result!
    i'm pretty sure i am doing something in the wrong way! Please help me!

  4. #4
    gcc -Wall -pedantic *.c
    Join Date
    Jan 2009
    Location
    London
    Posts
    60
    Ok i solved the error in the server code. My mistake was to call the bind() in the function which accepts the new connection, instead of calling it in the function which initializes the socket!...Now it's working fine. The problem i still have is how to make the client wait for five attempts, even if the server doesn't start yet or it is establishing a connection with another client!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. multiple forks in one client connection (ftpclient)
    By Dynamo in forum Networking/Device Communication
    Replies: 5
    Last Post: 01-16-2011, 12:41 PM
  2. Winsock connection problem
    By Nephiroth in forum C++ Programming
    Replies: 2
    Last Post: 02-07-2006, 07:54 AM
  3. passing a connection to another process.
    By Kinasz in forum Networking/Device Communication
    Replies: 3
    Last Post: 10-09-2004, 05:34 PM
  4. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 07:18 AM
  5. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM