0

I have written two simple programs ,one for server and one for client, to exchange messages after developing a TCP connection between them . I want client to send message to server two times ,but the problem I am facing is that at second time both programs get stuck. Here is the code.

Server.c

    // Connection already established upto this point


    // ------------------------------------- First Time -------------------------------
    printf("Receving messages ...\n");

    if (recv(client_sock, client_message, sizeof(client_message), 0) < 0)
    {
            printf("Receive Failed. Error!!!!!\n");
            return -1;
    }


    //while (strcmp(client_message, "DISCONNECTED") != 0) {

    printf("Client Message: %s\n", client_message);

    strcpy(server_message, client_message);



    // successfully send the message to the client but does not proceed after this line , where client_sock is the socket of client returned by 'accept' funtion call

    if (send(client_sock, server_message, strlen(server_message), 0) < 0)
    {
            printf("Send Failed. Error!!!!!\n");
            return -1;
    }
    
    printf("Came"); //Stuck here i.e this print statement doesn't get executed


    // ------------------------------------- Seconds Time -------------------------------
    printf("Receving messages ...");
    memset(client_message, '\0', sizeof(client_message));

    if (recv(client_sock, client_message, sizeof(client_message), 0) < 0)
    {
            printf("Receive Failed. Error!!!!!\n");
            return -1;
    }


    printf("Client Message: %s\n", client_message);

    strcpy(server_message, client_message);




    if (send(client_sock, server_message, strlen(server_message), 0) < 0)
    {
            printf("Send Failed. Error!!!!!\n");
            return -1;
    }




    // }







    close(client_sock);
    close(socket_desc);
    return 0;

My client code is:

printf("Connected\n");
    // ------------------------------------- First Time -------------------------------
    // Get Input from the User
    printf("Enter Message: ");
    scanf("%[^\n]", client_message);


    if (send(socket_desc, client_message, strlen(client_message), 0) < 0) {
            printf("Send Failed. Error!!!!\n");
            return -1;
    }


    if (recv(socket_desc, server_message, sizeof(server_message), 0) < 0) {
            printf("Receive Failed. Error!!!!!\n");
            return -1;
    }


    printf("Server Message: %s\n", server_message);
    
    printf("Yes"); // stuck here , this statement doesn't get executed
    memset(server_message, '\0', sizeof(server_message));
    memset(client_message, '\0', sizeof(client_message));

    // sleep(2);

    // ------------------------------------- First Time -------------------------------

    printf("Enter Message: ");
    scanf("%[^\n]", client_message);

    if (send(socket_desc, client_message, strlen(client_message), 0) < 0) {
            printf("Send Failed. Error!!!!\n");
            return -1;
    }

    if (recv(socket_desc, server_message, sizeof(server_message), 0) < 0) {
            printf("Receive Failed. Error!!!!!\n");
            return -1;
    }


    printf("Server Message: %s\n", server_message);

    memset(server_message, '\0', sizeof(server_message));
    memset(client_message, '\0', sizeof(client_message));






    // Closing the Socket
    // close(socket_desc);


    return 0;

Please help , where is the mistake so that it can successfully exchange messages two times.

Huzaifa
  • 31
  • 5
  • 1
    Relevant? Please replace `scanf("%[^\n]", client_message);` with `scanf(" %[^\n]", client_message);` (added space) and see [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). Other usages too. – Weather Vane Oct 21 '22 at 15:37
  • 1
    One major problem is that you seem to have forgotten that strings are really ***null-terminated** strings. The data you send from the client will *not* include the null-terminator, but the server uses the received data as a null-terminated string, which it really isn't. – Some programmer dude Oct 21 '22 at 15:38
  • We don't see `client_message` and exactly what `sizeof(client_message)` refers to. If it is a function argument than it's probably the size of a *pointer*. – Weather Vane Oct 21 '22 at 15:39
  • The thing is if I remove the code below 'second time' comment line and run these programs , to exchange messages once , then it works fine , so if it really because of the problems like null terminated strings , size of client messages etc as you people have mentioned , then it should not work too for only one time message exchange ? If I'm wrong correct me please . – Huzaifa Oct 21 '22 at 15:50
  • Undefined behaviour might or might not work, sometimes, always or never. – Weather Vane Oct 21 '22 at 15:55
  • Also, TCP is a *streaming* protocol, without any beginnings or ends (expect connection establishment and connection close). It's all a flow of bytes without any information about its contents or any message boundaries. As such, one call to `recv` might not actually get all the data that has been sent. You need to implement a protocol on top of TCP, to introduce something that ells you message sizes or boundaries. Also note that one call to `recv` might get a full message *and perhaps more*, so you have to be able to handle that case as well. – Some programmer dude Oct 21 '22 at 15:56
  • One possible way to introduce message boundaries is to send the actual string terminator. For example by sending `strlen(...) + 1` bytes. Then your receiver needs to use a loop to receive data until it has received a null-terminator. – Some programmer dude Oct 21 '22 at 15:57
  • Try to send/receive null terminated string within fixed sized buffer; send/recv sizeof(client_message) bytes. – ulix Oct 21 '22 at 16:51

0 Answers0