sockets - Segmentation fault when send HTTP request and get response in C -


i made client socket connect server. wanna send http request it.

get /index.html http/1.1\r\n host: www.google.com\r\n \r\n 

this code:

#include <stdlib.h> #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <string.h> #include <arpa/inet.h> #include <netdb.h>  #define max 4096  int main(int argc, char **argv){     char *host;     strcpy(host, argv[1]);      char *request = "get ";     strcat(request, argv[3]);     strcat(request, " http/1.1\r\nhost: ");     strcat(request, argv[2]);     strcat(request, "\r\n\r\n");      char *response;      int port;     port = atoi(argv[2]);      int sockfd;     struct sockaddr_in servaddr;     if((sockfd = socket(af_inet, sock_stream, 0)) < 0){             perror("failed create socket!");             exit(1);     }      memset(&servaddr, 0, sizeof(servaddr));     servaddr.sin_family = af_inet;     servaddr.sin_port = htons(port);      struct hostent *server;     server = gethostbyname(host);     if (server == null) perror("failed host name!");      memcpy(&servaddr.sin_addr.s_addr, server->h_addr, server->h_length);      if(connect(sockfd, (struct sockaddr *) &servaddr, sizeof(servaddr)) < 0){             perror("failed connect!");             exit(1);     }      while(fgets(request, max, stdin) != null){             send(sockfd, request, strlen(request), 0);             if(recv(sockfd, response, max, 0) == 0){                     perror("failed recieve!");                     exit(1);             }              printf("response:/n");             fputs(response, stdout);     } 

when run it, got segmentation fault. use gdb trace it.

#0  strcmp () @ ../sysdeps/x86_64/multiarch/../strcmp.s:132 #1  0x00007ffff7deb1a5 in _dl_name_match_p (name=0x4004e9 "libc.so.6",     map=0x7ffff7ffe1c8) @ dl-misc.c:289 #2  0x00007ffff7de402f in do_lookup_x (new_hash=new_hash@entry=479433942, old_hash=old_hash@entry=0x7fffffffe950,  result=result@entry=0x7fffffffe960, scope=<optimized out>, i=<optimized out>, i@entry=0, flags=flags@entry=1,  skip=skip@entry=0x0, undef_map=undef_map@entry=0x7ffff7ffe1c8) @ dl-lookup.c:462 #3  0x00007ffff7de4961 in _dl_lookup_symbol_x (undef_name=0x40056b "strcat", undef_map=0x7ffff7ffe1c8,  ref=ref@entry=0x7fffffffea18, symbol_scope=0x7ffff7ffe520, version=0x7ffff7ff9a10, type_class=type_class@entry=1, flags=1,  skip_map=skip_map@entry=0x0) @ dl-lookup.c:737 #4  0x00007ffff7de9527 in _dl_fixup (l=<optimized out>, reloc_arg=<optimized out>) @ ../elf/dl-runtime.c:111 #5  0x00007ffff7df04d5 in _dl_runtime_resolve () @ ../sysdeps/x86_64/dl-trampoline.s:45 #6  0x0000000000400b18 in main () 

the error message made me confused. made mistakes?

char *request = "get "; /* can't */ strcat(request, argv[3]); strcat(request, " http/1.1\r\nhost: "); strcat(request, argv[2]); strcat(request, "\r\n\r\n"); 

as request points constant string, cannot append more characters it. need allocate more memory , append more strings it.

similarly response. allocate memory before reading data it.

in - general add

request = malloc(sizeof(char) * max); response = malloc(sizeof(char) * max); 

then

strcpy(request, "get "); strcat(request, argv[3]); strcat(request, " http/1.1\r\nhost: "); strcat(request, argv[2]); strcat(request, "\r\n\r\n"); 

better use strncat etc function safe.


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -