16 #include <sys/socket.h>
17 #include <arpa/inet.h>
62 ROS_ERROR(
"Tcp::write: Connection is not open");
69 bytesSent = ::send(*socketPtr, (
const char*)buffer, numberOfBytes, 0);
72 bytesSent = ::send(*socketPtr, buffer, numberOfBytes, 0);
75 if (bytesSent != (
INT32)numberOfBytes)
77 printWarning(
"Tcp::write: Failed to send data to socket.");
133 std::string ipAdrStr;
137 bool result =
open(ipAdrStr, port, enableVerboseDebugOutput);
167 ROS_ERROR(
"Tcp::open: socket() failed, aborting.");
172 ROS_INFO_STREAM(
"sick_scan_xd: Tcp::open: connecting to " << ipAddress <<
":" << port <<
" ...");
175 struct sockaddr_in addr;
176 struct hostent *
server = 0;
177 server = gethostbyname(ipAddress.c_str());
178 memset(&addr, 0,
sizeof(addr));
179 addr.sin_family = AF_INET;
183 memcpy((
char*)&addr.sin_addr.s_addr, (
char*)
server->h_addr,
server->h_length);
185 bcopy((
char*)
server->h_addr, (
char*)&addr.sin_addr.s_addr,
server->h_length);
190 addr.sin_addr.s_addr = inet_addr(ipAddress.c_str());
192 addr.sin_port = htons(port);
201 std::string text =
"Tcp::open: Failed to open TCP connection to " + ipAddress +
":" +
toString(port) +
", aborting.";
203 char msgbuf[256] =
"";
204 int err = WSAGetLastError();
205 FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), msgbuf,
sizeof(msgbuf),
NULL);
206 text = text +
" Connect error " +
toString(WSAGetLastError()) + std::string(msgbuf);
219 ROS_INFO_STREAM(
"sick_scan_xd Tcp::open: connected to " << ipAddress <<
":" << port);
248 else if (result == 0)
266 const UINT16 max_length = 8192;
267 UINT8 inBuffer[max_length];
268 INT32 recvMsgSize = 0;
274 ROS_ERROR(
"Tcp::readInputData: Connection is not open, aborting!");
276 ROS_INFO(
"Tcp::readInputData: Connection is not open, aborting");
291 ret = poll(&fd, 1, 1000);
315 ROS_ERROR(
"Tcp::readInputData: Failed to read data from socket, aborting!");
317 ROS_INFO(
"Tcp::readInputData: Failed to read data from socket, aborting!");
321 else if (recvMsgSize > 0)
338 for (
INT32 i = 0; i < recvMsgSize; i++)
346 else if (recvMsgSize == 0)
350 ROS_ERROR(
"Tcp::readInputData: Read 0 bytes, connection is lost!");
352 ROS_INFO(
"Tcp::readInputData: Read 0 bytes, connection is lost");
479 std::string outString;
480 const UINT16 maxStringLength = 8192;
504 printWarning(
"Receive-String has excessive length (" +
toString(
m_rxString.length()) +
" bytes). Clearing string. On serial devices, incorrect bitrate settings may cause this behaviour.");
516 if ((
m_beVerbose ==
true) && (outString.length() > 0))