First of all you should allocate a buffer for filename
. The next problem is your offset.
char buffer[512] = "GET /testfile.htm HTTP/1.0";
char filename[512]; // I want *filename to hold only "/testfile.htm"
msgLen = recv(connecting_socket, buffer, 512, 0);
strncpy(filename, buffer+4, msgLen-4-9);
//the first parameter should be buffer+4, not 5. Indexes are zero based.
//the second parameter is count, not the end pointer. You should subtract
//the first 4 chars too.
Also you should make sure you add a null at the end of string as strncpy
doesn't do it.
filename[msgLen-4-9] = 0;
You could also use memcpy
instead of strncpy
as you want to just copy some bytes:
memcpy(filename, buffer+4, msgLen-4-9);
fileName[msgLen-4-9] = 0;
In either case, make sure you validate your input. You might receive invalid input from the socket.