NSUKit 1.4.0
板卡级统一交互接口
载入中...
搜索中...
未找到
c_socket.h
浏览该文件的文档.
1#ifndef _C_SOCKET
2#define _C_SOCKET
3
4#include <iostream>
5#include <cstdio>
6#include <string>
7#include <utility>
8
9#ifdef linux
10#include <sys/types.h>
11#include <sys/socket.h>
12#include <netinet/in.h>
13#include <arpa/inet.h>
14#include <unistd.h>
15#include <sys/shm.h>
16#include <fcntl.h>
17#elif _WIN32
18
19#include <winsock2.h> //
20#include <windows.h>
21
22#pragma comment(lib, "ws2_32.lib")
23// C4996 'inet_addr': Use inet_pton() or InetPton() instead or define _WINSOCK_DEPRECATED_NO_WARNINGS to disable deprecated API warnings
24#pragma warning(disable:4996)
25
26#endif
27
28#include "type.h"
29
30namespace nsukit {
31 #define LISTEN_COUNT 5
32 #define SELECT_TIMEOUT 1
33 #define RECV_DEFAULT_TIMEOUT 5000
34 #define BLOCK_SIZE 1024
35
36
71 private:
72#ifdef linux
73 struct sockaddr_in addrSer;
74 struct sockaddr_in addrCli;
75 socklen_t addr_len;
76#else
77 sockaddr_in addrSer{};
78 sockaddr_in addrCli{};
79 int addr_len{};
80#endif
81 unsigned short int port;
82 std::string ip;
83 bool mode;
84 public:
86
87 int tcpServer{};
88 int tcpClient{};
90
91 // SocketGenerator() {};
92 explicit SocketGenerator(unsigned short int port = 0, std::string ip = "", bool mode = true);
93
95
96 void Init(bool update, unsigned short int port = 0, std::string ip = "");
97
98 int AcceptClient();
99
100 int ConnectServer(float connect_time = SELECT_TIMEOUT);
101
102 int RecvData(int s, char *buf, int len);
103
104 int SendData(int s, char *buf, int len);
105
106 void CloseSock(int s);
107
108 unsigned short int GetPort() const {
109 return port;
110 };
111
112 std::string GetIp() {
113 return ip;
114 }
115 };
116
117 int TcpRecvBytes(SocketGenerator *s, char *buf, size_t len, bool *stopped = nullptr);
118
119 int TcpSendBytes(SocketGenerator *s, char *buf, size_t len, bool *stopped = nullptr);
120}
121#endif
#define SELECT_TIMEOUT
Definition: c_socket.h:32
std::string GetIp()
Definition: c_socket.h:112
unsigned short int GetPort() const
Definition: c_socket.h:108
int TcpRecvBytes(SocketGenerator *s, char *buf, size_t len, bool *stopped=nullptr)
Definition: c_socket.cpp:203
int TcpSendBytes(SocketGenerator *s, char *buf, size_t len, bool *stopped=nullptr)
Definition: c_socket.cpp:228
#define NSU_DLLEXPORT
Definition: type.h:18