1 #include <cstdio>
2 #include <iostream>
3 #include <string>
4 #include <unistd.h>
5 #include <arpa/inet.h>
6 #include <netinet/in.h>
7 #include <sys/socket.h>
8 #define CHECK_RET(q) if((q)==false){
return -1;}
9 #define LISTEN_BACKLOG 5
10
11 struct data_t{
12 int num1;
13 int num2;
14 char op;
15 };
16 class TcpSocket{
17 private:
18 int _sockfd;
19 public:
20 TcpSocket():_sockfd(-1){
}
21 int GetFd() {
22 return _sockfd;
23 }
24 void SetFd(int fd){
25 _sockfd = fd;
26 }
27 bool Socket() {
28 _sockfd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
29 if (_sockfd < 0) {
30 perror("socket error");
31 return false;
32 }
33 return true;
34 }
35 bool Bind(const std::string &ip, const uint16_t port){
36 sockaddr_in addr;
37 addr.sin_family = AF_INET;
38 addr.sin_port = htons(port);
39 addr.sin_addr.s_addr = inet_addr(&ip[0]);
40 socklen_t len = sizeof(sockaddr_in);
41 int ret = bind(_sockfd, (sockaddr*)&addr, len)
【网络】http自定制协议
最新推荐文章于 2024-05-06 21:58:51 发布