0

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Ivan M
  • 61
  • 9
  • 1
    Without even trying to look at the specific problem: don't do that. If you need data sent through a byte stream such as a socket, serialize it to an array of bytes for sending and deserialize it to correct type on reception. Any other way is just asking for bugs. – spectras Jun 05 '17 at 00:49
  • 1
    ...and check `read`'s return value. It can and will return less bytes than you asked for in many occasions. You **must** check you got all the data you need, otherwise you're just reading uninitialized memory. – spectras Jun 05 '17 at 00:53
  • 2
    Additionally, unless you can guarantee a homogeneous network, you should send binary long integers after converting them to network byte order (via htonl() ) and the receiver should convert them from network byte order into host byte order (via ntohl() ) before using it.... the same advise goes for short integers ( via htons() and ntohs() ). – TonyB Jun 05 '17 at 03:04
  • @TonyB It's even better to use fixed-width integer types (look in endian.h on Linux), because integer values aren't guaranteed to be the same size on different architectures. And see https://stackoverflow.com/questions/13413521/is-there-any-reason-not-to-use-fixed-width-integer-types-e-g-uint8-t for some interesting implications if `CHAR_BIT` isn't 8... Because fixed-width integer types such as `int32_t` aren't required to exist. – Andrew Henle Jun 05 '17 at 09:22
  • Point taken. However, as I said when sending binary data over a network, you must convert it from "host byte order" into "network byte order", before sending and from "network byte order" to "host byte order" upon receipt... whether you use "htonl(), htons(), ntohl(), and ntohs()" or one of the functions defined in Linux's endian.h or something 'home grown' it should be done. – TonyB Jun 06 '17 at 22:39

0 Answers0