YARP
Yet Another Robot Platform
TcpStream.cpp
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-FileCopyrightText: 2010 Anne van Rossum <anne@almende.com>
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <yarp/conf/system.h>
8 #ifndef YARP_HAS_ACE
9 
10 
11 // General files
12 #include <sys/socket.h>
13 #include <cstdio>
14 
15 #include <yarp/os/impl/TcpStream.h>
17 
18 using namespace yarp::os::impl;
19 
20 namespace {
21 YARP_OS_LOG_COMPONENT(TCPSTREAM_POSIX, "yarp.os.impl.TcpStream.posix")
22 }
23 
24 /* **************************************************************************************
25  * Implementation of TcpStream
26  * **************************************************************************************/
27 
28 TcpStream::TcpStream() = default;
29 
30 TcpStream::~TcpStream() = default;
31 
32 int TcpStream::open()
33 {
34  set_handle(socket(AF_INET, SOCK_STREAM, 0));
35  if (get_handle() == -1) {
36  yCError(TCPSTREAM_POSIX, "At TcpStream::open there was an error: %d, %s", errno, strerror(errno));
37  return -1;
38  }
39  return 0;
40 }
41 
42 int TcpStream::get_local_addr(sockaddr & sa)
43 {
44  int len = sizeof(sa);
45  if (::getsockname(get_handle(), &sa, reinterpret_cast<socklen_t*>(&len)) == -1) {
46  return -1;
47  }
48  return 0;
49 }
50 
51 int TcpStream::get_remote_addr (sockaddr & sa)
52 {
53  int len = sizeof(sa);
54  if (::getpeername(get_handle(), &sa, reinterpret_cast<socklen_t*>(&len)) == -1) {
55  return -1;
56  }
57  return 0;
58 }
59 
60 
61 #endif
#define yCError(component,...)
Definition: LogComponent.h:154
#define YARP_OS_LOG_COMPONENT(name, name_string)
Definition: LogComponent.h:34
The components from which ports and connections are built.