Logo
  • Main Page
  • Related Pages
  • Modules
  • Classes
  • Files

mmstcpclient.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2005-2007 Stefan Schwarzer, Jens Schneider,             *
00003  *                           Matthias Hardt, Guido Madaus                  *
00004  *                                                                         *
00005  *   Copyright (C) 2007-2008 BerLinux Solutions GbR                        *
00006  *                           Stefan Schwarzer & Guido Madaus               *
00007  *                                                                         *
00008  *   Copyright (C) 2009-2012 BerLinux Solutions GmbH                       *
00009  *                                                                         *
00010  *   Authors:                                                              *
00011  *      Stefan Schwarzer   <stefan.schwarzer@diskohq.org>,                 *
00012  *      Matthias Hardt     <matthias.hardt@diskohq.org>,                   *
00013  *      Jens Schneider     <jens.schneider@diskohq.org>,                   *
00014  *      Guido Madaus       <guido.madaus@diskohq.org>,                     *
00015  *      Patrick Helterhoff <patrick.helterhoff@diskohq.org>,               *
00016  *      René Bählkow       <rene.baehlkow@diskohq.org>                     *
00017  *                                                                         *
00018  *   This library is free software; you can redistribute it and/or         *
00019  *   modify it under the terms of the GNU Lesser General Public            *
00020  *   License version 2.1 as published by the Free Software Foundation.     *
00021  *                                                                         *
00022  *   This library is distributed in the hope that it will be useful,       *
00023  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00024  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00025  *   Lesser General Public License for more details.                       *
00026  *                                                                         *
00027  *   You should have received a copy of the GNU Lesser General Public      *
00028  *   License along with this library; if not, write to the                 *
00029  *   Free Software Foundation, Inc.,                                       *
00030  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
00031  **************************************************************************/
00032 
00033 #include "mmstools/mmstcpclient.h"
00034 #include "mmstools/tools.h"
00035 #include <netdb.h>
00036 #include <arpa/inet.h>
00037 #include <string.h>
00038 #include <cerrno>
00039 #include <stdlib.h>
00040 
00041 MMSTCPClient::MMSTCPClient(string host, unsigned int port) {
00042     this->host = host;
00043     this->port = port;
00044     this->s = -1;
00045 }
00046 
00047 bool MMSTCPClient::isConnected() {
00048     return (this->s>=0);
00049 }
00050 
00051 bool MMSTCPClient::connectToServer() {
00052     struct hostent      *he;
00053     struct in_addr      ia;
00054     struct sockaddr_in  sa;
00055 
00056     WRITE_MSG("MMSTCPClient", "connect to %s:%u",this->host.c_str(), this->port);
00057 
00058     if (this->s>=0) {
00059         WRITE_MSG("MMSTCPClient", "already connected");
00060         return true;
00061     }
00062 
00063     // get host ip in network byte order
00064     he = gethostbyname(this->host.c_str());
00065     WRITE_MSG("MMSTCPClient", "hostname: %s", he->h_name);
00066 
00067     // get host ip in numbers-and-dots
00068     ia.s_addr = *((unsigned long int*)*(he->h_addr_list));
00069     this->hostip = inet_ntoa(ia);
00070 
00071     // get a socket
00072     if ((this->s = socket(AF_INET, SOCK_STREAM, 0))<=0) {
00073         WRITE_ERR("MMSTCPClient", "socket() failed");
00074         return false;
00075     }
00076 
00077     // connect to hostip
00078     memset(&sa, 0, sizeof(sa));
00079     sa.sin_family = AF_INET;
00080     sa.sin_port = htons(this->port); //this->port / 0x100 + (this->port % 0x100) * 0x100;
00081     sa.sin_addr.s_addr = inet_addr(this->host.c_str());
00082     if (connect(this->s, (struct sockaddr *)&sa, sizeof(struct sockaddr_in))!=0) {
00083         WRITE_ERR("MMSTCPClient", "connect to %s:%d failed: %s",this->host.c_str(), this->port, strerror(errno));
00084         disconnectFromServer();
00085         return false;
00086     }
00087 
00088     // connection established
00089     return true;
00090 }
00091 
00092 bool MMSTCPClient::disconnectFromServer() {
00093     if (this->s<0) return true;
00094     close(this->s);
00095     this->s = -1;
00096     return true;
00097 }
00098 
00099 bool MMSTCPClient::sendString(string rbuf) {
00100     //char  mybuf[128000+1];
00101     char    *mybuf;
00102     int     len, from;
00103 
00104     if (!isConnected()) {
00105         WRITE_ERR("MMSTCPClient", "in send not connected");
00106         return false;
00107     }
00108 
00109     mybuf = (char *)malloc(rbuf.size() +1);
00110 
00111     // send request
00112     from = 0;
00113     do {
00114         strcpy(mybuf, (rbuf.substr(from, sizeof(mybuf)-1)).c_str());
00115         if (!*mybuf) break;
00116         if ((len = send(this->s, mybuf, strlen(mybuf), 0))<0) return false;
00117         from+=len;
00118     } while (len>0);
00119     send(this->s, "\0", 1, 0);
00120     WRITE_MSG("MMSTCPClient", "sent %d bytes", from + 1);
00121 
00122     free(mybuf);
00123     return true;
00124 }
00125 
00126 bool MMSTCPClient::receiveString(string *abuf) {
00127     char    *mybuf;
00128     int     len;
00129 
00130     if (!isConnected()) return false;
00131 
00132     mybuf = (char *)malloc(128000 +1);
00133 
00134     // receive answer
00135     *abuf = "";
00136     do {
00137         if ((len = recv(this->s, mybuf, sizeof(mybuf)-1, 0))<0) return false;
00138         if (len>0) {
00139             mybuf[len]=0;
00140             (*abuf)+= mybuf;
00141         }
00142     } while ((len>0)&&(mybuf[len-1]!=0));
00143     free(mybuf);
00144     return true;
00145 }
00146 
00147 bool MMSTCPClient::receiveString(string *abuf, int buflen) {
00148     //char  mybuf[128000+1];
00149     char    *mybuf;
00150     ssize_t     len;
00151     ssize_t received=0;
00152 
00153     if (!isConnected()) return false;
00154 
00155     mybuf = (char*)malloc(buflen+1);
00156 
00157     memset(mybuf,0,buflen+1);
00158 
00159     // receive answer
00160     *abuf = "";
00161     do {
00162         if ((len = recv(this->s, &mybuf[received], buflen-received, MSG_WAITALL))<0) return false;
00163 
00164         received+=len;
00165         if (len>0) {
00166             mybuf[len]=0;
00167         }
00168     } while(received < buflen);
00169 
00170     *abuf= string(mybuf);
00171     free(mybuf);
00172 
00173     return true;
00174 }
00175 
00176 bool MMSTCPClient::peekString(string *abuf, int buflen) {
00177     char    mybuf[128000+1];
00178     int     len;
00179     int     received=0;
00180 
00181     if (!isConnected()) return false;
00182     memset(mybuf,0,128000+1);
00183 
00184     // receive answer
00185     *abuf = "";
00186     do {
00187         if ((len = recv(this->s, &mybuf[received], buflen-received, MSG_PEEK))<0) return false;
00188 
00189         received+=len;
00190         if (len>0) {
00191             mybuf[len]=0;
00192         }
00193     } while(received < buflen);
00194 
00195     (*abuf) = mybuf;
00196     return true;
00197 }
00198 
00199 bool MMSTCPClient::sendAndReceive(string rbuf, string *abuf) {
00200     bool    retcode = false;
00201 
00202     if (!connectToServer()) {
00203         return false;
00204     }
00205 
00206 
00207     WRITE_MSG("MMSTCPClient", "send string");
00208     if (sendString(rbuf)) {
00209         WRITE_MSG("MMSTCPClient", "receive string");
00210         if (receiveString(abuf)) {
00211             WRITE_MSG("MMSTCPClient", "receive string");
00212             retcode = true;
00213         }
00214     }
00215     disconnectFromServer();
00216 
00217     return retcode;
00218 }
00219 
00220 
00221 void MMSTCPClient::setAddress(string &host, unsigned int port)  {
00222     this->host = host;
00223     this->port = port;
00224 }
00225 
00226 void MMSTCPClient::setAddress(const char *host, unsigned int port) {
00227     this->host = host;
00228     this->port = port;
00229 }

Generated by doxygen