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
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
00064 he = gethostbyname(this->host.c_str());
00065 WRITE_MSG("MMSTCPClient", "hostname: %s", he->h_name);
00066
00067
00068 ia.s_addr = *((unsigned long int*)*(he->h_addr_list));
00069 this->hostip = inet_ntoa(ia);
00070
00071
00072 if ((this->s = socket(AF_INET, SOCK_STREAM, 0))<=0) {
00073 WRITE_ERR("MMSTCPClient", "socket() failed");
00074 return false;
00075 }
00076
00077
00078 memset(&sa, 0, sizeof(sa));
00079 sa.sin_family = AF_INET;
00080 sa.sin_port = htons(this->port);
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
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
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
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
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
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
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
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 }