26 logger = Logger::getLogger(
"Client");
34 bool Client::connection(
string host,
int port)
38 return connectionUnresolv(host, port);
41 struct sockaddr_in *remote;
45 sockfd = create_tcp_socket();
46 ip = get_ip((
char*)host.c_str());
47 fprintf(stderr,
"IP is %s\n", ip);
48 remote = (
struct sockaddr_in *)malloc(
sizeof(
struct sockaddr_in *));
49 remote->sin_family = AF_INET;
50 tmpres = inet_pton(AF_INET, ip, (
void *)(&(remote->sin_addr.s_addr)));
53 perror(
"Can't set remote->sin_addr.s_addr");
58 fprintf(stderr,
"%s is not a valid IP address\n", ip);
61 remote->sin_port = htons(port);
63 if(connect(sockfd, (
struct sockaddr *)remote,
sizeof(
struct sockaddr)) < 0){
64 perror(
"Could not connect");
76 bool Client::connectionUnresolv(
string host,
int port)
78 struct addrinfo hints, *servinfo, *p;
80 char s[INET6_ADDRSTRLEN];
82 memset(&hints, 0,
sizeof hints);
83 hints.ai_family = AF_UNSPEC;
84 hints.ai_socktype = SOCK_STREAM;
85 string sport = CastUtil::lexical_cast<
string>(port);
86 if ((rv = getaddrinfo(host.c_str(), sport.c_str(), &hints, &servinfo)) != 0) {
87 fprintf(stderr,
"getaddrinfo: %s\n", gai_strerror(rv));
92 for(p = servinfo; p != NULL; p = p->ai_next) {
93 if ((sockfd = socket(p->ai_family, p->ai_socktype,
94 p->ai_protocol)) == -1) {
95 perror(
"client: socket");
99 if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
101 perror(
"client: connect");
112 fprintf(stderr,
"client: failed to connect\n");
116 inet_ntop(p->ai_family, get_in_addr((
struct sockaddr *)p->ai_addr),
120 freeaddrinfo(servinfo);
125 void Client::setSocketBlocking()
127 fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFD, 0) | O_NONBLOCK);
130 void Client::setSocketNonBlocking()
132 fcntl(sockfd, F_SETFL, fcntl(sockfd, F_GETFD, 0) | O_SYNC);
135 int Client::sendData(
string data)
138 while(data.length()>0)
140 int tmpres = send(sockfd, data.c_str(), data.length(), 0);
142 perror(
"Can't send data");
144 data = data.substr(tmpres);
149 string Client::getTextData(
string hdrdelm,
string cntlnhdr)
156 BIO* sbio=BIO_new_socket(sockfd,BIO_NOCLOSE);
157 BIO* io=BIO_new(BIO_f_buffer());
161 er = BIO_gets(io,buf,MAXBUFLE-1);
164 if(io!=NULL)BIO_free_all(io);
168 if(!strcmp(buf,hdrdelm.c_str()))
174 string temp(buf, er);
175 temp = temp.substr(0,temp.length()-1);
176 alldat += (temp +
"\n");
177 if(temp.find(cntlnhdr)!=string::npos)
179 std::string cntle = temp.substr(temp.find(
": ")+2);
180 cntle = cntle.substr(0,cntle.length()-1);
183 cntlen = CastUtil::lexical_cast<
int>(cntle);
187 logger <<
"bad lexical cast" <<endl;
190 memset(&buf[0], 0,
sizeof(buf));
197 toRead = MAXBUFLE - 1;
198 er = BIO_read(io,buf,toRead);
201 if(io!=NULL)BIO_free_all(io);
205 string temp(buf, er);
208 memset(&buf[0], 0,
sizeof(buf));
213 int Client::receive(
string& buf,
int flag)
215 char buff[MAXBUFLE+1];
216 memset(buff, 0,
sizeof(buff));
217 int t = recv(sockfd, buff, MAXBUFLE, flag);
219 memset(buff, 0,
sizeof(buff));
223 int Client::receivelen(
string& buf,
int len,
int flag)
226 memset(buff, 0,
sizeof(buff));
227 int t = recv(sockfd, buff, len, flag);
229 memset(buff, 0,
sizeof(buff));
233 int Client::sendlen(
string buf,
int len)
235 return send(sockfd, buf.c_str(), len, 0);
238 string Client::getBinaryData(
int len,
bool isLengthIncluded)
242 char *buf1 =
new char[len+1];
243 memset(buf1, 0, len);
244 recv(sockfd, buf1, len, 0);
245 for (
int var = 0; var < len; ++var) {
246 alldat.push_back(buf1[var]);
248 memset(buf1, 0, len);
250 int leng = getLengthCl(alldat, len);
256 char *buf =
new char[leng+1];
257 memset(buf, 0,
sizeof(buf));
258 recv(sockfd, buf, leng, 0);
259 for (
int var = 0; var < leng; ++var) {
260 alldat.push_back(buf[var]);
262 memset(buf, 0,
sizeof(buf));
267 void Client::closeConnection()
273 bool Client::isConnected()
275 return connected && ClientInterface::isConnected(sockfd);
278 string Client::getData()
282 while ((numbytes = recv(sockfd, buf, MAXBUFLE-1, 0)) == -1)
295 string data(buf,buf+numbytes);
296 memset(&buf[0], 0,
sizeof(buf));