웹 소켓 핸드 셰이크를 구현하고 싶습니다. 다음 코드를 사용하고 있습니다. 하지만 내가 동적으로 할당하는 메모리를 해제 시작 세그먼트 화 오류가 발생합니다. 처음으로 자유 기능을 사용하는 곳에 오류가 나타납니다. 도와주세요. 당신은 상수 문자열 "\r\n..."
로 handshake_part3
를 재 할당하는메모리를 해제하는 동안 세그먼트 오류가 발생했습니다.
char rbuf[656];
char handshake[800];
char *handshake_part2, *handshake_part3,*key,*magic,*final;
unsigned char hash [20];
key=strndup(rbuf+359, 24);
magic = malloc(strlen("258EAFA5-E914-47DA-95CA-C5AB0DC85B11")+2);
strcpy(magic,"258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
final = malloc (60);
final = strcat(key,magic);
SHA1(final,strlen(final),hash);
base64(hash, sizeof(hash));
handshake_part2= malloc(400);
handshake_part2= base64(hash, sizeof(hash));
strcpy (handshake,"HTTP/1.1 101 Web Socket Protocol Handshake\r\nUpgrade: Websocket\r \nConnection: Upgrade\r\nSec-WebSocket-Accept: ");
strcat(handshake,handshake_part2);
handshake_part3= malloc(400);
handshake_part3="\r\nWebSocket-Origin: http://localhost:9605\r\nWebSocket-Location: ws://localhost:9609/\r\n\r\n";
strcat(handshake,handshake_part3);
printf("Response Header :\n%s", handshake);
free(handshake_part3);
handshake_part3=NULL;
printf("Free 1");
free(handshake_part2);
handshake_part2=NULL;
printf("Free 2");
free(final);`
final=NULL;
printf("Free 3");
free(magic);
magic=NULL;
printf("Free 4");
free(key);
참으로 "더" 들여 쓰기를 수정하십시오. – sean
malloc 호출 후 handshake_part3의 값을 검사하여 메모리가 처음 할당되었는지 확인해야합니다 : – Raj
왜 처음에 malloc을 했습니까? – netcoder