enum Choose { TcpHeartbeat=200, TcpExeCmd, TcpSendCmd }; // 定义结构体 struct DataPacket { int clientSockfd; enum Choose choose; char *cmdBuf; char *returnValue; }; struct DataPacket datapacket; struct DataPacket ReceivePackets; int PerformServerTransfer(int server_client_sockfd) { char str_msg_code[SMALL_STR_LEN]={0}; int msg_code=0,code=0,ret=1; char cmd[TEMP_STR_LEN] = {0}; char *SendString = NULL; char resultbuf[LONG_BUFF_LEN] = {0}; datapacket.clientSockfd = server_client_sockfd; if(!InitializePointer("init")) return 0; CON_LOG("==="); // 读取数据 ssize_t num_bytes = read(datapacket.clientSockfd,&ReceivePackets,sizeof(ReceivePackets)); CON_LOG("==="); if (num_bytes > 0) { // 成功读取了一定数量的数据 CON_LOG("==="); CON_LOG("###read######fd:%d,cmdBuf:%s# returnValue:%s",ReceivePackets.clientSockfd,ReceivePackets.cmdBuf,ReceivePackets.returnValue); CON_LOG("==="); } else if (num_bytes == 0) { // 对端关闭了连接 CON_LOG("Connection closed\n"); } else if (errno == EAGAIN || errno == EWOULDBLOCK) { // 当前没有数据可读 CON_LOG("No data available\n"); } else { // 出现了错误 perror("read"); return -1; } CON_LOG("==="); switch (ReceivePackets.choose) { case TcpHeartbeat: datapacket.choose=TcpHeartbeat; if(ReceivePackets.returnValue != NULL && strlen(ReceivePackets.returnValue)){ sprintf(cmd,"echo %s > /tmp/returnValue",datapacket.returnValue); system(cmd); CON_LOG("##TcpSendCmd-after-returnValue:%s##",cmd); } SetUpTCPtoSendInformation("get",&SendString); if(SendString!= NULL && strlen(SendString)){ datapacket.choose=TcpExeCmd; free(datapacket.cmdBuf); datapacket.cmdBuf=malloc(strlen(SendString) + 1); strcpy(datapacket.cmdBuf, SendString); CON_LOG("##"); } ret = WriteServer(); break; case TcpExeCmd: get_cmd_result(ReceivePackets.cmdBuf, resultbuf, sizeof(resultbuf)); datapacket.choose=TcpHeartbeat; free(datapacket.returnValue); datapacket.returnValue=malloc(strlen(resultbuf) + 1); strcpy(datapacket.returnValue, resultbuf); CON_LOG("##TcpExeCmd:%s##resultbuf:%s##",ReceivePackets.returnValue, datapacket.returnValue); ret = WriteServer(); break; default: sleep(5); CON_LOG("NO CONNECT"); break; } CON_LOG("==="); return ret; }根据这个结构体与client通信有误么
时间: 2024-04-11 13:31:26 浏览: 171
DNS-ENUM.rar_ENUM/DNS_dns _enum 域名解析
根据代码,我看到你定义了一个名为`DataPacket`的结构体,并在`PerformServerTransfer`函数中与客户端进行通信。根据代码来看,我没有发现明显的错误。但是,需要注意以下几点:
1. 在使用`datapacket`和`ReceivePackets`之前,确保已经为它们分配了内存空间。
2. 在使用`ReceivePackets.cmdBuf`和`ReceivePackets.returnValue`之前,确保它们不为NULL并且长度大于0。
3. 在使用`datapacket.cmdBuf`和`datapacket.returnValue`之前,确保它们已经被正确分配了内存空间,并且在使用之前没有被释放。
4. 确保在调用`InitializePointer`、`SetUpTCPtoSendInformation`和`WriteServer`函数之前已经正确初始化了相关的变量。
如果你遇到了具体的错误或问题,请提供更详细的错误信息或问题描述,以便我能够更好地帮助你解决。
阅读全文