TcpSocket.cpp
Go to the documentation of this file.
1 // -- BEGIN LICENSE BLOCK ----------------------------------------------
20 // -- END LICENSE BLOCK ------------------------------------------------
21 
23 
24 namespace visionary {
25 
26 int TcpSocket::connect(const std::string& hostname, uint16_t port)
27 {
28  int iResult = 0;
29 
30 #ifdef _WIN32
31  //-----------------------------------------------
32  // Initialize Winsock
33  WSADATA wsaData;
34  iResult = ::WSAStartup(MAKEWORD(2, 2), &wsaData);
35  if (iResult != NO_ERROR)
36  {
37  return iResult;
38  }
39 #endif
40 
41  //-----------------------------------------------
42  // Create a receiver socket to receive datagrams
43  m_socket = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
44  if (m_socket == INVALID_SOCKET)
45  {
46  return (int)INVALID_SOCKET;
47  }
48 
49  //-----------------------------------------------
50  // Bind the socket to any address and the specified port.
51  sockaddr_in recvAddr;
52  recvAddr.sin_family = AF_INET;
53  recvAddr.sin_port = port;
54  recvAddr.sin_addr.s_addr = inet_addr(hostname.c_str());
55 
56  iResult = ::connect(m_socket, (sockaddr*)&recvAddr, sizeof(recvAddr));
57 
58  if (iResult != 0)
59  {
60  return iResult;
61  }
62 
63  // Set the timeout for the socket to 5 seconds
64  long timeoutSeconds = 5L;
65 #ifdef _WIN32
66  // On Windows timeout is a DWORD in milliseconds
67  // (https://docs.microsoft.com/en-us/windows/desktop/api/winsock/nf-winsock-setsockopt)
68  long timeoutMs = timeoutSeconds * 1000L;
69  iResult = setsockopt(m_socket, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeoutMs, sizeof(DWORD));
70 #else
71  struct timeval tv;
72  tv.tv_sec = timeoutSeconds; /* 5 seconds Timeout */
73  tv.tv_usec = 0L; // Not init'ing this can cause strange errors
74  iResult = setsockopt(m_socket, SOL_SOCKET, SO_RCVTIMEO, (const char*)&tv, sizeof(struct timeval));
75 #endif
76 
77  return iResult;
78 }
79 
80 int TcpSocket::openServer(uint16_t port)
81 {
82  int iResult = 0;
83 #ifdef _WIN32
84  //-----------------------------------------------
85  // Initialize Winsock
86  WSADATA wsaData;
87  iResult = ::WSAStartup(MAKEWORD(2, 2), &wsaData);
88  if (iResult != NO_ERROR)
89  {
90  return iResult;
91  }
92 #endif
93 
94  //-----------------------------------------------
95  // Create a server TCP socket to be able to connect to TCP clients
96  m_socketServer = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
98  {
99  return (int)INVALID_SOCKET;
100  }
101 
102  //-----------------------------------------------
103  // Bind the socket to any address and the specified port.
104  sockaddr_in server;
105  server.sin_family = AF_INET;
106  server.sin_port = port;
107  server.sin_addr.s_addr = INADDR_ANY;
108 
109  iResult = bind(m_socketServer, (sockaddr*)&server, sizeof(server));
110  if (iResult == 0)
111  {
112  iResult = listen(m_socketServer, 1);
113  }
114  return iResult;
115 }
116 
117 
118 int TcpSocket::openTcp(uint16_t port)
119 {
120  int iResult = -1;
121 
122 #ifdef _WIN32
123  //-----------------------------------------------
124  // Initialize Winsock
125  WSADATA wsaData;
126  iResult = ::WSAStartup(MAKEWORD(2, 2), &wsaData);
127  if (iResult != NO_ERROR)
128  {
129  return iResult;
130  }
131 #endif
132 
133  //-----------------------------------------------
134  // Create a TCP socket to be able to connect to TCP device
135  m_socketTcp = ::socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
137  {
138  std::printf("TCP socket is not created\n");
139  return (int)INVALID_SOCKET;
140  }
141 
142  //-----------------------------------------------
143  // Bind the socket to any address and the specified port.
144  sockaddr_in saddr;
145  saddr.sin_family = AF_INET;
146  saddr.sin_port = port;
147  saddr.sin_addr.s_addr = INADDR_ANY;
148 
149  iResult = bind(m_socketTcp, (sockaddr*)&saddr, sizeof(saddr));
150 
151  return iResult;
152 }
153 
155 {
156  bool res = false;
157  sockaddr_in client;
158  SOCKET clientSocket;
159  int size = sizeof(sockaddr_in);
160 
161  clientSocket = accept(m_socketServer, (sockaddr*)&client, (socklen_t*)&size);
162 
163  if (clientSocket != INVALID_SOCKET)
164  {
165  m_socket = clientSocket;
166  res = true;
167  printf("Connected to IP: %s, Port: %d\n", inet_ntoa(client.sin_addr), client.sin_port);
168  }
169  return res;
170 }
171 
173 {
174  // Close the socket when finished receiving datagrams
175 #ifdef _WIN32
176  closesocket(m_socket);
177  closesocket(m_socketServer);
178  closesocket(m_socketTcp);
179  WSACleanup();
180 #else
181  close(m_socket);
182  close(m_socketServer);
183  close(m_socketTcp);
184 #endif
188 
189  return 0;
190 }
191 
192 int TcpSocket::send(const std::vector<std::uint8_t>& buffer)
193 {
194  // send buffer via TCP socket
195  return ::send(m_socket, (char*)buffer.data(), static_cast<int>(buffer.size()), 0);
196 }
197 
198 int TcpSocket::recv(std::vector<std::uint8_t>& buffer, std::size_t maxBytesToReceive)
199 {
200  // receive from TCP Socket
201  buffer.resize(maxBytesToReceive);
202  char* pBuffer = reinterpret_cast<char*>(buffer.data());
203 
204  return ::recv(m_socket, pBuffer, static_cast<int>(maxBytesToReceive), 0);
205 }
206 
207 int TcpSocket::read(std::vector<std::uint8_t>& buffer, std::size_t nBytesToReceive)
208 {
209  // receive from TCP Socket
210  buffer.resize(nBytesToReceive);
211  char* pBuffer = reinterpret_cast<char*>(buffer.data());
212 
213  int bytesReceived = 0;
214  while (nBytesToReceive > 0)
215  {
216  bytesReceived = ::recv(m_socket, pBuffer, static_cast<int>(nBytesToReceive), 0);
217 
218  if (bytesReceived == SOCKET_ERROR || bytesReceived == 0)
219  {
220  return -1;
221  }
222  pBuffer += bytesReceived;
223  nBytesToReceive -= bytesReceived;
224  }
225  pBuffer = NULL;
226  return buffer.size();
227 }
228 
229 } // namespace visionary
TcpSocket.h
INVALID_SOCKET
#define INVALID_SOCKET
Definition: TcpSocket.h:44
visionary
Definition: AuthenticationLegacy.h:25
visionary::TcpSocket::read
int read(std::vector< std::uint8_t > &buffer, std::size_t nBytesToReceive) override
Definition: TcpSocket.cpp:207
visionary::TcpSocket::m_socketServer
SOCKET m_socketServer
Definition: TcpSocket.h:65
SOCKET
int SOCKET
Definition: TcpSocket.h:43
visionary::TcpSocket::m_socketTcp
SOCKET m_socketTcp
Definition: TcpSocket.h:66
SOCKET_ERROR
#define SOCKET_ERROR
Definition: TcpSocket.h:45
visionary::TcpSocket::WaitForConnection
bool WaitForConnection()
Definition: TcpSocket.cpp:154
visionary::TcpSocket::m_socket
SOCKET m_socket
Definition: TcpSocket.h:64
visionary::TcpSocket::openTcp
int openTcp(uint16_t port)
Definition: TcpSocket.cpp:118
visionary::TcpSocket::shutdown
int shutdown()
Definition: TcpSocket.cpp:172
visionary::TcpSocket::send
int send(const std::vector< std::uint8_t > &buffer) override
Definition: TcpSocket.cpp:192
visionary::TcpSocket::openServer
int openServer(uint16_t port)
Definition: TcpSocket.cpp:80
visionary::TcpSocket::connect
int connect(const std::string &hostname, uint16_t port)
Definition: TcpSocket.cpp:26
visionary::TcpSocket::recv
int recv(std::vector< std::uint8_t > &buffer, std::size_t maxBytesToReceive) override
Definition: TcpSocket.cpp:198


sick_safevisionary_base
Author(s):
autogenerated on Sat Oct 21 2023 02:24:26