00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
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 #include <unistd.h>
00041
00042 MMSTCPClient::MMSTCPClient(string host, unsigned int port) {
00043 this->host = host;
00044 this->port = port;
00045 this->s = -1;
00046 }
00047
00048 bool MMSTCPClient::isConnected() {
00049 return (this->s>=0);
00050 }
00051
00052 bool MMSTCPClient::connectToServer() {
00053 struct hostent *he;
00054 struct in_addr ia;
00055 struct sockaddr_in sa;
00056
00057 WRITE_MSG("MMSTCPClient", "connect to %s:%u",this->host.c_str(), this->port);
00058
00059 if (this->s>=0) {
00060 WRITE_MSG("MMSTCPClient", "already connected");
00061 return true;
00062 }
00063
00064
00065 he = gethostbyname(this->host.c_str());
00066 WRITE_MSG("MMSTCPClient", "hostname: %s", he->h_name);
00067
00068
00069 ia.s_addr = *((unsigned long int*)*(he->h_addr_list));
00070 this->hostip = inet_ntoa(ia);
00071
00072
00073 if ((this->s = socket(AF_INET, SOCK_STREAM, 0))<=0) {
00074 WRITE_ERR("MMSTCPClient", "socket() failed");
00075 return false;
00076 }
00077
00078
00079 memset(&sa, 0, sizeof(sa));
00080 sa.sin_family = AF_INET;
00081 sa.sin_port = htons(this->port);
00082 sa.sin_addr.s_addr = inet_addr(this->host.c_str());
00083 if (connect(this->s, (struct sockaddr *)&sa, sizeof(struct sockaddr_in))!=0) {
00084 WRITE_ERR("MMSTCPClient", "connect to %s:%d failed: %s",this->host.c_str(), this->port, strerror(errno));
00085 disconnectFromServer();
00086 return false;
00087 }
00088
00089
00090 return true;
00091 }
00092
00093 bool MMSTCPClient::disconnectFromServer() {
00094 if (this->s<0) return true;
00095 close(this->s);
00096 this->s = -1;
00097 return true;
00098 }
00099
00100 bool MMSTCPClient::sendString(string rbuf) {
00101
00102 char *mybuf;
00103 int len, from;
00104
00105 if (!isConnected()) {
00106 WRITE_ERR("MMSTCPClient", "in send not connected");
00107 return false;
00108 }
00109
00110 mybuf = (char *)malloc(rbuf.size() +1);
00111
00112
00113 from = 0;
00114 do {
00115 strcpy(mybuf, (rbuf.substr(from, sizeof(mybuf)-1)).c_str());
00116 if (!*mybuf) break;
00117 if ((len = send(this->s, mybuf, strlen(mybuf), 0))<0) return false;
00118 from+=len;
00119 } while (len>0);
00120 send(this->s, "\0", 1, 0);
00121 WRITE_MSG("MMSTCPClient", "sent %d bytes", from + 1);
00122
00123 free(mybuf);
00124 return true;
00125 }
00126
00127 bool MMSTCPClient::receiveString(string *abuf) {
00128 char *mybuf;
00129 int len;
00130
00131 if (!isConnected()) return false;
00132
00133 mybuf = (char *)malloc(128000 +1);
00134
00135
00136 *abuf = "";
00137 do {
00138 if ((len = recv(this->s, mybuf, sizeof(mybuf)-1, 0))<0) return false;
00139 if (len>0) {
00140 mybuf[len]=0;
00141 (*abuf)+= mybuf;
00142 }
00143 } while ((len>0)&&(mybuf[len-1]!=0));
00144 free(mybuf);
00145 return true;
00146 }
00147
00148 bool MMSTCPClient::receiveString(string *abuf, int buflen) {
00149
00150 char *mybuf;
00151 ssize_t len;
00152 ssize_t received=0;
00153
00154 if (!isConnected()) return false;
00155
00156 mybuf = (char*)malloc(buflen+1);
00157
00158 memset(mybuf,0,buflen+1);
00159
00160
00161 *abuf = "";
00162 do {
00163 if ((len = recv(this->s, &mybuf[received], buflen-received, MSG_WAITALL))<0) return false;
00164
00165 received+=len;
00166 if (len>0) {
00167 mybuf[len]=0;
00168 }
00169 } while(received < buflen);
00170
00171 *abuf= string(mybuf);
00172 free(mybuf);
00173
00174 return true;
00175 }
00176
00177 bool MMSTCPClient::peekString(string *abuf, int buflen) {
00178 char mybuf[128000+1];
00179 int len;
00180 int received=0;
00181
00182 if (!isConnected()) return false;
00183 memset(mybuf,0,128000+1);
00184
00185
00186 *abuf = "";
00187 do {
00188 if ((len = recv(this->s, &mybuf[received], buflen-received, MSG_PEEK))<0) return false;
00189
00190 received+=len;
00191 if (len>0) {
00192 mybuf[len]=0;
00193 }
00194 } while(received < buflen);
00195
00196 (*abuf) = mybuf;
00197 return true;
00198 }
00199
00200 bool MMSTCPClient::sendAndReceive(string rbuf, string *abuf) {
00201 bool retcode = false;
00202
00203 if (!connectToServer()) {
00204 return false;
00205 }
00206
00207
00208 WRITE_MSG("MMSTCPClient", "send string");
00209 if (sendString(rbuf)) {
00210 WRITE_MSG("MMSTCPClient", "receive string");
00211 if (receiveString(abuf)) {
00212 WRITE_MSG("MMSTCPClient", "receive string");
00213 retcode = true;
00214 }
00215 }
00216 disconnectFromServer();
00217
00218 return retcode;
00219 }
00220
00221
00222 void MMSTCPClient::setAddress(string &host, unsigned int port) {
00223 this->host = host;
00224 this->port = port;
00225 }
00226
00227 void MMSTCPClient::setAddress(const char *host, unsigned int port) {
00228 this->host = host;
00229 this->port = port;
00230 }