I get segmentation fault when i read for the second time from the fifo in the infinite loop.I don't understand why and if i don't call the function servi(...) that creat a new that send data to client i don't get a segmentation fault.I really don't understand why.
This is the code.
#include <pthread.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <fcntl.h>
#define MAX_BYTES (300);
int lung_max_msg = 30;
int lung_min_chiave = 3;
int lung_max_chiave = 10;
char nome_server[30];
void* servi(char* nome_client,char* input_messaggio,char* input_chiave,char codec);
int main()
{
pthread_t pid;
char* nome_client;
int buf = MAX_BYTES;
char buffer[buf];
char* tmpcodec;
char codec;
char* input_messaggio;
char* input_chiave;
int numread;
int fd ;
strcpy(nome_server,"miafifo");
printf("Programma avviato......\n");
printf("Attesa dei client......\n");
int ret_val = mkfifo(nome_server,0777);
if((ret_val == -1) && (errno != EEXIST))
{
perror("Errore durante la creazione della fifo!");
exit(1);
}
printf("Ho creato la FIFO\n");
fd = open("miafifo",O_RDONLY);
printf("------Ho aperto la FIFO SERVER-------- \n");
int i = 0;
while(1)
{
printf("Ciclo: %d , sto per leggere...\n",i);
i++;
numread = read(fd,&buffer,buf);
if(numread < 0)
{
perror("Errore lettura buffer!\n");
exit(1);
}
printf("\nIl buffer contiene : \"%s\"\n",buffer);
if(strcmp(buffer,"") != 0)
{
printf("nuovo client..\n");
nome_client = strtok(buffer,"$");
input_messaggio = strtok(NULL,"$");
input_chiave = strtok(NULL,"$");
tmpcodec = strtok(NULL,"$");
codec = tmpcodec[0];
printf("Ho prelevato i dati...\n");
printf("Nome client = %s \n",nome_client);
printf("Ho letto tutti i dati\n");
printf("Primo client servito..\n");
}
strcpy(buffer,"");
}
printf("Sto chiudendo il server.....\n");
exit(0);
}
void codificatore(char codec,char* msg,char* chiave,char* ris,int lung_msg,int lung_chiave)
{
printf("Entro nella procedura codificatore()\n");
printf("\ncodec = %c ,msg = %s ,chiave = %s ,ris = %s ,lung_msg = %d ,lung_chiave = %d \n\n\n",codec,msg,chiave,ris,lung_msg,lung_chiave);
int maiuscolo;
int i,j;
for(i = 0,j=0; i < lung_msg;i++)
{
if(isupper(msg[i]))
{
maiuscolo = 1;
}
else
{
maiuscolo = 0;
}
msg[i] = tolower(msg[i]);
chiave[i] = tolower(chiave[i]);
if( msg[i] == ' ')
{
ris[i] = ' ';
}
else
{
if(chiave[j] != ' ')
{
if(codec == '1')
{
ris[i] = ( ((msg[i] - 96 ) + (chiave[j] - 96 )) % 26) + 96;
}
else
{
ris[i] = ( ((msg[i] - 96 ) - (chiave[j] - 96 )));
if(ris[i] < 1)
{
ris[i] = 26 + ris[i];
}
printf(" : %d,",ris[i] );
ris[i]+= 96;
}
}
else
{
ris[i] = ' ';
}
if(maiuscolo)
{
ris[i] = toupper(ris[i]);
}
j = (j + 1) % lung_chiave;
}
}
ris[lung_msg] = '\0';
}
void* servi(char* nome_client,char* input_messaggio,char* input_chiave,char codec)
{
printf("Etro in servi()\n");
char msg[31];
char chiave[11];
char ris[30];
int m;
char errore_input = '0';
char msg_errore[300];
int fb = open(nome_client,O_WRONLY);
printf("Ho aperto la FIFO per la scrittura.....\n");
int lung_chiave = strlen(input_chiave);
int lung_msg = strlen(input_messaggio);
char* tmperror[10];
strcpy(msg_errore,"");
if(lung_msg > lung_max_msg)
{
sprintf(tmperror,"%d",lung_max_msg);
strcat(msg_errore,"\n\nLunghezza massima messaggio = ");
strcat(msg_errore,tmperror);
errore_input = '1';
}
if(lung_chiave < lung_min_chiave)
{
sprintf(tmperror,"%d",lung_min_chiave);
strcat(msg_errore,"\n\nLunghezza minima chiave = ");
strcat(msg_errore,tmperror);
errore_input = '1';
}
if(lung_chiave > lung_max_chiave)
{
sprintf(tmperror,"%d",lung_max_chiave);
strcat(msg_errore,"\n\nLunghezza massima chiave = ");
strcat(msg_errore,tmperror);
errore_input = '1';
}
strcpy(msg,input_messaggio);
strcpy(chiave,input_chiave);
if(errore_input == '0')
{
printf("\nSto elaborando il msg!\n\n");
sleep(1);
codificatore(codec,msg,chiave,ris,lung_msg,lung_chiave);
printf("Risultato : ");
for(m = 0;m < lung_msg; m++)
{
printf("%c ",ris[m]);
}
}
if(errore_input == '1')
{
printf("\nERRORE:%s\n",msg_errore);
write(fb,"34$",3);
write(fb,msg_errore,strlen(msg_errore));
write(fb,"$",1);
}
else
{
write(fb,"27$",3);
write(fb,ris,strlen(ris));
write(fb,"$",1);
printf("\nRisultato inviato al client\n");
}
printf("\n-------------------------------------------------------------------------\n");
sleep(1);
}