# 200 Ok respond not working

**URL:** <https://forum.codecrafters.io/t/200-ok-respond-not-working/817>\
**Category:** General\
**Tags:** challenge:http-server\
**Created:** [June 22, 2024, 9:06pm UTC](https://forum.codecrafters.io/t/200-ok-respond-not-working/817 "2024-06-22T21:06:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![malisio](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/malisio/32/745_2.png) [@malisio](https://forum.codecrafters.io/u/malisio)\
**Post date:** [June 22, 2024, 9:06pm UTC](https://forum.codecrafters.io/t/200-ok-respond-not-working/817/1 "2024-06-22T21:06:31Z")

</div>

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/](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/](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

```auto
#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.

---

<div class="post-metadata">

**Author:** ![corryjnr](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/corryjnr/32/548_2.png) [@corryjnr](https://forum.codecrafters.io/u/corryjnr)\
**Post date:** [June 24, 2024, 11:29am UTC](https://forum.codecrafters.io/t/200-ok-respond-not-working/817/2 "2024-06-24T11:29:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![malisio](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/malisio/32/745_2.png) [@malisio](https://forum.codecrafters.io/u/malisio)\
**Post date:** [June 24, 2024, 12:29pm UTC](https://forum.codecrafters.io/t/200-ok-respond-not-working/817/3 "2024-06-24T12:29:39Z")

</div>

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;

```

}  
`
