3
int resp = recv(s, buf, len, flags);

if(resp == 18) {
    char data[18];
    strcpy(data, buf);
    ...
}

strlen(data) が 18 に等しいことを期待していますが、そうではありません。私は何を取りこぼしたか?

4

2 に答える 2

2

ジョーが言おうとしているのは、読み取ったバイト数から始めて、データをデータ配列にコピーすることから始めて、あなたのコードは防弾ではないということだと思います。

int resp = recv(s, buf, len, flags);
if(resp > 0) 
{
  // ! This code assumse that all the data will fit into 18 bytes.
  char data[18];  
  memset(data, 0, sizeof(data));  

  // ! As Joe warned above, this code assumes there's a null terminating 
  // ! character in the buf you received.

  strcpy(data, buf);  // consider memcpy if binary data (i.e. not strings)
}
于 2013-06-20T22:03:21.657 に答える