Commit 059690d8 by guanghui.cui

socket发送动态分配内存修改实现方式,不使用结构体

parent 296c12ce
......@@ -2,10 +2,10 @@
port=24446
[ODS]
ip=172.16.13.71
ip=103.13.247.72
pushPort=30001
recvPort=30002
socketTimeout=60
socketTimeout=120
[POS]
ip=127.0.0.1
......
......@@ -6,7 +6,7 @@
FILENAME = "./logs/log.log"
MILLISECONDS_WIDTH = 3
PERFORMANCE_TRACKING = false
MAX_LOG_FILE_SIZE = 10485760
MAX_LOG_FILE_SIZE = 20485760
LOG_FLUSH_THRESHOLD = 10
* TRACE:
......@@ -28,4 +28,4 @@
FILENAME = "./logs/info_log.log"
* VERBOSE:
ENABLED = false
\ No newline at end of file
ENABLED = false
......@@ -203,23 +203,41 @@ void TCPClient::setSocketTimeout(int timeout)
//send
bool TCPClient::send(const std::string& message)
{
bool rlt = true;
Packet *buf = nullptr;
bool rlt = true;
int msgLength = message.length();
unsigned int msgNetLen= htonl(msgLength);
char *tmpSend = new char[msgLength + sizeof(msgNetLen)];
memset(tmpSend, 0, msgLength + sizeof(msgNetLen));
memcpy(tmpSend, &msgNetLen, sizeof(msgNetLen));
memcpy(tmpSend + sizeof(msgNetLen), message.data(), msgLength);
//LOG(INFO) << "message length:" << message.length();
int msgLength = message.length();
buf = (Packet *)malloc(msgLength + sizeof(Packet));
memset(buf, 0, msgLength + sizeof(Packet));
buf->msgLen = htonl(message.length());
strcpy(buf->text, message.c_str());
if (writen(m_sockfd, buf, msgLength + sizeof(Packet)) == -1)
if (writen(m_sockfd, tmpSend, msgLength + sizeof(msgNetLen)) == -1)
{
m_bValid = false;
rlt = false;
}
free(buf);
delete[] tmpSend;
return rlt;
//----------------用结构体写法--------------
//bool rlt = true;
//Packet *buf = nullptr;
//
////LOG(INFO) << "message length:" << message.length();
//int msgLength = message.length();
//buf = (Packet *)malloc(msgLength + sizeof(Packet));
//memset(buf, 0, msgLength + sizeof(Packet));
//buf->msgLen = htonl(message.length());
//strcpy(buf->text, message.c_str());
//if (writen(m_sockfd, buf, msgLength + sizeof(Packet)) == -1)
//{
// m_bValid = false;
// rlt = false;
//}
//free(buf);
//return rlt;
}
bool TCPClient::receive(std::string &message)
{
......
......@@ -46,11 +46,11 @@ protected:
class TCPClient : public TCPSocket
{
private:
struct Packet
{
unsigned int msgLen; //数据部分的长度(网络字节序)
char text[1]; //报文的数据部分
};
//struct Packet
//{
// unsigned int msgLen; //数据部分的长度(网络字节序)
// char text[1]; //报文的数据部分
//};
public:
TCPClient():m_bValid(false){}
~TCPClient(){}
......
......@@ -11,6 +11,7 @@
//#pragma comment(lib,"../bin/sqlite3.lib")
#else
#include <pthread.h>
#include <unistd.h>
#endif
#define ELPP_NO_DEFAULT_LOG_FILE
......@@ -254,7 +255,7 @@ int main(int argc,char *argv[])
el::Helpers::installPreRollOutCallback(logRolloutHandler);
LOG(INFO) << "---------software start---------";
LOG(INFO) << "---------"<< "version"<< VERSION <<"---------";
LOG(INFO) << "version:"<< VERSION ;
// 读取配置文件信息
std::string strIniPath(strBinPath.data());
strIniPath.append("config.ini");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment