这里使用的是云服务器实现,需要在云服务器控制台防火墙设置中开放对应的端口。因为程序负责让云服务器监听内网地址,但是外部的浏览器访问的是公网地址。设置9000端口为套接字的绑定端口,1024以下是不建议的因为这个范围内主要是知名端口。这里作为http服务器采用80端口也可以。
http服务器就是在TCP服务器的基础上按照http的协议格式组织和发送数据。
http_srv.cpp
#include"tcpsocket.hpp"
#include<string>
#include<cstring>
#include<sstream>
int main(int argc, char *argv[])
{
//argc是程序运行参数个数
//argv存放程序运行参数 ./tcp_srv 10.0.4.7 9000
if(argc !=3)
{
printf("useage: ./tcp_srv 10.0.4.7 9000");
return -1;
}
std::string ip = argv[1];
uint16_t port = std::stoi(argv[2]);
TcpSocket lst_sock;
//1.创建套接字
CHECK_RET(lst_sock.Socket());
//2.绑定地址信息
CHECK_RET(lst_sock.Bind(ip,port));
//3.开始监听
CHECK_RET(lst_sock.Listen());
while(1)
{
//4.获取新建连接
TcpSocket new_sock;
bool ret = lst_sock.Accpet(&new_sock,&ip,&port);
if(ret ==false)
{
usleep(1000);
continue;
}
std::string buf;
new_sock.Recv(&buf);
std::cout<<"req["<<buf<<"]\n";
std::string body;
body = "<html><body><h1>Not Found</h1></body></html>";
std::stringstream ss;
ss<<"HTTP/1.1 404 ok\r\n";
ss<<"Content-Type: text/html\r\n";
ss<<"Content-Length: "<<body.size()<<"\r\n";
ss<<"Location: https://www.baidu.com\r\n";
ss<<"\r\n";
ss<<body;
new_sock.Send(ss.str());
new_sock.Close();
//6.关闭套接字
}
lst_sock.Close();
return 0;
}
tcpsocket.hpp
#include<cstdio>
#include<iostream>
#include<string>
#include<unistd.h>
#include<arpa/inet.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<cstring>
#define MAX_LISTEN 5
#define CHECK_RET(q) if((q)==false){return false;}
typedef struct
{
int num1;
int num2;
char op;
}tmp_t;
class TcpSocket
{
public:
TcpSocket():_sockfd(-1){}
bool Socket()
{
this->_sockfd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if(this->_sockfd <0)
{
perror("socket perror");
return false;
}
return true;
}
int GetFd()
{
return _sockfd;
}
void Addr(struct sockaddr_in* addr, const std::string& ip, uint16_t port)
{
addr->sin_family = AF_INET;
addr->sin_port = htons(port);
addr->sin_addr.s_addr = inet_addr(ip.c_str());
return;
}
bool Bind(const std::string &ip, uint16_t port)
{
struct sockaddr_in addr;
Addr(&addr,ip,port);
socklen_t len = sizeof(struct sockaddr_in);
int ret = bind(_sockfd, (sockaddr*)&addr, len);
if(ret<0)
{
perror("bind error");
return false;
}
return true;
}
bool Listen(int backlog = MAX_LISTEN)
{
//int listen(描述符,同一时间最大连接数)
int ret =listen(_sockfd,backlog);
if(ret<0)
{
perror("listen error");
return false;
}
return true;
}
bool Connect(const std::string &ip, u_int16_t port)
{
//connect(描述符,服务端地址,服务端地址长度)
struct sockaddr_in addr;
Addr(&addr,ip,port);
socklen_t len = sizeof(struct sockaddr_in);
int ret = connect(_sockfd,(struct sockaddr*)&addr, len);
if(ret<0)
{
perror("connect error");
return false;
}
return true;
}
bool Accpet(TcpSocket* sock, std::string *ip=NULL, uint16_t *port = NULL)
{
//accept(监听描述符,客户端地址,地址长度)
struct sockaddr_in addr;
socklen_t len = sizeof(struct sockaddr_in);
int newfd = accept(_sockfd,(struct sockaddr*)&addr, &len);
if(newfd<0)
{
perror("accept error");
return false;
}
sock->_sockfd = newfd;
if(ip != NULL)
{
*ip = inet_ntoa(addr.sin_addr);
}
if(port != NULL)
{
*port = ntohs(addr.sin_port);
}
return true;
}
bool Send(const std::string &data)
{
//ssize_t send(描述符,数据,长度,标志)
int total =0;
while(total <(int)data.size())
{
int ret = send(_sockfd, &data[0]+total,data.size()-total,0);
if(ret <0)
{
printf("发送失败\n");
perror("send error");
return false;
}
total+=ret;
}
return true;
}
bool Recv(std::string *buf)
{
//ssize_t recv(描述符,缓冲区,长度,标志);
char tmp[4096]={0};
int ret = recv(_sockfd,tmp,4096,0);
if(ret<0)
{
perror("recv error");
return false;
}
if(ret ==0)
{
std::cout<<"peer shutdown"<<std::endl;
return false;
}
buf->assign(tmp,ret);//申请空间拷贝指定长度数据
return true;
}
bool Close()
{
if(_sockfd != -1)
{
close(_sockfd);
}
return true;
}
private:
int _sockfd;
};