[C] Errore di segmentazione[ core dump creato ]

Linguaggi di programmazione: php, perl, python, C, bash e tutti gli altri.
Scrivi risposta
FRANCONETt0
Prode Principiante
Messaggi: 1
Iscrizione: giovedì 19 giugno 2014, 21:39

[C] Errore di segmentazione[ core dump creato ]

Messaggio da FRANCONETt0 »

Ciao a tutti,qualcuno potrebbe gentilmente spiegarmi perche questo codice .

Codice: Seleziona tutto


#include    <stdio.h>
#include    <stdlib.h>
#include    <string.h>
#include    <inttypes.h>
#include    <arpa/inet.h>
#include    <sys/stat.h>
#include    <unistd.h>
#include    <sys/socket.h>
#include    <sys/types.h>
#include <rpc/xdr.h>

#include "types.h"
#include "sockwrap.h"
#include "errlib.h"
#include "mysocket.h"

#define MSG_ERR        "-ERR\r\n"
#define MSG_OK         "+OK\r\n"
#define DEFAULT_PORT    2000

#define BUFLEN 64 


/*GLOBAL VARIABLE*/
char *prog_name;


/*PROTOTYPE*/
int MyReadLine(int  s,char * buf,int maxlen);
int init_passive_socket_TCP(int bklog,uint16_t lport_n);
uint32_t FileDimension(char *filename);
void Write_OK_Response(XDR xdr_sended);
void Send_ERR_Response(int s);
int Service(int s);



int main(int argc,char **argv)
{

	int	 		conn_request_skt;	/* socket where connections are accepted */
	int	 		s;			/*socket for trasmission*/ 
	uint16_t 		lport_n,lport_h;	/* port where the server listens (net/host byte ord resp.) */
	int	 		bklog = 10;	/* listen backlog */

 
        
        
		
	int	 		result;

	socklen_t		addrlen;
	struct sockaddr_in 	saddr, caddr;		/* server and client address structures */ 

        char bufferfile[BUFLEN];
        

 


/****XDR DATA*****/

char buf[BUFLEN];  //buffer in trasmissione
char rbuf[BUFLEN]; //buffer in ricezione


 


/****************/


 

	prog_name=argv[0];

	/* get server port number */
	lport_h = DEFAULT_PORT;
	if (argc > 1)
		if (sscanf(argv[1], "%" SCNu16, &lport_h)!=1) {
			printf("Invalid port number, using default\n");
			lport_h = DEFAULT_PORT;
		}
	lport_n = htons(lport_h);


	/* initialize server TCP passive socket */
	 
        conn_request_skt = init_passive_socket_TCP(bklog,lport_n);
 

 


for (;;)
{      
                printf("Waiting for new connection\n");		 
		/* accept next connection */
		addrlen = sizeof(struct sockaddr_in);
		s = Accept(conn_request_skt, (struct sockaddr *) &caddr, &addrlen);		
		printf("New request received, associated socket: %u\n",s);

		/* serve the client on socket s */

		Service(s)

		
}

}


/* Gets a line of text from standard input after having printed a prompt string 
   Substitutes end of line with '\0'
   Empties standard input buffer but stores at most maxline-1 characters in the
   passed buffer
*/
int mygetline(char *line, size_t maxline, char *prompt)
{
	char	ch;
	size_t  i;

	printf("%s", prompt);
	for (i=0; i< maxline-1 && (ch = getchar()) != '\n' && ch != EOF; i++)
		*line++ = ch;
	*line = '\0';
	while (ch != '\n' && ch != EOF)
		ch = getchar();
	if (ch == EOF)
		return(EOF);
	else    return(1);
}






/* Writes nbytes from buffer ptr to stream socket s */
/*if all is ok return nbytes */

int writen_whit_send(SOCKET s, char *ptr, size_t nbytes)
{
    size_t nleft;
    ssize_t nwritten;

    for (nleft=nbytes; nleft > 0; )
    {
	nwritten = send(s, ptr, nleft, 0);
	if (nwritten <=0)
	    return (nwritten);
	else
	{
	    nleft -= nwritten;
	    ptr += nwritten;   
	}
    }
    return (nbytes - nleft);
}




int init_passive_socket_TCP(int bklog,uint16_t lport_n){

	struct sockaddr_in 	saddr;

	/* create the socket */	 
        int conn_request_skt = Socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
 

	/* bind the socket to any local IP address */
	memset(&saddr,0,sizeof(saddr));
	saddr.sin_family      = AF_INET;
	saddr.sin_port        = lport_n;
	saddr.sin_addr.s_addr = INADDR_ANY;
	
	Bind(conn_request_skt, (struct sockaddr *) &saddr, sizeof(saddr));
 	Listen(conn_request_skt, bklog);

        return conn_request_skt;
}


/*Semantica valore di ritorno Service

0 Voglio rimanere nel ciclo
1 Voglio uscire   dal ciclo
-1 ERRORE.

*/

int Service(int s){

char filename[64];
XDR xdr_received;
XDR xdr_sended;
clientmessage xdr_client_message;
command_server xdr_server_response;
FILE *fp;			 /*Pointer to file*/
uint32_t                size_of_file;    /*dimensione del file richiesto dal client*/


for (;;){      
          

FILE* fp_xdr_request=fdopen(s,"r");
xdrstdio_create(&xdr_received,fp_xdr_request,XDR_DECODE);

printf("i'am waiting for a new transfer request\n");

if(!xdr_clientmessage(&xdr_received,&xdr_client_message))
{
xdr_destroy(&xdr_received);
return -1;
}



printf("Decodifica effettuata con successo \n");

if(xdr_client_message.command!=1 && xdr_client_message.command!=0){    
printf("WRONG COMMAND received\n");      
Send_ERR_Response(s);  
continue;   
}               
			
	            
if(xdr_client_message.command==1) {     printf("QUIT COMMAND received\n"); break; }                 


if(xdr_client_message.command==0){      

/*GET COMMAND BEHAVIOR*/
printf("GET COMMAND received\n");           
/*Nel caso di GET received estraggo filename,negli altri casi sara irrilevante il suo contenuto*/
 
filename[0]='\0';
 

strcpy(filename,xdr_client_message.filename);    
 
xdr_destroy(&xdr_received);
 


printf("il file richiesto è %s = %s\n",filename,xdr_client_message.filename);
fflush(stdout);

 

size_of_file=FileDimension(filename);
  
/*CASO GETfilevuoto*/

if(size_of_file==0) {
printf("richiesto file vuoto");  
Send_ERR_Response(s);
continue;    
}




FILE *fp_xdr_response=fdopen(s,"w");
xdrstdio_create(&xdr_sended,fp_xdr_response,XDR_ENCODE);  
Write_OK_Response(xdr_sended);
fflush(fp_xdr_response);
                


printf("size_of_file--> %d\n",size_of_file); 

/*scrivo dim file  xdr buffer*/

if(!xdr_uint32_t(&xdr_sended,&size_of_file)) { xdr_destroy(&xdr_sended); return -1; }

printf("size_of_file scritt con successo --> %d\n",size_of_file);                         
	
fp=fopen(filename,"rb");  if(fp==NULL){ printf("errore apertura file ");  return 1;  }			


		 

int x;
                       
char tmpc;

do {
        x=fscanf(fp,"%c",&tmpc);
        printf("%c",tmpc);
	if(x>0) {
				 
					if(!xdr_u_char(&xdr_sended,&tmpc)){
                                         xdr_destroy(&xdr_sended); return -1;}


				}
			}while(x>0);


			
/*calcolo dim xdr buffer*/
 int len_client_message=xdr_getpos(&xdr_sended);
 
fflush(fp_xdr_response);
 xdr_destroy(&xdr_sended);

			
 printf("Trasferimento andato a buon fine \n");
fclose(fp);


}
           
}

}











uint32_t FileDimension(char *filename){

uint32_t                size_of_file;    /*dimensione del file richiesto dal client*/

struct stat bufs;			 /*struttura necessaria calcolo dimensioni file*/
	FILE *fp=fopen(filename,"rb");
	if(fp==NULL){ printf("errore apertura file ");  return 0;   }
	else{                     
	lstat(filename,&bufs);
        size_of_file=bufs.st_size;
       
  }

 fclose(fp);
return size_of_file;
}




void Send_ERR_Response(int s){

command_server xdr_server_response;
XDR xdr_sended;

xdr_server_response=1;

FILE *fp_xdr_response=fdopen(s,"w");
xdrstdio_create(&xdr_sended,fp_xdr_response,XDR_ENCODE);  

/*scrivo esito richiesta xdr buffer*/
if(!xdr_command_server(&xdr_sended,&xdr_server_response)) { xdr_destroy(&xdr_sended); exit(-1); }


fflush(fp_xdr_response);

xdr_destroy(&xdr_sended);
printf("Trasferimento non possibile,hai inserito un comando errato oppure");
printf(" il  file richiesto è :\ninesistente \n dimensioni nulle\n");


}


void Write_OK_Response(XDR xdr_sended){

command_server xdr_server_response;
xdr_server_response=0;




/*scrivo esito richiesta xdr buffer*/
if(!xdr_command_server(&xdr_sended,&xdr_server_response)) { xdr_destroy(&xdr_sended); exit(-1); }



 
printf(" il  file richiesto è  valido\n");


}


Genera un errore del tipo [C] Errore di segmentazione[ core dump creato ]?


La cosa strana è che mettendo la routine service direttamente nel main funziona tutto alla meraviglia, modularizzando il codice invece esce questo tipo di errore....

ps è un server che invia file ai suoi client a seguito di getnomefile .
Tutte le info sono inviate con xdr..

Ringrazie in ancitipo
CIAO!
Scrivi risposta

Ritorna a “Programmazione”

Chi c’è in linea

Visualizzano questa sezione: 0 utenti iscritti e 3 ospiti