在进行一些令我困惑的套接字编程时,我遇到了一些奇怪的行为。我从服务器获得了成功的响应,最终程序完全执行。但是,执行一部分逻辑大约需要一分钟。这是它的样子:
int j = 2;
char buffer[BUFFER_SIZE];
char *response;
response = (char*)malloc(BUFFER_SIZE);
while(read(fd, buffer, BUFFER_SIZE - 1) != 0) {
if(j == 2)
response = strcpy(response, buffer);
else
response = strcat(response, buffer);
response = (char*)realloc(response, BUFFER_SIZE * j);
j++;
bzero(buffer, BUFFER_SIZE);
}
fprintf(stderr, "%s", response);
为什么执行需要这么长时间?谢谢你的目光!