function exits alone

This is a discussion on function exits alone within the C Programming forums, part of the General Programming Boards category; Hi, i'm making a client-server file transfer program (using sockets) and i'm having a little issue with one function. The ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    11

    function exits alone

    Hi,
    i'm making a client-server file transfer program (using sockets) and i'm having a little issue with one function.

    The server asks the client for the name of the file he wants to upload, and the directory he wants to put it.

    After this, the server runs a function to see if everything is ok with the directory client said:
    Function:

    Code:
    int file_exist(const char *path,const char *file){	
    	DIR *dip;
            struct dirent *dit;
     
     	int ret=0;	
            
            if ((dip = opendir(path)) == NULL){	/
                    perror("Erro a aceder directoria");	
                    return -1;
            }
     
    
            while ((dit = readdir(dip)) != NULL){	
                    if(strcmp(dit->d_name,file)==0){	
                    	ret=1;	
                    }
            }
    
    
            if (closedir(dip) == -1){	
                    perror("Erro a fechar directoria");
                    return -1;
            }
    
            return ret;
    }
    if the 'path' is unreachable, opendir will fail e function will return -1.

    then the server's child process that is dealing with this communication, is running a function where this is done:

    Code:
    int existe;
       		
    existe=file_exist(caminho,buffer);	
    if(existe==-1){		
       	printf("Error reading path.1n");	
    }
    after this (in the case where opendir fails), the child's process function ends, and the child process obviously ends too...

    My question is:
    I'm not doing any exit() in this last 'if' condition...so why does the functions suddenly ends?


    Sorry for the big post...
    Thanks in advance

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,761
    > existe=file_exist(caminho,buffer);
    I think we need more code from around this point.

    For example, how you declare and initialise your two strings.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    11
    ok...i'll post the important parts...
    Server main process...creating child process to deal with more than one client:

    Code:
    if (pid == 0)  {
             				
                 		close(sockfd);	
                 		comunic(newsockfd);	/
                 		printf("closed client %s \n", inet_ntoa(cli_addr.sin_addr));	
                 		exit(0);
             	}


    Function 'comunic': (only some parts...function is big)

    Code:
    ...
    char buffer[256];
    char path[256];
       	memset(path,0,sizeof(path));
       	if(recv(sock,path,sizeof(path),0)<=0){
       		error("Error 'recv'. Connection lost");
       	}
    
    memset(buffer,0,sizeof(buffer));	
       
       	n = recv(sock,buffer,sizeof(buffer),0);	
       	if (n < 0) {
       		error("Erro a receber mensagem no socket");
       	}
       	else if(n==0){
       		error("Houve um shutdown por parte do cliente. Falha na ligação");
       	}
       	else{			
       	
       		int aux,existe;
       		
    		existe=file_exist(path,buffer);	
       		if(existe==-1){	
       			printf("opendir failed...path doesn't exist\n");	// 
       		}
    
                   aux=htons(existe);		
       		if(send(sock,&aux,sizeof(aux),0)<0){
       			error("Error send");
       		}
        ....
    .....
    ...
    .
    .

    the problem is that, this last send function, doesn't appear to be running...
    i'm doing a recv on client side, so client can realize that he writted a bad directory...that the directory wasn't found....
    and i'm doing a printf on client, to show this last info sent by server...but it doesn't show anything...it just ends right away

    client recv:

    Code:
    if(recv(sockfd,&estado,sizeof(int),0)<0){	
        	    			error("Error");
        	    		}
        	    		
        		    	estado=ntohs(estado);	
        		    	printf("%d \n",estado);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling C in Visual Studio 2005
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-16-2009, 04:25 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 04:06 PM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 07:54 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21