network_old.cc
Go to the documentation of this file.
1 /* Copyright 2017 UFACTORY Inc. All Rights Reserved.
2  *
3  * Software License Agreement (BSD License)
4  *
5  * Author: Jimy Zhang <jimy92@163.com>
6  ============================================================================*/
7  // #define _WINSOCK_DEPRECATED_NO_WARNINGS
8 
9 #include <stdio.h>
10 #include <string.h>
11 #include <iostream>
12 
13 #ifdef _WIN32
14 #include <winsock2.h>
15 #pragma comment(lib, "ws2_32.lib")
16 #include <cstring>
17 #include<ws2tcpip.h>
18 #include<MSTCPiP.h>
19 #define errno WSAGetLastError()
20 #else
21 #include <errno.h>
22 #include <unistd.h>
23 #include <arpa/inet.h>
24 #include <net/if.h>
25 #include <netinet/tcp.h>
26 #include <sys/ioctl.h>
27 #endif
28 
29 #ifdef _WIN32
30 #ifndef inet_pton
31 extern "C" {
32  WINSOCK_API_LINKAGE INT WSAAPI inet_pton(INT Family, PCSTR pszAddrString, PVOID pAddrBuf);
33 }
34 #endif
35 #endif
36 
37 #include "xarm/core/os/network.h"
38 
39 #define DB_FLG "[net work] "
40 #define PRINT_ERR printf
41 
42 #define PERRNO(ret, db_flg, str) \
43  { \
44  if (-1 == ret) { \
45  PRINT_ERR("%s%s, errno=%d\n", db_flg, str, errno); \
46  return -1; \
47  } \
48  \
49 }
50 
51 #ifdef _WIN32
52 
53 int socket_init(char *local_ip, int port, int is_server) {
54  // int iResult;
55  // WSADATA wsaData;
56  // iResult = WSAStartup(MAKEWORD(2,2), &wsaData);
57  // if (iResult != 0) {
58  // printf("WSAStartup failed: %d\n", iResult);
59  // return -1;
60  // }
61  // struct addrinfo *result = NULL, *ptr = NULL, hints;
62  // ZeroMemory(&hints, sizeof(hints));
63  // hints.ai_family = AF_INET; // AF_UNSPEC;
64  // hints.ai_socktype = SOCK_STREAM;
65  // hints.ai_protocol = IPPROTO_TCP;
66  // if (is_server) hints.ai_flags = AI_PASSIVE;
67  // iResult = getaddrinfo(local_ip, port, &hints, &result);
68  // if (iResult != 0) {
69  // printf("getaddrinfo failed: %d\n", iResult);
70  // WSACleanup();
71  // return -1;
72  // }
73  // SOCKET sockfd = INVALID_SOCKET;
74  // // Attempt to connect to the first address returned by
75  // // the call to getaddrinfo
76  // ptr = result;
77 
78  // // Create a SOCKET for connecting to server
79  // sockfd = socket(ptr->ai_family, ptr->ai_socktype, ptr->ai_protocol);
80  // if (sockfd == INVALID_SOCKET) {
81  // printf("Error at socket(): %ld\n", WSAGetLastError());
82  // freeaddrinfo(result);
83  // WSACleanup();
84  // return -1;
85  // }
86  // if (is_server) {
87  // iResult = bind(sockfd, result->ai_addr, (int)result->ai_addrlen);
88  // if (iResult == SOCKET_ERROR) {
89  // printf("bind failed with error: %d\n", WSAGetLastError());
90  // freeaddrinfo(result);
91  // closesocket(sockfd);
92  // WSACleanup();
93  // return -1;
94  // }
95  // }
96 
97  WORD sockVersion = MAKEWORD(2, 2);
98  WSADATA data;
99  PERRNO(WSAStartup(sockVersion, &data), DB_FLG, "ESAStartup");
100  int sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
101  PERRNO(sockfd, DB_FLG, "error: socket");
102 
103  int on = 1;
104  int keepAlive = 1; // Turn on keepalive attribute
105  int keepIdle = 1; // If there is no data in n seconds, probe
106  int keepInterval = 1; // Detection interval,5 seconds
107  int keepCount = 3; // 3 detection attempts
108  struct timeval timeout = { 2, 0 };
109 
110  int ret =
111  setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on));
112  PERRNO(ret, DB_FLG, "error: setsockopt");
113  ret = setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (char *)&keepAlive,
114  sizeof(keepAlive));
115  PERRNO(ret, DB_FLG, "error: setsockopt");
116 
117  tcp_keepalive alive_in;
118  tcp_keepalive alive_out;
119  alive_in.keepalivetime = 1000; // 1s TCP_KEEPIDLE
120  alive_in.keepaliveinterval = 1000; //1s TCP_KEEPINTVL
121  alive_in.onoff = 1;
122  unsigned long ulBytesReturn = 0;
123  ret = WSAIoctl(sockfd, SIO_KEEPALIVE_VALS, &alive_in, sizeof(alive_in),
124  &alive_out, sizeof(alive_out), &ulBytesReturn, NULL, NULL);
125  if (ret == SOCKET_ERROR)
126  {
127  PERRNO(ret, DB_FLG, "WSAIoctl failed");
128  // TRACE(L"setsockopt failed: %d\n", WSAGetLastError());
129  }
130  /*
131  ret = setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPIDLE, (char *)&keepIdle,
132  sizeof(keepIdle));
133  PERRNO(ret, DB_FLG, "error: setsockopt");
134  ret = setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPINTVL, (char *)&keepInterval,
135  sizeof(keepInterval));
136  PERRNO(ret, DB_FLG, "error: setsockopt");
137  ret = setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPCNT, (char *)&keepCount,
138  sizeof(keepCount));
139  PERRNO(ret, DB_FLG, "error: setsockopt");
140  */
141  ret = setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout,
142  sizeof(struct timeval));
143  PERRNO(ret, DB_FLG, "error: setsockopt");
144 
145  if (is_server) {
146  struct sockaddr_in local_addr;
147  local_addr.sin_family = AF_INET;
148  local_addr.sin_port = htons(port);
149  // local_addr.sin_addr.s_addr = inet_addr(local_ip);
150  inet_pton(AF_INET, local_ip, (void *)&local_addr.sin_addr.S_un.S_addr);
151  ret = bind(sockfd, (struct sockaddr *)&local_addr, sizeof(local_addr));
152  PERRNO(ret, DB_FLG, "error: bind");
153 
154  int ret = listen(sockfd, 10);
155  PERRNO(ret, DB_FLG, "error: listen");
156  }
157  return sockfd;
158 }
159 
160 int socket_connect_server(int *socket, char server_ip[], int server_port) {
161  struct sockaddr_in server_addr;
162  server_addr.sin_family = AF_INET;
163  server_addr.sin_port = htons(server_port);
164  //inet_aton(server_ip, &server_addr.sin_addr);
165  inet_pton(AF_INET, server_ip, &server_addr.sin_addr);
166  //InetPton(AF_INET, server_ip, &server_addr.sin_addr);
167  int ret =
168  connect(*socket, (struct sockaddr *)&server_addr, sizeof(server_addr));
169  PERRNO(ret, DB_FLG, "error: connect");
170  return 0;
171 }
172 
173 int socket_send_data(int client_fp, unsigned char *data, int len) {
174  int ret = send(client_fp, (char *)data, len, 0);
175  PERRNO(ret, DB_FLG, "error: socket_send_data");
176  // if (ret == -1) { PRINT_ERR(DB_FLG "error: socket_send_data\n"); }
177  return ret;
178 }
179 
180 #else
181 
182 int socket_init(char *local_ip, int port, int is_server) {
183  int sockfd = socket(AF_INET, SOCK_STREAM, 0);
184  PERRNO(sockfd, DB_FLG, "error: socket");
185 
186  int on = 1;
187  int keepAlive = 1; // Turn on keepalive attribute
188  int keepIdle = 60; // If there is no data in n seconds, probe
189  int keepInterval = 10; // Detection interval,5 seconds
190  int keepCount = 3; // 3 detection attempts
191  struct timeval timeout = { 2, 0 };
192 
193  int ret =
194  setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (void *)&on, sizeof(on));
195  PERRNO(ret, DB_FLG, "error: setsockopt");
196  ret = setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (void *)&keepAlive,
197  sizeof(keepAlive));
198  PERRNO(ret, DB_FLG, "error: setsockopt");
199  ret = setsockopt(sockfd, SOL_TCP, TCP_KEEPIDLE, (void *)&keepIdle,
200  sizeof(keepIdle));
201  PERRNO(ret, DB_FLG, "error: setsockopt");
202  ret = setsockopt(sockfd, SOL_TCP, TCP_KEEPINTVL, (void *)&keepInterval,
203  sizeof(keepInterval));
204  PERRNO(ret, DB_FLG, "error: setsockopt");
205  ret = setsockopt(sockfd, SOL_TCP, TCP_KEEPCNT, (void *)&keepCount,
206  sizeof(keepCount));
207  PERRNO(ret, DB_FLG, "error: setsockopt");
208  ret = setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout,
209  sizeof(struct timeval));
210  PERRNO(ret, DB_FLG, "error: setsockopt");
211 
212  if (is_server) {
213  struct sockaddr_in local_addr;
214  local_addr.sin_family = AF_INET;
215  local_addr.sin_port = htons(port);
216  local_addr.sin_addr.s_addr = inet_addr(local_ip);
217  ret = bind(sockfd, (struct sockaddr *)&local_addr, sizeof(local_addr));
218  PERRNO(ret, DB_FLG, "error: bind");
219 
220  int ret = listen(sockfd, 10);
221  PERRNO(ret, DB_FLG, "error: listen");
222  }
223  return sockfd;
224 }
225 
226 int socket_connect_server(int *socket, char server_ip[], int server_port) {
227  struct sockaddr_in server_addr;
228  server_addr.sin_family = AF_INET;
229  server_addr.sin_port = htons(server_port);
230  inet_aton(server_ip, &server_addr.sin_addr);
231  int ret =
232  connect(*socket, (struct sockaddr *)&server_addr, sizeof(server_addr));
233  PERRNO(ret, DB_FLG, "error: connect");
234  return 0;
235 }
236 
237 int socket_send_data(int client_fp, unsigned char *data, int len) {
238  int ret = send(client_fp, (void *)data, len, 0);
239  PERRNO(ret, DB_FLG, "error: socket_send_data");
240  // if (ret == -1) { PRINT_ERR(DB_FLG "error: socket_send_data\n"); }
241  return ret;
242 }
243 
244 #endif
#define DB_FLG
Definition: network_old.cc:39
#define PERRNO(ret, db_flg, str)
Definition: network_old.cc:42
int socket_connect_server(int *socket, char server_ip[], int server_port)
Definition: network_old.cc:226
int socket_send_data(int client_fp, unsigned char *data, int len)
Definition: network_old.cc:237
int socket_init(char *local_ip, int port, int is_server)
Definition: network_old.cc:182


xarm_api
Author(s):
autogenerated on Sat May 8 2021 02:51:23