linux C++ 用SOCKET封装HTTP协议的方法有吗
,我需要做一个POST请求,然后将返回的HTTP报文头和内容解析内容,现在缺少一个HTTPCLIENT的程序...
,我需要做一个POST请求,然后将返回的HTTP报文头和内容解析内容,现在缺少一个HTTPCLIENT的程序
展开
2个回答
展开全部
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
class SocClient {
public:
SocClient();
~SocClient();
public:
int OpenSocket(int domain, int type, int protocol);
int BindSocket(const struct sockaddr *addr, socklen_t addrlen);
int ListenSocket(int backlog);
int ConnectSocket(const struct sockaddr *addr, socklen_t addrlen);
ssize_t Send(const void *buf, size_t len, int flags);
ssize_t Recv(void *buf, size_t len, int flags);
ssize_t Sendto(const void *buf, size_t len, int flags,
const struct sockaddr *dest_addr, socklen_t addrlen);
ssize_t Recvfrom(void *buf, size_t len, int flags,
struct sockaddr *src_addr, socklen_t *addrlen);
ssize_t Sendmsg(const struct msghdr *msg, int flags);
ssize_t Recvmsg(struct msghdr *msg, int flags);
protected:
//protected code here
private:
int fd;//socket fd;
}
SocClient::SocClient()
:fd(0)
{
}
SocClient::~SocClient()
{
if(fd)
close(fd);
}
int SocClient::OpenSocket(int domain, int type, int protocol)
{
//call system socket function
fd = socket(domain, type, protocol);
}
int SocClient::BindSocket(const struct sockaddr *addr, socklen_t addrlen)
{
return bind(fd, addr, addrlen);
}
剩下的自己实现。
#include <sys/types.h>
#include <sys/socket.h>
class SocClient {
public:
SocClient();
~SocClient();
public:
int OpenSocket(int domain, int type, int protocol);
int BindSocket(const struct sockaddr *addr, socklen_t addrlen);
int ListenSocket(int backlog);
int ConnectSocket(const struct sockaddr *addr, socklen_t addrlen);
ssize_t Send(const void *buf, size_t len, int flags);
ssize_t Recv(void *buf, size_t len, int flags);
ssize_t Sendto(const void *buf, size_t len, int flags,
const struct sockaddr *dest_addr, socklen_t addrlen);
ssize_t Recvfrom(void *buf, size_t len, int flags,
struct sockaddr *src_addr, socklen_t *addrlen);
ssize_t Sendmsg(const struct msghdr *msg, int flags);
ssize_t Recvmsg(struct msghdr *msg, int flags);
protected:
//protected code here
private:
int fd;//socket fd;
}
SocClient::SocClient()
:fd(0)
{
}
SocClient::~SocClient()
{
if(fd)
close(fd);
}
int SocClient::OpenSocket(int domain, int type, int protocol)
{
//call system socket function
fd = socket(domain, type, protocol);
}
int SocClient::BindSocket(const struct sockaddr *addr, socklen_t addrlen)
{
return bind(fd, addr, addrlen);
}
剩下的自己实现。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
Storm代理
2023-08-29 广告
2023-08-29 广告
"StormProxies是全球大数据IP资源服务商,其住宅代理网络由真实的家庭住宅IP组成,可为企业或个人提供满足各种场景的代理产品。点击免费测试(注册即送1G流量)StormProxies有哪些优势?1、IP+端口提取形式,不限带宽,I...
点击进入详情页
本回答由Storm代理提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询