enum Choose { TcpHeartbeat=200, TcpExeCmd, TcpSendCmd }; // 定义结构体 struct DataPacket { int clientSockfd; enum Choose choose; char *cmdBuf; char *returnValue; }; struct DataPacket datapacket; struct DataPacket ReceivePackets; int InitializePointer(char option[]) { next: if (strstr(option, "init")) { ReceivePackets.cmdBuf = calloc(BUFFER_SIZE, sizeof(char)); ReceivePackets.returnValue = calloc(BUFFER_SIZE, sizeof(char)); datapacket.cmdBuf = calloc(BUFFER_SIZE, sizeof(char)); datapacket.returnValue = calloc(BUFFER_SIZE, sizeof(char)); if (ReceivePackets.cmdBuf == NULL || ReceivePackets.returnValue == NULL || datapacket.cmdBuf == NULL || datapacket.returnValue == NULL) { CON_LOG("memory allocation failed"); goto next; } } else if (strstr(option, "free")) { free(datapacket.cmdBuf); datapacket.cmdBuf = NULL; free(ReceivePackets.returnValue); ReceivePackets.returnValue = NULL; free(datapacket.cmdBuf); datapacket.cmdBuf = NULL; free(ReceivePackets.cmdBuf); ReceivePackets.cmdBuf = NULL; } return 1; } int WriteServer(){ ssize_t bytes_written = write(datapacket.clientSockfd , &datapacket,sizeof(datapacket)); if (bytes_written == -1) { perror("Write error"); goto fail; } else if (bytes_written < sizeof(datapacket)){ CON_LOG("Only partial data was written"); goto fail; } else { CON_LOG("Write successful"); CON_LOG("Write#fd:%d# choose:%d# cmdBuf:%s# returnValue:%s#",datapacket.clientSockfd,datapacket.choose,datapacket.cmdBuf,datapacket.returnValue); } InitializePointer("free"); return 1; } 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; }这段程序可行性和优化
时间: 2024-04-11 12:31:20 浏览: 118
DNS-ENUM.rar_ENUM/DNS_dns _enum 域名解析
这段程序的可行性是可以的,它实现了一个服务器与客户端之间的通信。根据代码逻辑,服务器接收数据包并根据选择的类型进行相应的操作,然后将数据包发送回客户端。
在优化方面,我有几点建议:
1. 对于内存分配和释放,建议在使用完之后立即释放,避免出现内存泄漏。可以在每次使用完后调用相应的free函数来释放内存。
2. 在读取数据时,可以考虑使用非阻塞IO(non-blocking I/O)来提高程序的性能和响应速度。
3. 在处理switch语句时,可以添加一个default选项来处理未知的选择类型,以防止程序出现意外情况。
4. 在使用system函数执行命令时,建议使用更安全的方式,比如使用exec系列函数来执行命令。
5. 对于错误处理,可以考虑添加适当的错误处理代码,例如对write函数返回值进行判断并处理错误情况。
6. 对于网络通信部分,可以考虑添加异常处理代码,以应对网络中断或连接超时等情况。
这些只是一些建议,具体优化还需要根据实际需求和场景进行。希望对你有帮助!如果有任何问题,请随时提问。
阅读全文