11 #include "LibCyberRadio/Common/Pythonesque.h" 12 #include "LibCyberRadio/NDR651/ClientSocket.h" 22 _serverHostname = hostname;
33 struct sockaddr_in serv_addr;
34 struct hostent *server;
36 BasicStringList::iterator cmd, rsp;
37 bool cmdRspError =
false;
41 server = gethostbyname(_serverHostname.c_str());
42 _sockfd = socket(AF_INET, SOCK_STREAM, 0);
43 bzero((
char *) &serv_addr,
sizeof(serv_addr));
44 serv_addr.sin_family = AF_INET;
45 bcopy((
char *)server->h_addr, (
char *)&serv_addr.sin_addr.s_addr, server->h_length);
46 serv_addr.sin_port = htons(_serverPort);
47 if (connect(_sockfd,(
struct sockaddr *) &serv_addr,
sizeof(serv_addr)) < 0) {
56 FD_SET(_sockfd, &
set);
72 if (select(FD_SETSIZE, &
set, NULL, NULL, &tout)>0)
101 return write(_sockfd, cmd.c_str(), cmd.size())!=(
int)cmd.size();
109 tout.tv_sec = (
long int) timeout/1000;
110 tout.tv_usec = (
long int)(1000*timeout)%1000000;
112 FD_SET(_sockfd, &
set);
113 if (select(FD_SETSIZE, &
set, NULL, NULL, &tout)>0) {
115 rsp.push_back(std::string(_rxBuff));
130 bool txError=
false, rxError=
false;
132 tout.tv_sec = (
long int) timeout/1000;
133 tout.tv_usec = (
long int)(1000*timeout)%1000000;
136 FD_SET(_sockfd, &
set);
137 while((!rxError)&&((rsp.size()==0)||((rsp.size()>0)&&(rsp.back().find(
"OK>")==std::string::npos)))) {
139 if (select(FD_SETSIZE, &
set, NULL, NULL, &tout)>0) {
144 for (i=0; i<x.size(); i++) {
159 for (BasicStringList::iterator it=rsp.begin(); it<rsp.end(); it++) {
160 std::cerr <<
"\tRSP = '" << *it <<
"'" << std::endl;
163 return txError||rxError;
static std::string Strip(const std::string &str, const std::string &chars=" \\\)
Strips both leading and trailing whitespace from the given string.
bool connectToServer(void)
Connects to the server.
bool disconnect(void)
Disconnects from the server.
bool sendCmdAndGetRsp(const std::string &cmd, BasicStringList &rsp, float timeout, bool print)
Sends a command to the server and gets the response.
bool isConnected(void)
Gets whether or not the socket is connected.
static BasicStringList Split(const std::string &str, const std::string &sep, int maxsplit=INT_MAX)
Splits the given string into a list of string tokens.
bool sendCmd(const std::string &cmd)
Sends a command to the server.
BASIC_LIST_CONTAINER< std::string > BasicStringList
Type representing a list of strings.
Defines functionality for LibCyberRadio applications.
bool getRsp(BasicStringList &rsp, float timeout)
Gets a command response from the server.
static std::string Join(const BasicStringList &vec, const std::string &sep)
Joins a list of string tokens, concatenating them into a single string.
ClientSocket(const std::string &hostname, unsigned int port, bool debug=true)
Constructs a ClientSocket object.
virtual ~ClientSocket()
Destroys a ClientSocket object.