I'm trying to return an int
value using sockets in C:
I use the following function to send some data and receive an operation result code: "res".
void enviarEmpleat(int sockServidor){
t_emp nouEmp;
int m, res;
system("clear");
int qtDpt;
if ((m = read (sockServidor, &qtDpt, sizeof(int))) < 0) perror ("read");
t_dpt llistaDepartaments[qtDpt];
if ((m = read (sockServidor, llistaDepartaments, sizeof(llistaDepartaments))) < 0) perror ("read");
printf("voy a hacer los reads con le qt %d",qtDpt);
int i =0;
for(i=0; i<qtDpt; i++){
printf("Informacío del departament %s amb codi %d:\n", llistaDepartaments[i].nom, llistaDepartaments[i].codi);
}
printf("Digues el nom del empleat: ");
scanf("%s", nouEmp.nom);
printf("Digues el DNI del empleat: ");
scanf("%s", nouEmp.dni);
printf("Digues el codi del departament del empleat: ");
scanf("%d", &nouEmp.codiDpt);
if ((m = write (sockServidor, &nouEmp, sizeof(t_emp))) < 0) perror ("write");
if ((m = read (sockServidor, &res, sizeof(int))) < 0) perror ("read");
if (res == -2) printf("Empleat correcte. Inserit a la llista d' empleats del servidor\n");
if ( res == -1) printf("Empleat INcorrecte. El departament no existeix\n");
if ( res == 0) printf("Empleat INcorrecte. El dni ja existeix\n");
printf("valor devuelto es %d \n",res);
sleep(2);
system("clear");
}
This function receives the data and sends the operation result "res" back to the client:
void inserirEmpleat(int sockClient){
t_emp nouEmp;
int m, r;
if ((m = write (sockClient, &qtDpt, sizeof(int))) < 0) perror ("write");
if ((m = write (sockClient, llistaDepartaments, sizeof(llistaDepartaments))) < 0) perror ("write");
if ((m = read (sockClient, &nouEmp, sizeof(nouEmp))) < 0) perror ("read");
r = buscarEmpleat(nouEmp);
int xx = -2;
if ((m = write (sockClient, &xx, sizeof(int))) < 0) perror ("write");
if ( r == -2 ){
llistaEmpleats[qtEmp] = nouEmp;
qtEmp++;
}
}
I'm testing with a result in which I clearly send back a "xx" value = -2 in "inserirEmpleat", but when I read in "enviarEmpleat" I get a "0" value all the times.