I’m stuck on Stage #CN2.
i have extracted the url given by the tester and also i have seperated the data from the URL, and sent back the data to the client using send() in C. it is working perfectly fine in my terminal. but idk why it doesn’t return anything in the turbo test run for codecrafters git push. help will be appreciated
this was the response i got when i run my server. i will attach my server code too for review
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
char* pathdata(char *str){
char *vpth = "GET /echo/";int space=0;
if(strncmp(str,vpth,strlen(vpth))==0){
for(int i=strlen(vpth);i<strlen(str);i++){
if(str[i]==' '){
space=i;break;
}
}
}
else{return "HTTP/1.1 404 Not Found\r\n\r\n";}
str+=strlen(vpth);
space-=strlen(vpth);
// printf("spce: %d\n",space);
// printf("STR: %s\n",str);
char data[space];
// str
// str[strlen(str)]='\0';
strncpy(data,str,space);
data[space]='\0';
// printf("data: %s\n",data);
// printf("----\n");
char *re="HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: %ld\r\n\r\n%s\n",*res;
sprintf(res,re,strlen(data),data);
// printf("res: %s\n",res);
printf("-------done func-------\n");
// char* f = res;
return res;
}
int main() {
// Disable output buffering
setbuf(stdout, NULL);
setbuf(stderr, NULL);
// You can use print statements as follows for debugging, they'll be visible when running tests.
printf("Logs from your program will appear here!\n");
// Uncomment this block to pass the first stage
int server_fd, client_addr_len;
struct sockaddr_in client_addr;
server_fd = socket(AF_INET, SOCK_STREAM, 0);
if (server_fd == -1) {
printf("Socket creation failed: %s...\n", strerror(errno));
return 1;
}
// Since the tester restarts your program quite often, setting SO_REUSEADDR
// ensures that we don't run into 'Address already in use' errors
int reuse = 1;
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)) < 0) {
printf("SO_REUSEADDR failed: %s \n", strerror(errno));
return 1;
}
struct sockaddr_in serv_addr = { .sin_family = AF_INET ,
.sin_port = htons(4221),
.sin_addr = htonl(INADDR_ANY) ,
};
if (bind(server_fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) != 0) {
printf("Bind failed: %s \n", strerror(errno));
return 1;
}
int connection_backlog = 5;
if (listen(server_fd, connection_backlog) != 0) {
printf("Listen failed: %s \n", strerror(errno));
return 1;
}
printf("Waiting for a client to connect...\n");
client_addr_len = sizeof(client_addr);
int cl_fd = accept(server_fd, (struct sockaddr *) &client_addr, &client_addr_len);
// printf("g: %d\n",g);
if(cl_fd==-1){
printf("accept katham katham");
exit(EXIT_FAILURE);
}
printf("Client connected\n");
char re[100];
int bytrd=read(cl_fd,re,99);
// printf("bufsz: %d\n",bytrd);
if(bytrd==-1){
printf("Recv tata bye bye");
exit(EXIT_FAILURE);
}
// printf("bufsz: %d\n",bytrd);
// re[bytrd]='\0';
// printf("%s\n",re);
// printf("15:\n");
// printf("%c\n ",re[16]);
// if(re)
char *res=pathdata(re);
printf("%s\n",res);
printf("strlen %ld\n",strlen(res));
int sd = write(cl_fd,res,strlen(res));
printf("sd: %d\n",sd);
if(sd!=-1){printf("Send success");}
else{printf("send failure!");}
close(cl_fd);
close(server_fd);
return 0;
}
and the output of my code was
Logs from your program will appear here!
Waiting for a client to connect...
Client connected
-------done func-------
HTTP/1.1 200 OK
Content-Type: text/plain
Content-Length: 11
helpmee:sob
strlen 77
sd: 77
codecrafters response:
Plz help, i tried using write() and send(), but no use, even tried making the char pointer to char array and all possible things, even if segmentation fault occurs, program won’t terminate successfully right?
if anyone faced this before or if i made some mistake, pls help broskie


