0

In my socket programming, i am to read input from a user on the client side on a loop until the user inputs an empty string, send it to server and have the server search through a text file to find something that matches the input from user.

I dont know what but something is wrong with the loop on the client side. Its a do while loop. It reads in once and gives the appropriate result(that even has a glitch) but then when the user gives an input a second time, it hangs. It does not send what the user just inputted to the server. It just hangs. CLIENT program:

void error(char *msg)
{
    perror(msg);
    exit(0);
}

int main(int argc, char *argv[])
{
    int sockfd, portno, n;
    struct sockaddr_in serv_addr;
    struct hostent *server;
    //char buffer[256];
    if (argc < 1) {
       fprintf(stderr,"usage %s hostname port\n", argv[0]);
       exit(0);
    }
    char str1[300]= " ";
    printf("Enter server port number: ");
    scanf("%s", str1);
    portno = atoi(str1);
    //create a socket point
    sockfd = socket(AF_INET, SOCK_STREAM, 0);
    if (sockfd < 0) {
        error("ERROR opening socket");
        exit(1);
    }
    char str[300]= " ";    
    printf("Enter server host name: ");
    scanf("%s", str);
    server = gethostbyname(str);
    if (server == NULL) {
        fprintf(stderr,"ERROR, no such host\n");
        exit(0);
    }  
    bzero((char *) &serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr, server->h_length);
    serv_addr.sin_port = htons(portno);
    //now connect to server
    if (connect(sockfd, (struct sockaddr*)&serv_addr,sizeof(serv_addr)) < 0)
    {
        error("ERROR connecting");
        exit(1);
    }
    //NOW ASK FOR MESSAGE FROM USER, read by server
    char buffer[256];
    char buffer2[256];
    do       //**i believe problem starts here**
    {
        printf("Enter a college major: ");
        bzero(buffer,256); 
        scanf("%s", buffer);
        //send message to server
        n = write(sockfd,buffer,strlen(buffer));
        if (n < 0) {
             error("ERROR writing to socket");
             exit(1);
        }
        //now read server response
        bzero(buffer,256);
        n = read(sockfd,buffer,256);
        if (n < 0)
        {
            error("ERROR reading from socket");
            exit(1);
        }
        printf("%s\n",buffer);
    } while(strcmp(buffer, " "));

    return 0;
    printf("Hello 1\n");
}

the input(College major) the user might put could have spaces, so the "scanf("%s", buffer);" does not work for those. This is what i meant by glitch. If someone puts in "Computer Science" only "Computer" is sent to the server. I am compiling this in linux so gets(buffer) is not allowed. I dont know what to do. Also is there a way to write this do-while loop as just a while loop. I want to terminate the program when the user inputs an empty string.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
Dees
  • 45
  • 1
  • 7
  • 2
    Try stepping through the code line by line in a debugger until you see where it "hangs". Perhaps the server doesn't reply? – Some programmer dude Apr 06 '18 at 03:44
  • On a somewhat related note, the `read` function will return `0` if the peer (the server in your case) closes the connection. You should handle that case. – Some programmer dude Apr 06 '18 at 03:45
  • After the first input the client is waiting for the server to send something. Does the server send something? – user253751 Apr 06 '18 at 03:45
  • @immibis yes. but when an input is given a second time, nothing is sent to the server. i have both server and client window side by side so i can see – Dees Apr 06 '18 at 03:50
  • @Someprogrammerdude the server never recieves anything. and i'm doing this in linux. On ubuntu bash. I dont know how to debug on it. – Dees Apr 06 '18 at 03:51
  • 1
    Re-reading your question, it seems your problem is with `scanf` not reading input with space in them? Is *that* the problem? It's not about the communication between the client and server at all? Please clarify. – Some programmer dude Apr 06 '18 at 03:55
  • And please [learn how to debug your program](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) in general, and find a tutorial on the GDB debugger (or use the debugger interface in your IDE, if you use one). – Some programmer dude Apr 06 '18 at 03:56
  • The print after return will never create output. You are not waiting for it, are you? – Yunnosch Apr 06 '18 at 04:56
  • When analysing/debugging loops and other logic structures, indentation can be very helpful. (Though the position of `{}` is a matter of taste...) – Yunnosch Apr 06 '18 at 05:00
  • https://stackoverflow.com/questions/2069367/how-to-debug-using-gdb – Yunnosch Apr 06 '18 at 05:02
  • Why does using linux mean that `gets()` is forbidden? How about `fgets()`? http://en.cppreference.com/w/c/io/fgets – Yunnosch Apr 06 '18 at 05:10
  • @Someprogrammerdude i have two problems. the client hangs after second input and if the input has space in it, it cuts off the text after the space – Dees Apr 06 '18 at 14:48
  • @Yunnosch i had to remove fgets. it was fgets that was there before instead of scanf. Problem was that after inputing the portno and host name, the client hung. It never went to "Enter college major" – Dees Apr 06 '18 at 14:49
  • @Yunnosch and no i am not waiting for anything after the return – Dees Apr 06 '18 at 14:50
  • Please try to narrow it down to a *single* problem. If you have two problems then please ask two questions. Each question should have a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) (which means most of the code could be skipped for the question about `scanf` not reading all of your input). And of course please [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly, and again, please [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Some programmer dude Apr 06 '18 at 14:54
  • Oh, and read e.g. [this `scanf` and family reference](http://en.cppreference.com/w/c/io/fscanf). Check what the `"%s"` format actually reads. Will save you a question. – Some programmer dude Apr 06 '18 at 14:54

0 Answers0