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()

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值