0

I'm creating a concurrent UDP socket for a university project. The client waits for a stdin command, then creates a new process and a new socket with the function child_job. The server receives the command from the client, creates a new process that initializes a new socket and then tries to send a string back to client. The problem is that the first sendto of the server create the error:

Invalid argument

I am not figuring out why. Can anyone help me please?

Code of the client:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>


void child_job(int sockfd, struct sockaddr_in servaddr, char* buffer){

    int n = sendto(sockfd, buffer, sizeof(buffer), 0, (struct sockaddr*)&servaddr, sizeof(servaddr));
        if(n < 0){

            perror("Error while sending roba to server\n");
            exit(1);

        }

    printf("Connected to server\n");


    char bufferaccio[128];

    int length = sizeof(struct sockaddr);

    int m = recvfrom(sockfd, bufferaccio, sizeof(bufferaccio), 0, (struct sockaddr*)&servaddr, (socklen_t *)&length);

    if(m < 0){

        perror("Error while receiving from server\n");
        exit(1);

    }



    char stringa[128] = "I am a string from client";

    m = sendto(sockfd, stringa, strlen(stringa), 0, (struct sockaddr *)&servaddr, sizeof(servaddr));

    if(m < 0){

        perror("Error while sending stringa to server\n");
        exit(1);

    }

    exit(1);

}


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

    int sockfd;
    struct sockaddr_in str;

    if(argc < 3){

        fprintf(stderr, "Usage: %s portno ip\n", argv[0]);
        exit(1);

    }

    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if(sockfd < 0){

        perror("Error while creating socket\n");
        exit(1);

    }


    memset((void*)&str, 0, sizeof(str));
    str.sin_family = AF_INET;
    str.sin_port = htons(atoi(argv[1]));

    if(inet_pton(AF_INET, argv[2], &str.sin_addr)< 0){

        perror("Error while inet_pton\n");
        exit(1);

    }

    pid_t pid;

    char buffer[128];

    printf("Write something to send\n");
    scanf("%s", buffer);

    pid = fork();

    if(pid == 0){

        printf("I am a children with pid %d\n", getpid());

        sockfd = socket(AF_INET, SOCK_DGRAM, 0);
        if(sockfd < 0){

            perror("Error while creating socket in the child process\n");
            exit(1);

        }

        child_job(sockfd, str, buffer);



    }else if(pid < 0){

        perror("Error while creating child process\n");
        exit(1);

    }

    wait(NULL);

    exit(1);


}

Code of the server:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sys/types.h>

void initialize_newSocket(int* sock_fd, struct sockaddr_in *servaddr){

    int sockfd;

    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if(sockfd < 0){

        perror("Error while creating new socket\n");
        exit(1);

    }

    int ret = bind(sockfd, (struct sockaddr *)servaddr, sizeof(*servaddr));
    if(ret < 0){

        perror("Error while binding in son\n");
        exit(1);

    }

    *sock_fd = sockfd;


}


void child_server_job(){

    struct sockaddr_in addr;

    int sockfd;

    memset((void *)&addr, 0, sizeof(addr));

    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = INADDR_ANY;
    addr.sin_port = htons(0);

    initialize_newSocket(&sockfd, &addr);

    char string[128] = "I am a string";

    socklen_t fromlen = sizeof(addr);

    int n = sendto(sockfd, string, strlen(string), 0, (struct sockaddr *)&addr, fromlen);

    if(n < 0){

        perror("Error while sending something\n");
        exit(1);

    }

    int lenght = sizeof(struct sockaddr);


    n = recvfrom(sockfd, string, strlen(string), 0, (struct sockaddr *)&addr, (socklen_t *)&lenght);

    if(n < 0){

        perror("Error while receveing data from client\n");
        exit(1);

    }

    printf("I received this string %s\n", string);

    exit(1);

}


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

    if(argc < 2){

        fprintf(stderr, "Usage: %s portno\n", argv[0]);
        exit(1);

    }

    int sockfd;
    struct sockaddr_in addr;

    sockfd = socket(AF_INET, SOCK_DGRAM, 0);
    if(sockfd < 0){

        perror("Error while creating socket\n");
        exit(1);

    }

    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi(argv[1]));
    addr.sin_addr.s_addr = INADDR_ANY;

    int retBind = bind(sockfd, (struct sockaddr*)&addr, sizeof(addr));
    if(retBind < 0){

        perror("Error while binding socket\n");
        exit(1);

    }

    char bufferone[129];

    int length = sizeof(struct sockaddr);

    int n = recvfrom(sockfd, bufferone, sizeof(bufferone), 0, (struct sockaddr*)&addr, (socklen_t *)&length);

    if(n < 0){

        perror("Error while receiving roba from client\n");
        exit(1);

    }

    printf("Printing: %s\n", bufferone);

    pid_t pid = fork();

    if(pid == 0){

        printf("I am the child with pid %d\n", getpid());

        child_server_job(sockfd, addr);


        exit(1);


    }else if(pid < 0){

        perror("Error while forking new process\n");
        exit(1);

    }

    wait(NULL);

    exit(1);



}

Client output:

Write something to send
string
I am a children with pid 5756
Connected to server

Server output:

Printing: string
I am the child with pid 5757
Now it's my turn to send something
Error while sending something
: Invalid argument
Felipe Augusto
  • 7,733
  • 10
  • 39
  • 73
leop
  • 41
  • 7

1 Answers1

0

The function "child_server_job" does not use passed parameters!

change the child_server_job to give parameter and use the passed parameters.

Edit

void child_server_job(){

struct sockaddr_in addr;

int sockfd;

memset((void *)&addr, 0, sizeof(addr));

addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(0);

initialize_newSocket(&sockfd, &addr);

To:

void child_server_job(int* sock_fd, struct sockaddr_in *servaddr){

struct sockaddr_in addr = *servaddr;

int sockfd = *sock_fd;

/*memset((void *)&addr, 0, sizeof(addr));

addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(0);

initialize_newSocket(&sockfd, &addr); */

AND

child_server_job(sockfd, addr);

To:

child_server_job(&sockfd, &addr);
G. Emadi
  • 230
  • 1
  • 8
  • Just edited, if I comment from memset to initialize I have: "Address already in use", if I don't comment that I have the same "invalid argument" error – leop Aug 12 '18 at 16:42
  • This is because of your last run! after work with sockets you should close the socket to avoid this error in the future run. use "close(sockfd);" when you don't need socket any more. try running the program after waiting some seconds to giving time to the Operating System to free the address. – G. Emadi Aug 12 '18 at 16:54
  • If you run the client and the server at on the same host, be aware that you should not give both the same port number as an input. Use different ports! – G. Emadi Aug 12 '18 at 17:03