-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.h
104 lines (90 loc) · 2.85 KB
/
socket.h
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
#ifndef _SOCKET_H_
#define _SOCKET_H_
#include<iostream>
#ifdef _WIN32
#include<winsock2.h>
#include<windows.h>
#pragma comment(lib,"ws2_32.lib")
typedef struct sockaddr_in sockaddr_in;
typedef struct WSAData WSAData;
typedef struct sockaddr sockaddr;
typedef int socklen_t;
#define MY_CLOSE_SOCKET(s) closesocket(s)
#endif //
#ifdef linux
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<unistd.h>
#define MY_CLOSE_SOCKET(s) close(s)
#endif //
const size_t max_bytes = 1024;
void func(sockaddr_in* addr);
class udp_socket{
public:
udp_socket(){
#ifdef _WIN32
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
// 处理初始化失败的情况
throw std::exception();
}
#endif
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
std::cout << sockfd << std::endl;
}
~udp_socket(){
MY_CLOSE_SOCKET(sockfd);
}
int connect(const sockaddr* serveraddr, socklen_t addrlen){
return ::connect(sockfd, serveraddr, addrlen);
}
int bind(uint16_t port, uint32_t ip = INADDR_ANY){
sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl(ip);
server_addr.sin_port = htons(port);
return ::bind(sockfd, (sockaddr*)&server_addr, sizeof(server_addr));
}
int recvfrom(int flags){
//char ip_address[256];
// if(inet_ntop(AF_INET, &(from.sin_addr), ip_address, INET_ADDRSTRLEN) == NULL){
// throw std::exception();
// }
// printf("IPv4 地址: %s\n", ip_address);
// ssize_t count = ::recvfrom(sockfd, buff, max_bytes, flags, (sockaddr*)&from, &addrlen);
// if(count == -1){
// if(errno == EWOULDBLOCK || errno == EAGAIN){
// std::cout << "receive timeout" << std::endl;
// }
// }
// return count;
return count = ::recvfrom(sockfd, buff, max_bytes, flags, (sockaddr*)&from, &addrlen);
}
int sendto(const char* buff, int nbytes, int flags, const sockaddr* to, socklen_t addrlen){
func((sockaddr_in*)to);
return ::sendto(sockfd, buff, nbytes, flags, to, addrlen);
}
int sendto(const char* buff, int nbytes, int flags){
return ::sendto(sockfd, buff, nbytes, flags, (sockaddr*)&from, sizeof(sockaddr_in));
}
sockaddr_in* getFrom() const{
return &from;
}
std::string getBuff(){
return std::string(buff, count);
}
bool setRecvTimeout(time_t sec, time_t m_second){
struct timeval timeout{sec, m_second}; //设置超时时间3秒钟
setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
}
private:
int sockfd;
mutable sockaddr_in from;
socklen_t addrlen = sizeof(sockaddr);
char buff[max_bytes];
int count;
};
class tcp_socket{
};
#endif