IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> c/c++中好用的Linux C函数 -> 正文阅读

[C++知识库]c/c++中好用的Linux C函数

前言:

为了自己能够在学习和工作中能够快速定位,快速理解,而浅浅的总结的。

希望在帮助自己的同时也能够帮助到各位码友。

1.#include <stdio.h>

  1. ?sscanf(),
  2. perror(),
  3. sprintf()
1.sscanf()
int parseRequestLine(const char* line, int cfd)
{
	// 解析请求行
	char method[12] = { 0 };
	char path[1024] = { 0 };
	char version[16] = { 0 };
	sscanf(line, "%[^ ] %[^ ] %[^\r\n]", method, path, version);
	decodeMsg(path, path);
	printf("method: %s, path: %s, version: %s\n", method, path, version);
----------------------------------------------------------------------------------------
2.perror
perror("recv");
-----------------------------------------------------------------------------------------

3.sprintf()
int sendHeadMsg(int cfd, int status, const char* descr, const char* type, int length)
{
	printf("START Response status line, header and blank line... \n");
	// 状态行
	char buf[4096] = { 0 };
	sprintf(buf, "http/1.1 %d %s\r\n", status, descr);
	// 响应头和空行
	sprintf(buf + strlen(buf), "content-type: %s\r\n", type);
	sprintf(buf + strlen(buf), "content-length: %d\r\n\r\n", length);
	//sprintf(buf + strlen(buf), "\r\n");

	send(cfd, buf, strlen(buf), 0);
	printf("END Response status line, header and blank line... \n");
	return 0;
}

2.#include <strings.h>?? ??? ?// strcasecmp():不区分大小写

	if (strcasecmp(method, "get") != 0)
	{
		return -1;
	}

?3.#include <string.h>?? ?

  1. ?memcpy(),
  2. memset(),
  3. strcmp(),
  4. strrchr(),
  5. strstr()
1,2.memcpy,memset
void* recvHttpRequset(void* arg)
{
	struct FdInfo* info = (struct FdInfo*)arg;
	printf("\nstart communication.....\n");
	int len = 0, total = 0;
	char tmp[1024];
	char buf[4096];
	while ((len = recv(info->fd, tmp, sizeof(tmp), 0)) > 0)
	{
		if (total + len < sizeof(buf))
		{
			memcpy(buf + total, tmp, len);
			memset(tmp, 0, sizeof(tmp));
		}
		total += len;
	}
----------------------------------------------------------------------------------
3.strcmp
	// 处理客户端请求的静态资源(目录或文件)
	char* file = NULL;
	if (strcmp(path, "/") == 0) 
	{
		file = "./";     //请求的是资源目录
	}
-------------------------------------------------------------------------------------
4.strrchr
	// a.jpg, a.mp4, a.html........
	// 自右向左查找'.'字符, 如果不存在返回null
	const char* dot = strrchr(name, '.');

5.	strstr
// 判断数据是否被接受完毕
	if (len == -1 && errno == EAGAIN)   //数据接受完毕
	{
		// 解析请求行
		char* pt = strstr(buf, "\r\n"); //在大的字符串中寻找子串
		int reqLen = pt - buf;
		buf[reqLen] = '\0';
		parseRequestLine(buf, info->fd);
	}

?4.#include <sys/stat.h> ? ? ? // 文件属性系列

	// 获取文件属性
	struct stat st;
	int ret = stat(file, &st);
	if (ret == -1)
	{
		// 文件不存在 --回复404
		sendHeadMsg(cfd, 404, "Not Found", getFileType(".html"), -1);
		sendFile("404.html", cfd);
		return 0;
	}
	// 判断文件类型
	if (S_ISDIR(st.st_mode))
	{
		// 把这个目录中的内容发送给客户端
		printf("The request is for a directory...\n");
		sendHeadMsg(cfd, 200, "OK", getFileType(".html"), -1);
		sendDir(file, cfd);
	}
	else
	{
		// 把文件的内容发送给客户端
	    printf("The request is for a file...\n");
		sendHeadMsg(cfd, 200, "OK", getFileType(file), st.st_size);
		sendFile(file, cfd);
	}

?5.#include <unistd.h>

  1. ?lseek()
  2. ,chdir():?//切换当前进程的工作目录,chdir(argv[2]);
	off_t offset = 0;
	int size = lseek(fd, 0, SEEK_END); //求文件大小
	printf("size: %ld\n", size);
	lseek(fd, 0, SEEK_SET);            //把文件指针移动到文件头
	printf("============================01\n");
	while (offset < size)
	{
		printf("============================02\n");
		int ret = sendfile(cfd, fd, &offset, size - offset);
		printf("ret value: %d\n", ret);
		if (ret == -1 && errno == EAGAIN)
		{
			printf("没有数据 no data......\n");
			/*perror("sendfile");*/
		}
	}

?6.#include <stdlib.h>

?1.atoi把字符串转化为整形?unsigned short port = atoi(argv[1]);

2.?malloc():struct FdInfo* info = (struct FdInfo*)malloc(sizeof(struct FdInfo));

3.free()

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-09-21 00:08:17  更:2022-09-21 00:11:18 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/19 13:17:06-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码