Home > Mobile >  Server only sending one message to client
Server only sending one message to client

Time:11-28

My Server is only able to send one message to the client (HELLO), the client then sends a WORD message and after the server had received the WORD message it's supposed to send a word.

For some reason the server only sends the HELLO message and upon receiving the WORD message it never sends the next message (in this case it's GREEK). I've tried so many different things but it just doesn't seem to work.

Server code (only the relevant parts) ALL CODE IS IN C

 while (1) {
        if ((newsockfd = accept(sockfd, (struct sockaddr *)&dest, &destlen)) == -1) {
            perror("Accept call failed");
            exit(-1);
        }

        if ((childpid = fork()) == 0) {
            //close(sockfd);
            talk_to_client(newsockfd);
            //close(newsockfd); 
        }
        else if (childpid > 0)  {
            //close(newsockfd);
        }
    }
}

talk_to_client()

void talk_to_client(int sockfd) {
    char message[1024] = "HELLO";
    char message2[1024] = "GREEK";
    char recieved[1024];
    ssize_t n;
    //cannot send more than one!!!!!!!!! WHY NOT
    write(sockfd, message, sizeof(message));
    while (1) {
        recv(sockfd, recieved, sizeof(recieved), 0);
        if (recieved == "WORD") {
            //send initial word
            printf("SENDING WORD");
            write(sockfd, message2, sizeof(message2));
        }
        if (recieved == "QUIT") {
            //close connection
            close(sockfd);
        }

    }
    return;
}

Client code (only relevant parts)

    char srv[512];

    char cli[512] = "WORD";
    // Connects socket to server
        rv = connect(sockfd, (struct sockaddr *) servaddr, sizeof(struct sockaddr));
        if (rv == -1){
            perror("Error connecting to the server");
            exit(-1);
        }       
        if(recv(sockfd, srv, sizeof(srv), 0) == -1) {
            perror("Client receiving error");
        }
        
        printf("Client received: %s\n", srv);

        if(send(sockfd, cli, sizeof(cli), 0) == -1){
            perror("Error sending message to the server");
            exit(-1);
        }
        printf("Client sending: %s\n", cli);

        if(recv(sockfd, srv, sizeof(srv), 0) == -1) {
            perror("Client receiving error");
        }
        printf("Client received: %s\n", srv);

        close(sockfd);
    

I tried many different ways to write to client (write, send, etc..) and I know for a fact it has nothing to do with my connect, bind, socket, listen or accept calls but this is the output I keep getting,

Client received: HELLO
Client sending: WORD
Client received:

CodePudding user response:

recv(sockfd, recieved, sizeof(recieved), 1024);

You are passing the number of bytes but the function is expecting a flag:

ssize_t recv(int sockfd, void *buf, size_t len, int flags);

You are also comparing strings in the wrong way here:

if (recieved == "WORD") {

you want

if (strcmp(recieved, "WORD") == 0) {

CodePudding user response:

char cli[512] = "WORD";
...
    if(send(sockfd, cli, sizeof(cli), 0) == -1){

sizeof(cli) is 512 based on the definition of cli. So it will send 512 bytes. strlen(cli) 1 would be more correct, i.e. send the string and the \0 at the end of the string.

    recv(sockfd, recieved, sizeof(recieved), 0);

This will thus likely receive these 512 bytes in the server, i.e. WORD\0 followed by many bytes junk. Note that I said "likely" since TCP is not a message based protocol but a byte stream and a single send does not need to match a single recv.

    if (recieved == "WORD") {

This does not do a string comparison but compares pointer values. strcmp would be more correct here.

  • Related