Socket data corrupted during TCP / IP transmission

When I send data through a pre-connected TCP-IP socket, I see that the data is getting corrupted.

Example:

Station 1 sends data to station 2. I printed the data before sending (to S1) and after receiving (in step S2). The following is the message:

S1: Sent data ACK
S2: Received dataAC

Not sure what the problem is. I even cleared the char buffer before sending data (on S1) and before receiving it (in step S2).

Any hint / info for the above would be very helpful.

+3
source share
3 answers

This is usually the result of something like:

/* BAD CODE */
const char* ack = "ACK";
err = write( sockfd, ack, strlen( ack )); /* sender */
/* ... */
char buf[SOME_SIZE]
readb = read( sockfd, buf, SOME_SIZE ); /* receiver */
printf( "%s", buf );

, (3) . . , / . printf , .

Edit:

, , , send(2) TCP- recv(2) ( , , " " ). TCP. , , . - . , ( select(2) - ).

:

  • - , . .
  • / - , . , , // /.

endianess - , .

+4

, ? TCP , , Station2, . , recv() .

, recv() "AC" "K"

+2

:

char message[200];

/*string to be sent*/
strcpy(message, "Hi PQRS, How are you!?");

/*send string to server socket*/
if( send(socket_desc , message , strlen(message)+1 , 0) < 0)
    {
        puts("Send failed");
        return 1;
    }
puts("Data Sent\n");

,

'strlen (message) + 1'

, '\ 0'

'STRLEN ()

.

, , , .

0

Source: https://habr.com/ru/post/1776532/


All Articles