Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <unistd.h>
- int main(void)
- {
- int s = socket(AF_INET, SOCK_STREAM, 0);
- if (s == -1)
- {
- return 1;
- }
- struct sockaddr_in addr;
- addr.sin_family = AF_INET;
- addr.sin_port = htons(80);
- addr.sin_addr.s_addr = INADDR_ANY;
- if (bind(s, (struct sockaddr*)&addr, sizeof(struct sockaddr_in)) == -1)
- {
- close(s);
- return 2;
- }
- if (listen(s, SOMAXCONN) == -1)
- {
- close(s);
- return 3;
- }
- char buf[1000];
- for (;;)
- {
- int c = accept(s, NULL, NULL);
- if (c == -1)
- {
- continue;
- }
- if (recv(c, buf, 1000, 0) <= 0)
- {
- close(c);
- continue;
- }
- else
- {
- if (send(c, "HTTP/1.1 200 OK\r\nContent-Length: 12\r\nConnection: close\r\nContent-Type: text/plain; charset=UTF-8\r\n\r\nHello world!", 111, 0) == -1)
- {
- close(c);
- continue;
- }
- }
- close(c);
- }
- close(s);
- return 0;
- }
Add Comment
Please, Sign In to add comment