0

Issue : Towards the end of the codes, fputs( join, stdout ); prints the string join correctly. However on the visitor side the same string is not printing correctly.

Server Side : ( everything is normal )

root@s1:/test# gcc server_2.c && ./a.out
You are visitor number : 1
Their Address : [127.0.0.1]
You are visitor number : 2
Their Address : [127.0.0.1]
You are visitor number : 3

Visitor side : ( Only "You" is printed and it is also lacking it's own new line )

root@s1:/# echo "a" | nc localhost 91
Youroot@s1:/# ^C
root@s1:/# echo "a" | nc localhost 91
root@s1:/# echo "a" | nc localhost 91
Youroot@s1:/# 

Code : ( Problem is closer to the end )

int main() {

    int one = 1;
    struct sockaddr_in My_Address, Their_Address;
    socklen_t sin_len = sizeof(Their_Address);

    int Path_to_Me = socket(AF_INET, SOCK_STREAM, 0);

    setsockopt(Path_to_Me, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(int));

    int Door = 91;

    My_Address.sin_family = AF_INET;
    My_Address.sin_addr.s_addr = INADDR_ANY;
    My_Address.sin_port = htons(Door);

    bind(Path_to_Me, (struct sockaddr *) &My_Address, sizeof(My_Address));

    listen(Path_to_Me, 5);

    int Me_and_Them;

    char response[] = "You are visitor number :";

    int count;
    count = 0;

    for (;;) {

    count = count + 1;

    char str_count[10];
    sprintf(str_count, " %d\n", count);

    char* join;
    join = ( malloc( strlen(response) + strlen(str_count) + 1 ) );
    strcpy(join, response);
    strcat(join, str_count);

    fputs( join, stdout);

    Me_and_Them = accept(Path_to_Me, (struct sockaddr *) &Their_Address, &sin_len);
    printf("Their Address : [%s]\n", inet_ntoa(Their_Address.sin_addr));

    write(Me_and_Them, join, sizeof(join) - 1); 

    close(Me_and_Them);

    }

return 0;
}
  • 1
    `sizeof(join) - 1` is the size of a *pointer* less-one. That clearly isn't what you had in mind. `sizeof` doesn't do what you think it does there. Nearly every time you find yourself providing a *pointer* to `sizeof` as its argument, you're doing something time from inception. – WhozCraig Oct 09 '19 at 20:57
  • That should be `strlen(join)` – Barmar Oct 09 '19 at 20:59
  • @Barmar, I followed your advice and it is working much better now. The only problem is it is lacking it's own new line. `You are visitor number : 8root@s1:/# ` – Sümer Kolçak Oct 09 '19 at 21:08
  • You're subtracting 1 from `strlen`, so you're not sending the last character, which is the newline. – Barmar Oct 09 '19 at 21:10
  • `strlen()` doesn't count the null terminator, so you don't need to subtract. – Barmar Oct 09 '19 at 21:11
  • @Barmar, I removed the `-1` and it works perfectly now. `-1` was there to remove the `\0`. I suppose it was a flaw idea. If you post the answers from your comments as an answer, I can select it as the `chosen answer`. – Sümer Kolçak Oct 09 '19 at 21:13
  • Can't post an answer to closed questions. – Barmar Oct 09 '19 at 21:14
  • @Barmar, I see, `-1` perhaps was not a flaw idea. Perhaps it was the correct code incase of `sizeof()` being used. I am not sure why `sizeof()` was being used instead of `strlen()`. – Sümer Kolçak Oct 09 '19 at 21:16
  • Maybe an earlier version was using a literal string, e.g. `sizeof("foobar") - 1` is the correct thing. – Barmar Oct 09 '19 at 21:17
  • Also `sizeof response - 1` will work, since `response` is an array, not a pointer. – Barmar Oct 09 '19 at 21:17

0 Answers0