samedi 20 juillet 2019

Why am I getting a segmentation fault in the while loop in the client program?

I have written a server.cpp file, and a client.cpp file. I am supposed to go through a loop prompting for a car name and model, connect to the server, send a request to the server by writing to the socket, read the server reply and print it out on the screen. I keep getting a segmentation fault on the client.cpp file after it goes through the loop once, and I'm not sure why.

Here is the while loop where the problem is occurring on the client.cpp file:

while(boolQuit == false)
{
    printf("Enter a car make and model: ");
    getline(cin, makemodel);

    if(makemodel == "quit")
    {
        boolQuit = true;
        break;
    }

    bzero(buffer,301);
    fgets(buffer, 300, stdin);

    n = write(sockfd,buffer,strlen(buffer));
    if (n < 0) 
    {
        error("ERROR writing to socket");
    }
    bzero(buffer,301);
    n = read(sockfd,buffer,300);
    if (n < 0)
    {
        error("ERROR reading from socket");
    }
    printf("%s\n",buffer);
    //makemodel = " ";

}

Here is the error message I am getting on the client.cpp program:

Enter the server host name: localhost
Enter the server port number: 2300
Enter a car make and model: Toyota Camry
The high price for that car is $50000
Enter a car make a model: Segmentation fault

And the program ends.

Aucun commentaire:

Enregistrer un commentaire