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.