1 Star 1 Fork 0

zhangjiatong / JTHTTPServer

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
Util.hpp 1.47 KB
一键复制 编辑 原始数据 按行查看 历史
zhangjiatong 提交于 2023-02-16 12:24 . 上传JThttpServer工程
#pragma once
#include <iostream>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
//工具类
class Util{
public:
static int ReadLine(int sock, std::string &out)
{
char ch = 'X';
while(ch != '\n'){
ssize_t s = recv(sock, &ch, 1, 0);
if(s > 0){
if(ch == '\r'){
recv(sock, &ch, 1, MSG_PEEK);
if(ch == '\n'){
//把\r\n->\n
//窥探成功,这个字符一定存在
recv(sock, &ch, 1, 0);
}
else{
ch = '\n';
}
}
//1. 普通字符
//2. \n
out.push_back(ch);
}
else if(s == 0){
return 0;
}
else{
return -1;
}
}
return out.size();
}
static bool CutString(const std::string &target, std::string &sub1_out, std::string &sub2_out, std::string sep)
{
size_t pos = target.find(sep);
if(pos != std::string::npos){
sub1_out = target.substr(0, pos);
sub2_out = target.substr(pos+sep.size());
return true;
}
return false;
}
};
C++
1
https://gitee.com/zhangjiatong/jthttpserver.git
git@gitee.com:zhangjiatong/jthttpserver.git
zhangjiatong
jthttpserver
JTHTTPServer
master

搜索帮助