封装一组socket常用通信操作,header only 风格。
#pragma once
#include <unistd.h>
#include <string>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#define CHECK_RET(exp) if(!(exp)){ return false; }
class TcpSocket{
public:
TcpSocket() : _fd(-1){
}
TcpSocket(int fd) : _fd(fd) {
}
bool Socket(){
_fd = socket(AF_INET, SOCK_STREAM, 0);
if(_fd < 0){
perror("socket");
return false;
}
return true;
}
bool Close() const {
close(_fd);
retur