52 lines
903 B
C
52 lines
903 B
C
|
#ifndef LXZL_NETWORKRELATED_H
|
||
|
#define LXZL_NETWORKRELATED_H
|
||
|
|
||
|
#include <iostream>
|
||
|
// 平台检测
|
||
|
#ifdef _WIN32
|
||
|
#include <winsock2.h>
|
||
|
#include <ws2tcpip.h>
|
||
|
#else
|
||
|
#include <sys/socket.h>
|
||
|
#include <netinet/in.h>
|
||
|
#include <arpa/inet.h>
|
||
|
#include <unistd.h>
|
||
|
#include <fcntl.h>
|
||
|
#include <sys/ioctl.h>
|
||
|
#endif
|
||
|
|
||
|
// 平台兼容类型定义
|
||
|
#ifdef _WIN32
|
||
|
using socklen_t = int;
|
||
|
using socket_t = SOCKET;
|
||
|
constexpr socket_t INVALID_SOCKET_VALUE = INVALID_SOCKET;
|
||
|
#else
|
||
|
using socket_t = int;
|
||
|
constexpr socket_t INVALID_SOCKET_VALUE = -1;
|
||
|
#define SOCKET_ERROR (-1)
|
||
|
#endif
|
||
|
|
||
|
|
||
|
|
||
|
// Windows平台初始化
|
||
|
#ifdef _WIN32
|
||
|
|
||
|
class WSAInitializer {
|
||
|
public:
|
||
|
WSAInitializer() {
|
||
|
WSADATA wsaData;
|
||
|
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
|
||
|
throw std::runtime_error("WSAStartup failed");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
~WSAInitializer() {
|
||
|
WSACleanup();
|
||
|
}
|
||
|
};
|
||
|
|
||
|
static WSAInitializer wsa_initializer;
|
||
|
#endif
|
||
|
|
||
|
#endif //LXZL_NETWORKRELATED_H
|