Michelangelo
Michelangelo

Reputation: 43

Strange characters during client/server comunication in C

I am programming a client/server application where the client, simply sends a string to the server and the server resends the same one. I run all like this: ./server localhost 8000 ./client localhost 8000 StringToSend

Server.c

#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>

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

    int sd, err, on, len;
    int GO = 1;
    struct addrinfo hints, *res;

    if(argc != 3){
        printf("errore\n");
    }


    memset(&hints, 0, sizeof(hints));

    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE;



    if ((err = getaddrinfo(argv[1], argv[2], &hints, &res)) != 0){
        fprintf(stderr, "Error bind: %s\n", gai_strerror(err));
        exit(EXIT_FAILURE);
    }

    if ((sd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0){
        perror("Error in socket");
        exit(EXIT_FAILURE);
    }

    on = 1;
    if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0){
        perror("setsockopt");
        exit(EXIT_FAILURE);
    }

    if (bind(sd, res->ai_addr, res->ai_addrlen) < 0){ 

        perror("Error in bind");
        exit(EXIT_FAILURE);
    }

    if(listen(sd, SOMAXCONN) < 0){  
      printf("error in listen\n");
    }

    int ns;

    while(GO){
    ns = accept(sd, NULL, NULL);

    }


    char* buff= malloc(100 *sizeof(char));

    read(sd, buff, sizeof(buff));

    write(sd, buff, sizeof(buff));

}

And the client.c

#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>

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

    int sd, err, on, len;
    struct addrinfo hints, *res;

    if(argc != 4){
         printf("errore\n");
    }

    memset(&hints, 0, sizeof(hints));

    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_flags = AI_PASSIVE;

    if ((err = getaddrinfo(argv[1], argv[2], &hints, &res)) != 0){
        fprintf(stderr, "Error after bind: %s\n", gai_strerror(err));
        exit(EXIT_FAILURE);
    }

    if ((sd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) < 0){
        perror("error when creating a socket");
        exit(EXIT_FAILURE);
    }

    if (connect(sd, res->ai_addr, res->ai_addrlen) == 0){
      printf("connected\n");
    }

    char* buff = malloc(100 * sizeof(char));

    buff = argv[3];

    write(sd, buff, sizeof(buff));

    char* risp = malloc(sizeof(char) * 100);

    read(sd, risp, sizeof(risp));

    puts(risp);

}

What happens is that the client receives a random string, probabilly because there are some problems with "\0" that I don't know how to fix, I've tried manually to stick the "\0" with strcat but nothing changed.

Upvotes: 0

Views: 46

Answers (1)

dash-o
dash-o

Reputation: 14452

You have to revise the memory allocation/read/write

Current code assume maximum buffer size of 100 character. Then issue a read for the size of the pointer (4 or 8 bytes). Then it write, unconditionally, 4 or 8 bytes to the client back.

char* buff= malloc(100 *sizeof(char));
read(sd, buff, sizeof(buff));
write(sd, buff, sizeof(buff));

You want the following: Read up to 100 character, write back the same number of characters (including the terminating NULL).

Server:

    int MSGSIZE = 100 ;
    char* buff= malloc(MSGSIZE *sizeof(char));
    while(GO){
        ns = accept(sd, NULL, NULL);
        int nread = read(ns, buff, MSGSIZE);
        if ( nread > 0 ) write(ns, buff, nread);
    }

Server:

    int MSGSIZE = 100 ;
    char* buff= malloc(MSGSIZE *sizeof(char));
    while(GO){
        ns = accept(sd, NULL, NULL);
        int nread = read(ns, buff, MSGSIZE);
        if ( nread > 0 ) write(ns, buff, nread);
    }

Client

    int MSGSIZE = 100 ;
    char* buff= malloc(MSGSIZE *sizeof(char));
    strcpy(buff, argv[3]) ;
    write(sd, buff, MSGSIZE) ;
    char* resp= malloc(MSGSIZE *sizeof(char));
    int nread = read(resp, buff, MSGSIZE);
    if ( nread > 0 ) puts(resp) ;

There are few other problems that you want to look at - closing the server socket, freeing the memory, etc. I hope the getting the read/write to work will help you move forward!

Upvotes: 1

Related Questions