Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface mbed-rtos mbed uniqueCPUID
Fork of bcsdk by
Client.cpp
- Committer:
- webmaster
- Date:
- 2017-09-21
- Revision:
- 8:f2a567ee3a46
- Child:
- 10:aabd720e632c
File content as of revision 8:f2a567ee3a46:
#include "Client.h"
//#include <WINSOCK2.H>
#include "mbed.h"
#include "EthernetInterface.h"
//#include "BlockChain.h"
Client::Client()
{
m_strIP = "101.201.199.150";
m_sPort = 80;
m_url = "/info";
m_host = "testbc.hwelltech.com";
m_token = "962cd39d-6496-4b23-a2c5-85e445069a78";
}
Client::Client(string ip,short port)
{
this->m_strIP = ip;
this->m_sPort = port;
m_url = "/info";
m_host = "testbc.hwelltech.com";
m_token = "962cd39d-6496-4b23-a2c5-85e445069a78";
}
Client::~Client()
{
}
bool Client::build_post_raw(const string & url,const string & content, string & post_raw)
{
return build_post_raw(url,m_host,m_token,content,post_raw);
}
bool Client::build_post_raw(const string & url, const string & host, const string & token, const string & content, string & post_raw)
{
bool ret = true;
post_raw = "";
post_raw.append("POST "+ url + " HTTP/1.1\r\n");
post_raw.append("Host: " + host + "\r\n");
post_raw.append("Connection: keep-alive\r\n");
post_raw.append("accept: application/json\r\n");
post_raw.append("content-type: application/json\r\n");
string authorization = "Basic YmxvY2tnZW5lcmF0b3I6MTM3NzQzMDJlNTcyYmU4MWMxZmRmZjg2NGZiODA2Yjc2NjcxMzg5NzMwZjBjMDYwZDNlODExNTQ4OGRjNjQ2Mg==";
//if(!token.empty())
// authorization = Base64Encode(token)
post_raw.append("authorization: " + authorization + "\r\n");
char pLen[16] = {0};
//sprintf_s(pLen, sizeof(pLen), "%d", content.length());
sprintf(pLen, "%d", content.length());
string strLen = pLen;
post_raw.append("Content-Length: " + strLen + "\r\n");
post_raw.append("\r\n");
post_raw.append(content);
post_raw.append("\r\n\r\n");
return ret;
}
int Client::http_post(const string & url,const string & content,string & strOut)
{
string post_raw = "";
build_post_raw(url,content,post_raw);
return http_post_raw(post_raw,strOut);
}
int Client::http_post_raw(const string &strIn,string &strOut)
{
string strReq;
string strRep;
strReq = strIn;
//SOCKET sClient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
TCPSocketConnection sClient;
/*
if(sClient == INVALID_SOCKET)
{
printf("invalid socket!");
return 0;
}
*/
#ifdef WIN32
// windows下是这样设置超时时间
int recvTimeout = 3 * 1000; //30s
int sendTimeout = 3 * 1000; //30s
setsockopt(sClient, SOL_SOCKET, SO_RCVTIMEO, (char *)&recvTimeout ,sizeof(int));
setsockopt(sClient, SOL_SOCKET, SO_SNDTIMEO, (char *)&sendTimeout ,sizeof(int));
#elif __LINUX__
// linux下是这样设置超时时间
struct timeval timeout = {3,0};
setsockopt(sClient, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,sizeof(struct timeval));
setsockopt(sClient, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout,sizeof(struct timeval));
#else
unsigned int timeout = 1500;
sClient.set_blocking(false, timeout); // Timeout after (1.5)s
#endif
/*
sockaddr_in serAddr;
serAddr.sin_family = AF_INET;
serAddr.sin_port = htons(m_sPort);
serAddr.sin_addr.S_un.S_addr = inet_addr(m_strIP.c_str());
if(connect(sClient, (sockaddr *)&serAddr, sizeof(serAddr)) == SOCKET_ERROR)
{
printf("connect error !");
closesocket(sClient);
return SOCKET_ERROR;
}
*/
while (sClient.connect(m_strIP.c_str(), m_sPort) < 0) {
printf("Unable to connect to (%s) on port (%d)\n", m_strIP.c_str(), m_sPort);
wait(1);
}
printf("Connected to Server at %s\n", m_strIP.c_str());
int ret = 0;
/*
int left = strReq.length();
unsigned int pos = 0;
ret = send(sClient, strReq.c_str(), strReq.length(), 0);
while (ret < left )
{
pos = pos + ret;
ret = send(sClient, strReq.c_str() + pos, strReq.length() - pos, 0);
ret = sClient.send(strReq.c_str() + pos, strReq.length() - pos);
if (ret < 0)
{
return SOCKET_ERROR;
}
}
*/
ret = sClient.send_all((char *)strReq.c_str(), strReq.length());
if (ret < 0)
{
//get last error
//get errno
return ret;
}
#ifdef DEBUG_LOG
cout<< ">>>>>>>>>>HTTP Request (Length = "<< strReq.length() << "):\n" << strReq << endl;
#endif
char recData[BUFSIZE+1] = {0};
ret = 0;
//ret = recv(sClient, recData, BUFSIZE, 0);
ret = sClient.receive(recData, BUFSIZE);
strRep.append(recData);
// 第一次收数据可能没有收完全,所以一直收到没有数据为止,这里会堵塞
while (ret > 0)
{
memset(recData,0,BUFSIZE);
//ret = recv(sClient, recData, BUFSIZE, 0);
ret = sClient.receive(recData, BUFSIZE);
if (ret > 0)
{
strRep.append(recData);
}
}
strOut = strRep;
#ifdef DEBUG_LOG
cout<< ">>>>>>>>>>HTTP Response (Length = "<< strRep.length() << "):\n" << strRep << endl;
#endif
// Clean up
//closesocket(sClient);
sClient.close();
return 0;
}
