当前位置: 技术问答>linux和unix
代码比较长, 但是编译连接都通过了,就是结果不对。 希望各位帮帮忙, 时间紧急啊。老师要完成的作业
来源: 互联网 发布时间:2016-06-24
本文导语: #include #include #include struct STRU_HEAD { int length; unsigned char version; unsigned char type; unsigned char encrypt; unsigned char reserved; }; //封装包头和消息体 int ...
#include
#include
#include
struct STRU_HEAD
{
int length;
unsigned char version;
unsigned char type;
unsigned char encrypt;
unsigned char reserved;
};
//封装包头和消息体
int make_request_msg(char* p_command,unsigned char command_type,char* buffer)
{
//封装包头
char* tmp = p_command;
struct STRU_HEAD head;
head.length = 8+sizeof(buffer);
head.version = 2;
head.type = command_type;
head.encrypt = 0x01;
head.reserved = 0x00;
memcpy(p_command,&head.length,sizeof(head.length));
p_command += sizeof(head.length);
memcpy(p_command,&head.version,sizeof(head.version));
p_command += sizeof(head.version);
memcpy(p_command,&head.type,sizeof(head.type));
p_command += sizeof(head.type);
memcpy(p_command,&head.encrypt,sizeof(head.encrypt));
p_command += sizeof(head.encrypt);
memcpy(p_command,&head.reserved,sizeof(head.reserved));
p_command += sizeof(head.reserved);
//封装消息体
memcpy(p_command,buffer,strlen(buffer));
p_command += strlen(buffer);
printf("The make_request_msg p_command is : %sn", p_command);
return (int)(p_command-tmp);
}
// 解析上面make_request_msg()函数所封装的消息包 到res_info 和 msg_id 两部分当中去
int deal_response(char* buffer,char* res_info, unsigned int *msg_id)
{
char* tmp;
char tmp_msg_id[10]={0};
printf("The deal_response buffer = [%s]n",buffer);
tmp =NULL;
if(0 != buffer[0])
{
tmp = strchr(buffer,'-');
if(tmp != NULL)
{
strncpy(tmp_msg_id,buffer,(tmp-buffer));
*msg_id = atoi(tmp_msg_id);
strcpy(res_info,tmp+1);
}
return 0;
}
printf("The deal_response msg_id is : %dn", *msg_id);
printf("The deal_response res_info is : %sn", res_info);
return 1;
}
int main(void)
{
char p_command[50] = {0};
char res_info[68] = {0};
unsigned int msg_id = 0;
char *buffer = "250-filename";
unsigned char command_type = 3;
int length ;
length = make_request_msg(p_command, command_type,buffer);
printf("The main p_command is %sn", p_command);
printf("The main message length is %dn", length);
deal_response(p_command,res_info,&msg_id);
printf("The main ret_info is %sn", res_info);
printf("The main msg_id is %ldn", msg_id);
return 0;
}
我希望的结果是:
(1)通过make_request_msg()函数 把char *buffer = "250-filename"和
unsigned char command_type = 3 封装到p_command[50]当中去。
(2)利用上面生成返回的p_command[50]这个字串再通过 deal_response(p_command,res_info,&msg_id)函数
来解析出当时通过make_request_msg()函数封装到里面的buffer = "250-filename"这条消息。 并把它分解成"250-"和"filename"这两部分。
我的感觉可能是函数当中调用的函数内部临时变量 在程序结束后并没把结果返回到函数外面就已经销毁。 所以在main函数当中
没有得到函数执行完后的 变量结果。 所以希望各位帮忙下 。怎样使函数当中的 变量结果能够在 main函数里显示出来啊?
可能用到指针和引用一些方法 是不是??
|
你先测试单个函数,看功能正常不?
|
有好几个错误,都给你改了
#include
#include
#include
struct STRU_HEAD
{
int length;
unsigned char version;
unsigned char type;
unsigned char encrypt;
unsigned char reserved;
};
//封装包头和消息体
int make_request_msg(char* p_command,unsigned char command_type,char* buffer)
{
//封装包头
char* tmp = p_command;
struct STRU_HEAD head;
// head.length = 8+sizeof(buffer); //sizeof(buffer)必为4,所以改成下面的
head.length = 8+strlen(buffer);
head.version = 2;
head.type = command_type;
head.encrypt = 0x01;
head.reserved = 0x00;
memcpy(p_command,&head.length,sizeof(head.length));
p_command += sizeof(head.length);
memcpy(p_command,&head.version,sizeof(head.version));
p_command += sizeof(head.version);
memcpy(p_command,&head.type,sizeof(head.type));
p_command += sizeof(head.type);
memcpy(p_command,&head.encrypt,sizeof(head.encrypt));
p_command += sizeof(head.encrypt);
memcpy(p_command,&head.reserved,sizeof(head.reserved));
p_command += sizeof(head.reserved);
//封装消息体
memcpy(p_command,buffer,strlen(buffer));
p_command += strlen(buffer);
printf("The make_request_msg p_command is : %sn", p_command);//这是要打印什么?p_command已经指向包末尾了,
return (int)(p_command-tmp); //就算是指向包头的起始地址,也不能打印,因为包头中含有'',(就是你那个保留值reserved),后面的字符串都被忽略了
}
// 解析上面make_request_msg()函数所封装的消息包 到res_info 和 msg_id 两部分当中去
int deal_response(char* buffer,char* res_info, unsigned int *msg_id)
{
char* tmp;
char tmp_msg_id[10]={0};
printf("The deal_response buffer = [%s]n",buffer); //上面说了为什么不能打印
tmp =NULL;
if(0 != buffer[0])
{
tmp = strchr(buffer+8,'-'); //找-应该跳过前面的8个字节
if(tmp != NULL)
{
strncpy(tmp_msg_id,buffer+8,(tmp-buffer-8));//找id也应该跳过前面的8个字节
*msg_id = atoi(tmp_msg_id);
strcpy(res_info,tmp+1);
}
return 0;
}
printf("The deal_response msg_id is : %dn", *msg_id);
printf("The deal_response res_info is : %sn", res_info);
return 1;
}
int main(void)
{
char p_command[50] = {0};
char res_info[68] = {0};
unsigned int msg_id = 0;
char *buffer = "250-filename";
unsigned char command_type = 3;
int length ;
length = make_request_msg(p_command, command_type,buffer);
printf("The main p_command is %sn", p_command); //必是乱码
printf("The main message length is %dn", length);
deal_response(p_command,res_info,&msg_id);
printf("The main ret_info is %sn", res_info);
printf("The main msg_id is %ldn", msg_id);
return 0;
}
|
由于昨晚改的匆忙,还有一点没有提到:
if(0 != buffer[0])这里是不对的,因为buffer[0]是消息包的长度,必>=8,
该成
if(8!= buffer[0])
if(0 != buffer[0])这里是不对的,因为buffer[0]是消息包的长度,必>=8,
该成
if(8!= buffer[0])
|
printf("The main p_command:length: %dn",*p_command);
printf("The main p_command:version: %dn",*(p_command+4));
printf("The main p_command:type: %dn",*(p_command+5));
printf("The main p_command:encrypt: %dn",*(p_command+6));
printf("The main p_command:reserved: %dn",*(p_command+7));
printf("The main p_command:msg: %sn",p_command+8);
printf("The main p_command:version: %dn",*(p_command+4));
printf("The main p_command:type: %dn",*(p_command+5));
printf("The main p_command:encrypt: %dn",*(p_command+6));
printf("The main p_command:reserved: %dn",*(p_command+7));
printf("The main p_command:msg: %sn",p_command+8);
|
uping