'Send HTTP 200 response from socket server
I am trying to send response to client, whenever I add HTTP/1.1 200 to my buffer, the server wont start,whenever I go on localhost:18000 it loads forever. Without the http/1.1 200 it starts but I want to send a http response. Am I doing something wrong?
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <string.h>
int main()
{
int socket_server = socket(AF_INET, SOCK_STREAM, 0);
int port = 18000;
char buffer[2000] = "HTTP/1.1 200 OK\nHello";
if (socket_server == -1)
{
return printf("Error in socket function");
}
struct sockaddr_in addres;
addres.sin_family = AF_INET;
addres.sin_port = htons(port);
addres.sin_addr.s_addr = INADDR_ANY;
int bind_server = bind(socket_server, (struct sockaddr*) &addres, sizeof(addres));
if(bind_server == -1) {
return printf("Error in bind function");
}
int listens = listen(socket_server, 0);
if(listens == -1) {
return printf("Error in listen function");
}
for(;;) {
int accepts = accept(socket_server, NULL, NULL);
send(accepts, &buffer, sizeof(buffer), 0);
}
return 0;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
