0

I'm trying to send two strings from client to server but only the first string is successfully passed, the second one doesn't seem to work.

client code

char *a[2];
    char temp[100];
    printf("Enter name of file : ");
    scanf("%s", temp);
    a[0] = malloc(strlen(temp) + 1); 
    strcpy(a[0],temp);

    printf("Enter name of operation : ");
    scanf("%s", temp);
    a[1] = malloc(strlen(temp) + 1); 
    strcpy(a[1],temp);

    int i = 0;
    while(i<2)
    {
        send(sock , a[i], sizeof(a[i]), 0 ); 
        i++;
    }

Server code:

int i = 0;
    char *buffer[2];
    while (i < 2)
    {
        buffer[i] = malloc(1000);
        int size = read(new_socket, buffer[i], 1000);
        buffer[i][size] = '\0';
        printf("BUFF : %s\n", buffer[i]);
        printf("Size : %d\n", size);
        i++;
    }

    printf("%s\n", buffer[0]);
    printf("%s\n", buffer[1]);

Input:

Enter file name : w

Enter operation code : 1f

output:

enter image description here

  • 1
    Duplicate question. Previous question asked by the same author: https://stackoverflow.com/questions/61153147/inconsistent-output-when-im-sending-2-strings-from-client-to-server-in-c#comment108189802_61153147 – Abhay Aravinda Apr 11 '20 at 11:13
  • I tried to help too. The issue was in server side – Abhay Aravinda Apr 11 '20 at 11:15
  • Ok, now you've deleted that previous question. So I repeat: For stream based protocols you cannot assume each `read` will read exactly one message sent by `send`. You need to keep calling `read` until all data is received. In this case, until you see two NUL characters. – kaylum Apr 11 '20 at 11:16
  • @AbhayAravinda That's was not helpful at all. I know that the issue is on the server side. But I'm not able to rectify it. That's the issue! –  Apr 11 '20 at 11:16
  • @kaylum So how can I make it work? –  Apr 11 '20 at 11:18
  • 2
    You can't assume that the data from `read` is exactly one string. One way is to read into a temporary buffer. Then parse the buffer copying each byte into the final `buffer[i]` until a NUL character is encountered which indicates the end of a string. Then move to the next `buffer[i]`. – kaylum Apr 11 '20 at 11:23
  • 1
    `scanf("%s", temp)` lacks return value checking and is [weak](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) code. Consider `if (fgets(temp, sizeof temp, stdin)) { ...` – chux - Reinstate Monica Apr 11 '20 at 11:27
  • 1
    Tip: Use sentinels to clearly see the being/end of a print which may have leading/trailing white-space or multiple lines. `printf("%s\n", buffer[i]);` --> `printf("<%s>\n", buffer[i]);` – chux - Reinstate Monica Apr 11 '20 at 11:30
  • 1
    `printf("%s\n", buffer[i]); buffer[i][size] = '\0';` is certainly out of order. Reverse that. Perhaps print `size` also. – chux - Reinstate Monica Apr 11 '20 at 11:34
  • "the second one doesn't seem to work." --> That is not so informative. Post input used, output seen, output expected. – chux - Reinstate Monica Apr 11 '20 at 11:35
  • @chux-ReinstateMonica Please have a look now. –  Apr 11 '20 at 11:42
  • Tips: 1) rather than post picture of text, post text. 2) Post true code//input/output: `Enter operation code : 1f` does not match expectations of `printf("Enter name of operation : "); scanf("%s", temp);` and so creates unneeded confusion. – chux - Reinstate Monica Apr 11 '20 at 12:02

1 Answers1

1

At least these problems:

Wrong size

Code sends using the size of pointer a[i] (likely 8) when the length of the string is desired.

// send(sock , a[i], sizeof(a[i]), 0 ); 
send(sock , a[i], strlen(s[i]), 0 ); 
// or (see below)
send(sock , a[i], strlen(s[i]) + 1, 0 ); 

No deliminator

The receiving end is getting the concatenation of the two send().

Also send the null character, so the receiver could parse input (which might be multiple strings).

or

Code could send the length and the string so the receiver could get the length and then the string,

or

many other ways. e.g. @kaylum, @chux

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256