200 Ok respond not working

Hey when I try to run test cases I get this
remote:
remote: [tester::#IA4] Running tests for Stage #IA4 (Respond with 200)
remote: [tester::#IA4] $ ./your_server.sh
remote: [your_program] Logs from your program will appear here!
remote: [your_program] Waiting for a client to connect…
remote: [tester::#IA4] $ curl -v http://localhost:4221/
remote: [your_program] Client connected
remote: [tester::#IA4] Failed to read response:
remote: [tester::#IA4] Received: “” (no content received)
remote: [tester::#IA4] ^ error
remote: [tester::#IA4] Error: Expected: HTTP-version, Received: “”
remote: [tester::#IA4] Test failed (try setting ‘debug: true’ in your codecrafters.yml to see more details)
But when I test it myself I dont get this type of Error and I get
$ curl -v http://localhost:4221/

  • Trying 127.0.0.1:4221…
  • Connected to localhost (127.0.0.1) port 4221 (#0)

GET / HTTP/1.1
Host: localhost:4221
User-Agent: curl/7.81.0
Accept: /

  • Mark bundle as not supporting multiuse
    < HTTP/1.1 200 OK
  • no chunk, no close, no size. Assume close to signal end
    <
  • Recv failure: Connection reset by peer
  • Closing connection 0
    curl: (56) Recv failure: Connection reset by peer
#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>

int main() {

	// Disable output buffering

	setbuf(stdout, 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");

	int server_fd, client_addr_len;

	struct sockaddr_in client_addr;

	// 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);

	//

	// accept(server_fd, (struct sockaddr *) &client_addr, &client_addr_len);

	// printf("Client connected\n");

	//

	// close(server_fd);

	server_fd = socket(AF_INET, SOCK_STREAM, 0);
	char * respond="HTTP/1.1 200 OK \r\n\r\n";
	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 fd = accept(server_fd, (struct sockaddr *) &client_addr, &client_addr_len);

	printf("Client connected\n");
	int bytes_send = send(fd,respond,strlen(respond),0);

	close(server_fd);

	return 0;

}

I got no idea what I’m doing wrong.

After int fd = accept(server_fd, (struct sockaddr *) &client_addr, &client_addr_len); can you try first receive a request before sending a response.

Oh I’m so dumb thanks for pointing it out I did pass the test cases Thanks!
here is the new Code `#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>

int main() {

// Disable output buffering

setbuf(stdout, 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");

int server_fd, client_addr_len;

struct sockaddr_in client_addr;
struct header{

char * host;
char * user_agent;

 	};

char *  OK_RESPOND="HTTP/1.1 200 OK\r\n\r\n";


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");

int client_addr_size= sizeof(client_addr);

int fd = accept(server_fd, (struct sockaddr *) &client_addr,&client_addr_size);
if(fd < 0){
	printf("Accpet Failed!\n");
	exit(EXIT_FAILURE);
}

char request_buffer[64];

ssize_t bytes_received = recv(fd, request_buffer, sizeof(request_buffer), 0);

if (bytes_received < 0) {
printf("Error receiving request: %s\n", strerror(errno));
close(fd);
close(server_fd);
return 1;

}

printf("Client connected\n");

request_buffer[bytes_received] = '\0';
 
int bytes_sent = send(fd, OK_RESPOND, strlen(OK_RESPOND), 0);

if (bytes_sent < 0) {
   printf("Error sending response: %s\n", strerror(errno));

}

close(fd);

close(server_fd);

return 0;

}
`