28 #include <sys/socket.h> 29 #include <netinet/in.h> 30 #include <sys/types.h> 65 _sock = ::socket(PF_INET, SOCK_STREAM, 0);
68 Logger::log() << Logger::Error <<
"Socket creation failed." << Logger::endl;
72 struct sockaddr_in localsocket;
73 struct sockaddr remote;
74 int adrlen =
sizeof(remote);
76 localsocket.sin_family = AF_INET;
77 localsocket.sin_port = htons(_port);
78 localsocket.sin_addr.s_addr = INADDR_ANY;
79 if( ::bind(_sock, (
struct sockaddr*)&localsocket,
sizeof(localsocket) ) < 0 )
85 #define TRY_OTHER_PORTS 87 #ifdef TRY_OTHER_PORTS 90 while( errno == EADDRINUSE && i < 5 && r < 0 )
92 localsocket.sin_port = htons(_port + i);
93 r = ::bind(_sock, (
struct sockaddr*)&localsocket,
sizeof(localsocket) );
98 Logger::log() << Logger::Info <<
"Port occupied, use port " << (_port+i-1) <<
" instead." << Logger::endl;
101 if( errno == EADDRINUSE )
103 Logger::log() << Logger::Error <<
"Binding of port failed: address already in use." << Logger::endl;
105 Logger::log() << Logger::Error <<
"Binding of port failed with errno " << errno << Logger::endl;
109 #ifdef TRY_OTHER_PORTS 114 if( ::listen(_sock, 2) < 0 )
116 Logger::log() << Logger::Info <<
"Cannot listen on socket" << Logger::endl;
122 int socket = ::accept( _sock, &remote,
123 reinterpret_cast<socklen_t*>(&adrlen) );
130 Logger::log() << Logger::Info <<
"Incoming connection" << Logger::endl;
131 _marshaller->
addConnection(
new Orocos::TCP::Socket(socket) );
138 :
Activity(10), _marshaller(marshaller)
144 Logger::log() << Logger::Info <<
"Starting server on port " << port << Logger::endl;
145 this->Activity::start();
169 Logger::log() << Logger::Error <<
"Could not listen on port " << _port << Logger::endl;
171 Logger::log() << Logger::Info <<
"Shutting down server" << Logger::endl;
182 int sock = ::socket(PF_INET, SOCK_STREAM, 0);
185 struct sockaddr_in socket;
186 socket.sin_family = AF_INET;
187 socket.sin_port = htons(_port);
188 socket.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
189 ::connect( sock, (
struct sockaddr*)&socket,
sizeof(socket) );
200 ListenThread::_instance =
new ListenThread( marshaller, port );
206 ListenThread::_instance->breakLoop();
214 TcpReporting::TcpReporting(std::string fr_name )
216 port_prop(
"port",
"port to listen/send to",3142)
bool addMarshaller(RTT::marsh::MarshallInterface *headerM, RTT::marsh::MarshallInterface *bodyM)
A component which writes data reports to a tcp/ip socket. It can serve different clients. It uses a ASCI-based protocol.
static void destroyInstance()
ListenThread(RTT::SocketMarshaller *marshaller, unsigned short port)
const RTT::PropertyBag * getReport()
RTT::SocketMarshaller * _marshaller
static ListenThread * _instance
Property< T > & addProperty(const std::string &name, T &attr)
A Component for periodically reporting Component Port contents to a human readable text format...
PropertyBag * properties()
ORO_LIST_COMPONENT_TYPE(OCL::logging::GenerationalFileAppender)
RTT::Property< unsigned int > port_prop
static void createInstance(RTT::SocketMarshaller *marshaller, unsigned short port=3142)
void addConnection(OCL::TCP::Socket *os)
RTT::SocketMarshaller * fbody