enum isBOOL{ isFalse=0, isTrue }; enum Choose { TcpHeartbeat=200, TcpExeCmd }; // 定义结构体 struct DataPacket { int sockfd; enum Choose choose; char cmdBuf[BUFFER_SIZE]; char returnValue[BUFFER_SIZE]; }; struct DataPacket datapacket; struct DataPacket receivePackets; int SetUpTCPtoSendInformation(char option[], char **command) { static int isFirstCall = 1; pthread_mutex_lock(&mutex); // 加锁 if (isFirstCall && strstr(option, "set")) { if (TCPCommand != NULL) { free(TCPCommand); } TCPCommand = malloc(strlen(*command) + 1); if (TCPCommand == NULL) { printf("Failed to set and obtain TCP command variable memory allocation\n"); goto fail; } strcpy(TCPCommand, *command); printf("set:%s\n", TCPCommand); isFirstCall = 0; goto succeed; } else if (!isFirstCall && strstr(option, "get") && TCPCommand != NULL && strlen(TCPCommand)) { free(*command); *command = malloc(strlen(TCPCommand) + 1); strcpy(*command, TCPCommand); printf("get:%s\n", *command); memset(TCPCommand, '\0', strlen(TCPCommand)); free(TCPCommand); TCPCommand = NULL; isFirstCall = 1; goto succeed; } memset(*command, 0, sizeof(*command)); strcpy(*command, ""); fail: pthread_mutex_unlock(&mutex); // 解锁 return 0; succeed: pthread_mutex_unlock(&mutex); // 解锁 return 1; } memset(&receivePackets, 0, sizeof(receivePackets)); int dataLen = recv(fd, &receivePackets, sizeof(receivePackets), 0); char *SendString = NULL; int result = SetUpTCPtoSendInformation("get", &SendString); if (result && SendString != NULL && strlen(SendString) > 0)详细代码
时间: 2024-04-12 10:31:36 浏览: 140
根据你提供的代码,我注意到你使用了一个名为 `datapacket` 的结构体来存储发送数据包的信息,并使用 `recv` 函数接收数据包。
在之后的代码中,你调用了 `SetUpTCPtoSendInformation("get", &SendString)` 来获取要发送的字符串。然后,你检查了 `SendString` 是否为 `NULL` 并且长度是否大于零。
这是一个合理的逻辑检查。如果条件满足,你可以使用 `SendString` 的值来执行发送操作。
请注意,你还需要在调用 `recv` 函数之前,使用 `memset` 函数将接收数据包的结构体 `receivePackets` 初始化为零,以确保接收到的数据正确填充到结构体中。
以下是修改后的代码示例:
```c
enum isBOOL {
isFalse = 0,
isTrue
};
enum Choose {
TcpHeartbeat = 200,
TcpExeCmd
};
struct DataPacket {
int sockfd;
enum Choose choose;
char cmdBuf[BUFFER_SIZE];
char returnValue[BUFFER_SIZE];
};
struct DataPacket datapacket;
struct DataPacket receivePackets;
int SetUpTCPtoSendInformation(char option[], char **command) {
static int isFirstCall = 1;
pthread_mutex_lock(&mutex); // 加锁
if (isFirstCall && strstr(option, "set")) {
if (TCPCommand != NULL) {
free(TCPCommand);
}
TCPCommand = malloc(strlen(*command) + 1);
if (TCPCommand == NULL) {
printf("Failed to set and obtain TCP command variable memory allocation\n");
goto fail;
}
strcpy(TCPCommand, *command);
printf("set:%s\n", TCPCommand);
isFirstCall = 0;
goto succeed;
} else if (!isFirstCall && strstr(option, "get") && TCPCommand != NULL && strlen(TCPCommand)) {
free(*command);
*command = malloc(strlen(TCPCommand) + 1);
strcpy(*command, TCPCommand);
printf("get:%s\n", *command);
memset(TCPCommand, '\0', strlen(TCPCommand));
free(TCPCommand);
TCPCommand = NULL;
isFirstCall = 1;
goto succeed;
}
memset(*command, 0, sizeof(*command));
strcpy(*command, "");
fail:
pthread_mutex_unlock(&mutex); // 解锁
return 0;
succeed:
pthread_mutex_unlock(&mutex); // 解锁
return 1;
}
memset(&receivePackets, 0, sizeof(receivePackets)); // 初始化接收数据包的结构体
int dataLen = recv(fd, &receivePackets, sizeof(receivePackets), 0);
char *SendString = NULL;
int result = SetUpTCPtoSendInformation("get", &SendString);
if (result && SendString != NULL && strlen(SendString) > 0) {
// 在这里执行发送 SendString 的操作
printf("SendString: %s\n", SendString);
}
```
请确保你在使用 `SendString` 之前检查其是否为 `NULL`,以避免访问空指针错误。同时,根据你的实际需求,对发送操作进行相应的处理。
阅读全文