2 #include <sys/socket.h>
6 #include "Exceptions.hpp"
8 const char CRLF
[] = { 13, 10, 0 };
10 Telnet::Telnet( int sock
)
23 // Read all that's waiting on the socket
27 int size
= recv( m_sock
, tmpBuf
, 1024, 0 );
33 // No error, just nothing to be read from socket
37 throw strerror( errno
);
41 throw ConnectionTerminatedException
;
44 buf
.append( tmpBuf
, size
);
47 // Parse according to telnet protocol
49 for( unsigned int i=0; i<buf.size(); i++ )
55 m_readBuf
.append( buf
);
57 return m_readBuf
.find( CRLF
) != std::string::npos
;
60 void Telnet::Write( const std::string
& msg
)
62 std::string
buf( msg
);
66 char *ptr
= (char*)buf
.c_str();
68 while( pos
!= buf
.size() )
70 int size
= send( m_sock
, ptr
, buf
.size() - pos
, 0 );
74 throw strerror( errno
);
78 throw ConnectionTerminatedException
;
86 std::string
Telnet::GetBuf()
90 unsigned int pos
= m_readBuf
.find( CRLF
);
91 if( pos
== std::string::npos
)
93 throw "Trying to get telnet buffer when buffer not ready";
96 ret
= m_readBuf
.substr( 0, pos
);
98 m_readBuf
.erase( 0, pos
+ 2 );