-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsocket.h
More file actions
134 lines (127 loc) · 2.9 KB
/
socket.h
File metadata and controls
134 lines (127 loc) · 2.9 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*************************************************************************
> File Name: socket.h
> Author: MidCHeck
> Mail: midcheck@foxmail.com
> Created Time: 2019年06月04日 星期二 12时56分01秒
************************************************************************/
#ifndef MIDCHECK_SOCKET_H
#define MIDCHECK_SOCKET_H
/* socket类,对不同平台的socket的封装
* 线程安全型
*/
#ifdef _WIN32
//define something for Windows (32-bit and 64-bit, this part is common)
#ifdef _WIN64
//define something for Windows (64-bit only)
#else
//define something for Windows (32-bit only)
#include<WinSock2.h>
#pragmacomment(lib, "ws2_32.lib")
#endif
#elif __APPLE__
#include "TargetConditionals.h"
#if TARGET_IPHONE_SIMULATOR
// iOS Simulator
#elif TARGET_OS_IPHONE
// iOS device
#elif TARGET_OS_MAC
// Other kinds of Mac OS
#else
# error "Unknown Apple platform"
#endif
#elif __ANDROID__
// android
#elif __linux__
// linux
#include<sys/types.h>
// 创建socket
#include<sys/socket.h>
// ip地址转换函数
#include<arpa/inet.h>
// 主机字节序与网络字节序转换
#include<netinet/in.h>
#include<fcntl.h>
#include<unistd.h>
#include<errno.h>
#include<stdlib.h>
#ifndef INVALID_SOCKET
#define INVALID_SOCKET -1
#endif
#ifndef SOCKET_ERROR
#define SOCKET_ERROR -1
#endif
#ifndef IPPROTO_TCP
#define IPPROTO_TCP 0
#endif
#elif __unix__ // all unices not caught above
// Unix
#elif defined(_POSIX_VERSION)
// POSIX
#else
# error "Unknown compiler"
#endif
#include<iostream>
#include "exception.h"
namespace MidCHeck{
class Socket{
protected:
#ifdef __linux__
int sock; // 控制连接
#elif _WIN32
WSADATA wsaData;
SOCKET sock;
#endif
sockaddr_in sock_addr;
public:
Socket() = delete;
Socket(const char* ip, int port){
#ifdef _WIN32
WORD sockVersion = MAKEWORD(2, 2);
try{
if((err = WSAStartup(sockVersion, &wsaData)) != 0)
mcthrow("初始化WSADATA失败!");
else if(LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion)!= 2){
WSACleanup();
mcthrow("不支持当前版本的socket!");
}
}catch(MCErr err){
// 初始化失败
std::cerr << err.what()
<< "\n请检查当前WSADATA初始化" << std::endl;
}
#endif
try{
if((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
mcthrow("监听socket创建出错!");
}catch(MCErr err){
std::cerr << err.what() << std::endl;
}
sock_addr.sin_family = AF_INET;
sock_addr.sin_port = htons(port);
sock_addr.sin_addr.s_addr = inet_addr(ip);
}
#ifdef __linux__
int GetSock()
#elif __WIN32
SOCKET GetSock()
#endif
{
return sock;
}
~Socket(){
#ifdef _WIN32
closesocket(sock);
WSACleanup();
#elif __linux__
close(sock);
#endif
}
#ifdef __linux__
friend int setnonblocking(int);
#endif
};
#ifdef __linux__
int setnonblocking(int fd);
#endif
} // 命名空间MidCHeck结束
#endif