技术标签: http服务器 C/C++ c语言 linux c linux socket
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/shm.h>
#include <pthread.h>
#include "http.h"
#define BUFFER_SIZE 1024
//处理用户的连接的线程方法
void *handle(void *arg){
int conn = *((int *)arg),i=0,j=0,len=0;
char buffer[BUFFER_SIZE];
char *buf = NULL;
char temp[1024];
int fp = -1;
int count = 0;
int flag = 0;
//需要优化
while(1){
memset(buffer,0,sizeof(buffer));
len = recv(conn, buffer, sizeof(buffer),0);
if (buf == NULL) {
buf = (char *)malloc(BUFFER_SIZE);
}else {
buf = (char *)realloc(buf, (i + 1)*BUFFER_SIZE);
}
for (j = 0; j < BUFFER_SIZE; j++) {
buf[i*BUFFER_SIZE + j] = buffer[j];
if(buffer[j]==13&&buffer[j+1]==10&&buffer[j+2]==13&&buffer[j+3]==10&&j<BUFFER_SIZE-4){
flag = 1;
}
}
if(flag)
break;
}
printf("%s\n\n",buf);
Request res = parseRequest(buf);
free(buf);
//处理请求文件 发送数据
//发送状态码 (用字符串链接)
len = strlen("HTTP/1.1 200 OK\r\nConnection: close\r\nAccept-Ranges: bytes\r\n")+1;
buf = (char *)malloc(sizeof(char)*len);
strcpy(buf,"HTTP/1.1 200 OK\r\nConnection: close\r\nAccept-Ranges: bytes\r\n");
sprintf(temp,"Content-Type: %s\r\n", getFileType(res.file));
len += strlen(temp);
buf = (char *)realloc(buf,sizeof(char)*len);
strcat(buf,temp);
printf("filename: %s\n", res.file);
printf("Content-Length: %d\n", getFileSize(res.file));
sprintf(temp, "Content-Length: %d\r\n\r\n", getFileSize(res.file));
len += strlen(temp);
buf = (char *)realloc(buf,sizeof(char)*len);
strcat(buf,temp);
//二进制发送文件
fp = open(res.file,O_RDONLY);
while (1) {
i = read(fp,temp,sizeof(temp));
if (i <= 0)
break;
buf = (char *)realloc(buf,sizeof(char)*(len+i));
memcpy(&buf[len], temp, i);
len+=i;
}
close(fp);
buf = (char *)realloc(buf,sizeof(char)*(len+4));
memcpy(&buf[len], "\r\n", 2);
send(conn, buf, (len+2), 0);
printf("finish\n");
close(conn);//关闭连接
}
int main(int argc,char *argv[]){
struct sockaddr_in client_addr; //获取客户机信息
socklen_t length = sizeof(client_addr); //类型大小
pthread_t *thread = NULL; //处理事务线程
int conn,*temp=NULL,err=0;
int httpServerFd = socket(AF_INET,SOCK_STREAM,0);
int opt = 1;
setsockopt(httpServerFd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(int));
struct sockaddr_in server_sockaddr;
server_sockaddr.sin_family = AF_INET;
server_sockaddr.sin_port = htons(atoi(argv[1]));
server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(httpServerFd,(struct sockaddr *)&server_sockaddr,sizeof(server_sockaddr))==-1){
printf("绑定失败!\n");
return 0;
}
///listen,成功返回0,出错返回-1
if(listen(httpServerFd,10) == -1){
printf("监听失败!\n");
return 0;
}
while(1){
///成功返回非负描述字,出错返回-1
conn = accept(httpServerFd, (struct sockaddr*)&client_addr, &length);
if(conn==-1){
printf("客户机连接出错!\n");
return 0;
}
temp = (int *)malloc(sizeof(int));
*temp = conn;
thread = (pthread_t *)malloc(sizeof(pthread_t));
err = pthread_create(thread,NULL,handle,(void *)temp);
if(err != 0 ){
printf("create thread fail\n");
}
}
}
#pragma once
typedef struct req {
char *method; //请求方式
char *file; //请求文件
char *host; //主机名称
char *accept; //请求文件类型
char *accept_Language; //请求语言
char *accept_Encoding; //客户端支持编码
char *connection; //链接状态
char *user_Agent; //用户环境
//form表单 暂不处理
}Request;
Request parseRequest(char *buffer); //根据请求头构造请求信息
#include "http.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "util.h"
Request parseRequest(char * buffer)
{
Request req;
char *head = NULL,**line = NULL,*temp=NULL;
int i = 0, line_count = 0,head_count=0,index=0;
req.accept = NULL;
req.accept_Encoding = NULL;
req.accept_Language = NULL;
req.connection = NULL;
req.host = NULL;
req.method = NULL;
req.file = NULL;
req.user_Agent = NULL;
//按行解析请求头
line_count = countChar(buffer, '\n');
line = (char **)malloc(line_count*sizeof(char *));
for (i = 0; i < line_count; i++)
line[i] = NULL;
temp = strtok(buffer, "\n");
i = 0;
while (temp) {
line[i] = (char *)malloc((strlen(temp)+1)*sizeof(char));
strcpy(line[i++], temp);
temp = strtok(NULL, "\n");
}
for (i = 0; i < line_count&&line[i]!=NULL; i++) {
head = strtok(line[i], " ");
if (strcmp(head, "GET")==0) { //获取请求文件 暂时不解析参数
temp = strtok(NULL, " ");
index = indexOfStrFirst(temp, '?');
if(index!=-1)
temp[index] = '\0';
req.file = (char *)malloc((strlen("/var/www/html")+strlen(temp) + 1));
printf("length:%d\n",(strlen(temp) + 1));
sprintf(req.file,"/var/www/html%s", temp);
}else if (strcmp(head, "POST")==0) { //获取请求文件 暂时不解析参数
temp = strtok(NULL, " ");
index = indexOfStrFirst(temp, '?');
if(index!=-1)
temp[index] = '\0';
req.file = (char *)malloc((strlen(temp) + 1));
strcpy(req.file, temp);
}else if (strcmp(head, "Host:")==0) {
printf("FN:%s\n",req.file);
printf("LN:%d\n",strlen(req.file));
temp = strtok(NULL, " ");
req.host = (char *)malloc((strlen(temp) + 1));
strcpy(req.host, temp);
printf("FN:%s\n",req.file);
printf("LN:%d\n",strlen(req.file));
}else if (strcmp(head, "User-Agent:")==0) {
temp = strtok(NULL, " ");
req.user_Agent = (char *)malloc((strlen(temp) + 1));
strcpy(req.user_Agent, temp);
}else if (strcmp(head, "Accept:")==0) {
temp = strtok(NULL, " ");
req.accept = (char *)malloc((strlen(temp) + 1));
strcpy(req.accept, temp);
}else if (strcmp(head, "Accept-Language:")==0) {
temp = strtok(NULL, " ");
req.accept_Language = (char *)malloc((strlen(temp) + 1));
strcpy(req.accept_Language, temp);
}else if (strcmp(head, "Accept-Encoding:")==0) {
temp = strtok(NULL, " ");
req.accept_Encoding = (char *)malloc((strlen(temp) + 1));
strcpy(req.accept_Encoding, temp);
}else if (strcmp(head, "Connection:")==0) {
temp = strtok(NULL, " ");
req.connection= (char *)malloc((strlen(temp) + 1));
strcpy(req.connection, temp);
}
}
return req;
}
#pragma once
int countChar(char *str, char c); //统计一串字符串中某个字符的个数
int indexOfStrFirst(char *str, char c); //查询字符首次出现的位置
char *getFileType(const char *filename); //获取对应的HTML的响应头的文件类型
int getFileSize(const char *filename); //获取文件大小
#include "util.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
int countChar(char * str, char c)
{
int i = 0,count=0;
for(i=0;i<strlen(str);i++)
if (str[i] == c)
count++;
return count;
}
int indexOfStrFirst(char * str, char c)
{
int i = 0;
for (i = 0; i < strlen(str); i++) {
if (str[i] == c)
return i;
}
return -1;
}
char *getFileType(const char *filename){ //根据扩展名返回文件类型描述
char sExt[32];
const char *p_start = filename;
memset(sExt, 0, sizeof(sExt));
while (*p_start)
{
if (*p_start == '.')
{
p_start++;
strncpy(sExt, p_start, sizeof(sExt));
break;
}
p_start++;
}
if (strncmp(sExt, "bmp", 3) == 0)
return "image/bmp";
if (strncmp(sExt, "gif", 3) == 0)
return "image/gif";
if (strncmp(sExt, "ico", 3) == 0)
return "image/x-icon";
if (strncmp(sExt, "jpg", 3) == 0)
return "image/jpeg";
if (strncmp(sExt, "avi", 3) == 0)
return "video/avi";
if (strncmp(sExt, "css", 3) == 0)
return "text/css";
if (strncmp(sExt, "dll", 3) == 0)
return "application/x-msdownload";
if (strncmp(sExt, "exe", 3) == 0)
return "application/x-msdownload";
if (strncmp(sExt, "dtd", 3) == 0)
return "text/xml";
if (strncmp(sExt, "mp3", 3) == 0)
return "audio/mp3";
if (strncmp(sExt, "mpg", 3) == 0)
return "video/mpg";
if (strncmp(sExt, "png", 3) == 0)
return "image/png";
if (strncmp(sExt, "ppt", 3) == 0)
return "application/vnd.ms-powerpoint";
if (strncmp(sExt, "xls", 3) == 0)
return "application/vnd.ms-excel";
if (strncmp(sExt, "doc", 3) == 0)
return "application/msword";
if (strncmp(sExt, "mp4", 3) == 0)
return "video/mpeg4";
if (strncmp(sExt, "ppt", 3) == 0)
return "application/x-ppt";
if (strncmp(sExt, "wma", 3) == 0)
return "audio/x-ms-wma";
if (strncmp(sExt, "wmv", 3) == 0)
return "video/x-ms-wmv";
return "text/html";
}
int getFileSize(const char * filename)
{
int fileSize = 0;
FILE *fp = NULL;
if (filename == NULL)
return 0;
//以二进制的形式读取
fp = fopen(filename, "rb");
if (fp == NULL)
return 0;
fseek(fp, 0, SEEK_END);
fileSize = ftell(fp);
rewind(fp);
fclose(fp);
return fileSize;
}
文章浏览阅读364次。1.WebMagicWebMagic是一个简单灵活的Java爬虫框架。基于WebMagic,你可以快速开发出一个高效、易维护的爬虫。2.在Eclipse中配置WebMagic1.首先需要下载WebMagic的压缩包官网地址为:WebMagic官网最新版本为:WebMagic-0.7.3,找到对应版本,打开下载界面,注意,下载要选择Source code(zip)版本,随便下载到哪里都可以;2.下载好的压缩包需要解压,此时解压到的位置即为后续新建的Eclipse的project位置,比如我的Ecli_使用eclipse搭建webmagic工程
文章浏览阅读1.9k次。mysql数据库是一种开放源代码的关系型数据库管理系统,有很多朋友都在使用。一些在linux系统上安装了mysql数据库的朋友,却不知道该如何对mysql数据库进行配置。那么linux该如何启动mysql服务呢?接下来小编就给大家带来linux启动mysql服务的命令教程。具体步骤如下:1、首先,我们需要修改mysql的配置文件,一般文件存放在/etc下面,文件名为my.cnf。2、对于mysql..._linux中 mysql 启动服务命令
文章浏览阅读537次。详解OJ(Online Judge)中PHP代码的提交方法及要点Introduction of How to submit PHP code to Online Judge SystemsIntroduction of How to commit submission in PHP to Online Judge Systems在目前常用的在线oj中,codeforces、spoj、uva、zoj..._while(fscanf(stdin, "%d %d", $a, $b) == 2)
文章浏览阅读534次。一、设置MyEclipse编码(1)修改工作空间的编码方式:Window-->Preferences-->General-->Workspace-->Text file encoding(2)修改一类文件的编码方式:Window-->Preferences-->General-->content Types-->修改default Encoding(..._java修改快捷缩写内容
文章浏览阅读1.4w次,点赞19次,收藏76次。1.前言市面上关于Android的技术书籍很多,几乎每本书也都会涉及到蓝牙开发,但均是上层应用级别的,而且篇幅也普遍短小。对于手机行业的开发者,要进行蓝牙模块的维护,就必须从Android系统底层,至少框架层开始,了解蓝牙的结构和代码实现原理。这方面的文档、网上的各个论坛的相关资料却少之又少。分析原因,大概因为虽然蓝牙协议是完整的,但是并没有具体的实现。蓝牙芯片公司只负责提供最底层的API_蓝牙原理图详解
文章浏览阅读7.7k次。图/源于网络文/曲尚菇凉1.今天早上出门去逛街,在那家冰雪融城店里等待冰淇淋的时候,听到旁边两个女生在讨论很久之前的一期《奇葩说》。那期节目主持人给的辩论题是“从未在一起和最终没有在一起哪个更遗憾”,旁边其中一个女生说,她记得当时印象最深的是有个女孩子说了这样一句话。她说:“如果我喜欢一个人呢,我就从第一眼到最后一眼,把这个人爱够,把我的感觉用光,我只希望那些年让我成长的人是他,之后的那些年他喝过..._从未在一起更遗憾
文章浏览阅读175次。Spring Cloud Alibaba 介绍Sping体系Spring 以 Bean(对象) 为中心,提供 IOC、AOP 等功能。Spring Boot 以 Application(应用) 为中心,提供自动配置、监控等功能。Spring Cloud 以 Service(服务) 为中心,提供服务的注册与发现、服务的调用与负载均衡等功能。Sping Cloud介绍官方介绍 Tools for building common patterns in distributed systems_sprngcloud alba
文章浏览阅读3.2k次,点赞4次,收藏21次。我这里是根据之前在测试数据类项目过程中的一些总结经验和掉过个坑,记录一下,可以给其他人做个参考,没什么高深的东西,但是如果不注意这些细节点,后期也许会陷入无尽的扯皮当中。1 需求实现的准确度根据产品需求文档描述发现不明确不详细的或者存在歧义的地方一定要确认,例如数据表中的一些字段,与开发和产品确认一遍,如有第三方相关的,要和第三方确认,数据类项目需要的是细心,哪怕数据库中的一个字段如果没有提前对清楚,后期再重新补充,会投入更大的精力。2 数据的合理性根据业务场景/常识推理,提..._基础字段的测试点
文章浏览阅读491次。大家好,我是爱学习的小xiong熊妹。在工作和面试中,很多小伙伴会遇到“对XX行业进行分析”的要求。一听“行业分析”四个字,好多人会觉得特别高大上,不知道该怎么做。今天给大家一个懒人攻略,小伙伴们可以快速上手哦。一、什么是行业?在做数据分析的时候,“行业”两个字,一般指的是:围绕一个商品,从生产到销售相关的全部企业。以化妆品为例,站在消费者角度,就是简简单单的从商店里买了一支唇膏回去。可站在行业角度,从生产到销售,有相当多的企业在参与工作(如下图)在行业中,每个企业常常扮._码工小熊
文章浏览阅读1.6w次,点赞2次,收藏2次。还需要做更多的研究来解决大型语言模型中的偏见、有毒评论和幻觉的风险。我们在数万亿个令牌上训练我们的模型,并表明可以仅使用公开可用的数据集来训练最先进的模型,而无需诉诸专有和不可访问的数据集。在大型语言模型空间中训练像 LLaMA 这样的小型基础模型是可取的,因为它需要更少的计算能力和资源来测试新方法、验证他人的工作和探索新的用例。作为 Meta 对开放科学承诺的一部分,今天我们公开发布 LLaMA(大型语言模型元 AI),这是一种最先进的基础大型语言模型,旨在帮助研究人员推进他们在 AI 子领域的工作。_llma
文章浏览阅读223次,点赞3次,收藏5次。1.背景介绍制造业是国家经济发展的重要引擎,其产能和质量对于国家经济的稳定和发展具有重要意义。随着工业技术的不断发展,制造业的生产方式也不断发生变化。传统的制造业通常依赖于人工操作和手工艺,这种方式的缺点是低效率、低产量和不稳定的质量。随着信息化、智能化和网络化等新技术的出现,制造业开始向智能制造迈出了第一步。智能制造的核心是通过大数据、人工智能、计算机视觉等技术,实现制造过程的智能化、自动化...
文章浏览阅读938次。系列文章目录文章目录系列文章目录 前言 一、ansible是什么? 二、使用步骤 1.引入库 2.读入数据 总结前言菜鸟一只,刚开始使用,仅作以后参考使用。边学习,边记录,介绍一下最基础的使用,可能会有理解不到位的地方,可以共同交流,废话不多说,走起。一、ansible 简介?ansible是自动化运维工具的一种,基于Python开发,可以实现批量系统配置,批量程序部署,批量运行命令,ansible是基于模块工作的,它本身没有批量部署的能力,真正.._pip安装ansible