-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsock.hpp
More file actions
103 lines (99 loc) · 3.17 KB
/
Copy pathsock.hpp
File metadata and controls
103 lines (99 loc) · 3.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* @file sock.hpp
* @brief Static socket utility. Wraps socket/bind/listen/accept/setnonblock syscalls.
*
* CSCI 599: Network Systems for Cloud Computing
* University of Southern California
*/
#ifndef __SOCK_HPP__
#define __SOCK_HPP__
#include "log.hpp"
#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <iostream>
#include <netinet/in.h>
#include <pthread.h>
#include <signal.h>
#include <string.h>
#include <string>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/resource.h>
class Sock {
private:
const static int gbacklog = 1024; // 一般不能太大也不能太小,后面再详细解释
public:
Sock() { }
~Sock() { }
static int Socket() {
int listen_sock = socket(AF_INET, SOCK_STREAM, 0);
if (listen_sock < 0) {
logMessage(FATAL, "create socket error, %d: %s", errno, strerror(errno));
exit(2);
}
int opt = 1;
setsockopt(listen_sock, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof opt);
logMessage(NORMAL, "create socket success, sock: %d", listen_sock);
return listen_sock;
}
static void Bind(int sock, uint16_t port, std::string ip = "0.0.0.0") {
struct sockaddr_in local;
memset(&local, 0, sizeof(local));
local.sin_family = AF_INET;
local.sin_port = htons(port);
inet_pton(AF_INET, ip.c_str(), &local.sin_addr);
if (bind(sock, (struct sockaddr*)&local, sizeof(local)) < 0) {
logMessage(FATAL, "bind error, %d: %s", errno, strerror(errno));
exit(3);
}
}
static void Listen(int sock) {
if (listen(sock, gbacklog) < 0) {
logMessage(FATAL, "listen error, %d: %s", errno, strerror(errno));
exit(4);
}
logMessage(NORMAL, "init TcpServer Success");
}
static int Accept(int listensock, std::string* ip, uint16_t* port, int* accept_errno) {
/*
一般经验:
const std::string& 输入型参数
std::string * 输出型参数
std::string & 输入输出型参数
*/
struct sockaddr_in src;
socklen_t len = sizeof(src);
*accept_errno = 0;
int service_sock = accept(listensock, (sockaddr*)&src, &len);
if (service_sock < 0) {
*accept_errno = errno;
return -1;
}
if (port) *port = ntohs(src.sin_port);
if (ip) *ip = inet_ntoa(src.sin_addr);
return service_sock;
}
static bool Connect(int sock, const std::string& server_ip, const uint16_t& server_port) {
struct sockaddr_in server;
memset(&server, 0, sizeof server);
server.sin_family = AF_INET;
server.sin_port = htons(server_port);
server.sin_addr.s_addr = inet_addr(server_ip.c_str());
if (connect(sock, (struct sockaddr*)&server, sizeof(server)) == 0)
return true;
else
return false;
}
static bool SetNonBlock(int sock) {
int fl = fcntl(sock, F_GETFL);
if (fl < 0)
return false;
fcntl(sock, F_SETFL, fl | O_NONBLOCK);
return true;
}
};
#endif