network_new.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 #ifdef _WIN32
10 #include <ws2tcpip.h>
11 #else
12 #include <arpa/inet.h>
13 #include <net/if.h>
14 #include <netinet/tcp.h>
15 #include <sys/ioctl.h>
16 #include <unistd.h>
17 #endif
18 #include <errno.h>
19 #include <stdio.h>
20 #include <string.h>
21 
22 #include "xarm/core/os/network.h"
23 
24 #define DB_FLG "[NetWork] "
25 #define PRINT_ERR printf
26 
27 #define PERRNO(ret, db_flg, str) \
28 { \
29  if (-1 == ret) { \
30  PRINT_ERR("%s%s, errno=%d\n", db_flg, str, errno); \
31  return -1; \
32  } \
33  \
34 }
35 
36 int socket_init(char *local_ip, int port, int is_server) {
37  int sockfd = static_cast<int> (socket(AF_INET, SOCK_STREAM, 0));
38  PERRNO(sockfd, DB_FLG, "Error: socket");
39 
40  int on = 1;
41  int keepAlive = 1; // Turn on keepalive attribute
42  int keepIdle = 60; // If there is no data in n seconds, probe
43  int keepInterval = 10; // Detection interval,, unit:seconds
44  int keepCount = 3; // 3 detection attempts
45  struct timeval timeout = { 2, 0 };
46 
47  int ret =
48  setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on));
49  PERRNO(ret, DB_FLG, "Error: setsockopt[SO_REUSEADDR]");
50  ret = setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, (char *)&keepAlive,
51  sizeof(keepAlive));
52  PERRNO(ret, DB_FLG, "Error: setsockopt[SO_KEEPALIVE]");
53  ret = setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPIDLE, (char *)&keepIdle,
54  sizeof(keepIdle));
55  PERRNO(ret, DB_FLG, "Error: setsockopt[TCP_KEEPIDLE]");
56  ret = setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPINTVL, (char *)&keepInterval,
57  sizeof(keepInterval));
58  PERRNO(ret, DB_FLG, "Error: setsockopt[TCP_KEEPINTVL]");
59  ret = setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPCNT, (char *)&keepCount,
60  sizeof(keepCount));
61  PERRNO(ret, DB_FLG, "Error: setsockopt[TCP_KEEPCNT]");
62  ret = setsockopt(sockfd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout,
63  sizeof(struct timeval));
64  PERRNO(ret, DB_FLG, "Error: setsockopt[SO_SNDTIMEO]");
65 
66  if (is_server) {
67  struct sockaddr_in local_addr;
68  local_addr.sin_family = AF_INET;
69  local_addr.sin_port = htons(port);
70  local_addr.sin_addr.s_addr = inet_addr(local_ip);
71  ret = bind(sockfd, (struct sockaddr *)&local_addr, sizeof(local_addr));
72  PERRNO(ret, DB_FLG, "error: bind");
73 
74  int ret = listen(sockfd, 10);
75  PERRNO(ret, DB_FLG, "error: listen");
76  }
77  return sockfd;
78 }
79 
80 int socket_connect_server(int *socket, char server_ip[], int server_port) {
81  struct sockaddr_in server_addr;
82  server_addr.sin_family = AF_INET;
83  server_addr.sin_port = htons(server_port);
84  inet_aton(server_ip, &server_addr.sin_addr);
85  int ret =
86  connect(*socket, (struct sockaddr *)&server_addr, sizeof(server_addr));
87  PERRNO(ret, DB_FLG, "error: connect");
88  return 0;
89 }
90 
91 int socket_send_data(int client_fp, unsigned char *data, int len) {
92  int ret = send(client_fp, (void *)data, len, 0);
93  PERRNO(ret, DB_FLG, "error: socket_send_data");
94  return ret;
95 }
#define DB_FLG
Definition: network_new.cc:24
int socket_connect_server(int *socket, char server_ip[], int server_port)
Definition: network_new.cc:80
#define PERRNO(ret, db_flg, str)
Definition: network_new.cc:27
int socket_init(char *local_ip, int port, int is_server)
Definition: network_new.cc:36
int socket_send_data(int client_fp, unsigned char *data, int len)
Definition: network_new.cc:91


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