voidunimplemented(int client){//回答不能执行的方法char buf[1024];//sprintf格式化输出字符串,printf的兄弟,区别在于spintf是输出到了目标缓冲区//状态行//http/1.1版本号 501错误代码 及原因 /*
状态代码有三位数字组成,第一个数字定义了响应的类别,共分五种类别:
1xx:指示信息--表示请求已接收,继续处理
2xx:成功--表示请求已被成功接收、理解、接受
3xx:重定向--要完成请求必须进行更进一步的操作
4xx:客户端错误--请求有语法错误或请求无法实现
5xx:服务器端错误--服务器未能实现合法的请求
*/sprintf(buf,"HTTP/1.0 501 Method Not Implemented\r\n");//501对应 not implementsend(client, buf,strlen(buf),0);//发送//消息头://服务器端名sprintf(buf, SERVER_STRING);send(client, buf,strlen(buf),0);//数据类型sprintf(buf,"Content-Type: text/html\r\n");send(client, buf,strlen(buf),0);//因为是错误,所以没有长度//空行sprintf(buf,"\r\n");send(client, buf,strlen(buf),0);//消息体sprintf(buf,"<HTML><HEAD><TITLE>Method Not Implemented\r\n");send(client, buf,strlen(buf),0);sprintf(buf,"</TITLE></HEAD>\r\n");send(client, buf,strlen(buf),0);//html格式,使用 /同名 表示结束//网页body里插入<p>标签,这个标签叫做段落标签,它是html中的一个标签//只要插入这个标签,文字就会自动换行并分段sprintf(buf,"<BODY><P>HTTP request method not supported.\r\n");send(client, buf,strlen(buf),0);sprintf(buf,"</BODY></HTML>\r\n");send(client, buf,strlen(buf),0);}
voidnot_found(int client){//客户端错误404char buf[1024];sprintf(buf,"HTTP/1.0 404 NOT FOUND\r\n");send(client, buf,strlen(buf),0);sprintf(buf, SERVER_STRING);send(client, buf,strlen(buf),0);sprintf(buf,"Content-Type: text/html\r\n");send(client, buf,strlen(buf),0);sprintf(buf,"\r\n");send(client, buf,strlen(buf),0);sprintf(buf,"<HTML><TITLE>Not Found</TITLE>\r\n");send(client, buf,strlen(buf),0);sprintf(buf,"<BODY><P>The server could not fulfill\r\n");send(client, buf,strlen(buf),0);sprintf(buf,"your request because the resource specified\r\n");send(client, buf,strlen(buf),0);sprintf(buf,"is unavailable or nonexistent.\r\n");send(client, buf,strlen(buf),0);sprintf(buf,"</BODY></HTML>\r\n");send(client, buf,strlen(buf),0);}
voidbad_request(int client)//错误请求代码400{char buf[1024];sprintf(buf,"HTTP/1.0 400 BAD REQUEST\r\n");send(client, buf,sizeof(buf),0);sprintf(buf,"Content-type: text/html\r\n");send(client, buf,sizeof(buf),0);sprintf(buf,"\r\n");send(client, buf,sizeof(buf),0);sprintf(buf,"<P>Your browser sent a bad request, ");send(client, buf,sizeof(buf),0);sprintf(buf,"such as a POST without a Content-Length.\r\n");send(client, buf,sizeof(buf),0);}
voiderror_die(constchar*sc){perror(sc);//perror(s) 用来将上一个函数发生错误的原因输出到标准设备(stderr)//例如 /*fp=fopen("/root/noexitfile","r+");
if(NULL==fp)
{
perror("/root/noexitfile");
}
将打印 /root/noexitfile: No such file or directory
*/exit(1);}