refer:hostloc
Author:dunce
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
const char* redir = "HTTP/1.1 301 Moved Permanently\r\n"
"Location: http://www.baidu.com\r\n\r\n";
int main()
{
struct sockaddr_in sa;
memset(&sa,0,sizeof(sa));
sa.sin_family = AF_INET;
sa.sin_port = htons(8888);
int fd = socket(AF_INET,SOCK_STREAM,0);
bind(fd,(struct sockaddr*)&sa,sizeof(sa));
listen(fd,16);
int xfd;
while(1)
{
xfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK);
write(xfd, redir, strlen(redir));
close(xfd);
}
close(fd);
}