Sending and receiving strings over TCP socket separately

后端 未结 4 1338
难免孤独
难免孤独 2021-02-10 19:17

I have a TCP server and client, with an established socket. Let\'s say I have the following case:

SERVER:

char *fn = \"John\";
char *ln = \"Doe\";

char          


        
4条回答
  •  情话喂你
    2021-02-10 19:50

    instead of sizeof(buffer) use strlen(fn); so it will transmit only the exact amount of bytes.. if you need null terminator together use strlen(fn)+1 but don't forget to concatenate null terminator with strcat()

    also if you send all the buffer size without cleaning with memset you'll also have rubbish along, so beware that..

提交回复
热议问题