2012-07-31 67 views
0

我在嘗試將數據從結構複製到字符數組然後通過套接字發送到服務器時遇到了一個奇怪的問題。下面是我供你參考將C結構複製到字符數組

#include <stdio.h> 
#define MAX_MSG_SIZE 1024 
#pragma pack(1) 
struct msgheader{ 
unsigned int messageLength; 
unsigned int messageType; 
}; 
struct floating_field{ 
unsigned char tag; 
unsigned char length; 
}; 
struct open_req{ 
    struct msgheader mhdr; 
unsigned int invokeid; 
unsigned int version; 
unsigned int timeout; 
unsigned int peripheralid; 
unsigned int servicesrequested; 
unsigned int callmsgmask; 
unsigned int agentstatemask; 
unsigned int configmsgmask; 
unsigned int reserved_1; 
unsigned int reserved_2; 
unsigned int reserved_3; 
}; 
char uccebuf[MAX_MSG_SIZE]; 
unsigned int invokeid = 1; 
char clientid[256]; 
char clientpassword[256]; 
int main(int argc, char *argv[]); 
void func1(); 
int main(int argc, char *argv[]){ 
func1(); 
exit(0); 
} 
void func1(){ 
int retval, error, bytes; 
struct open_req *request; 
struct floating_field *ff; 
char *p = uccebuf; 
unsigned int total_bytes, result; 
int templen,cnt; 
cnt = 0; 
//socklen_t len; 
error = 0; 
//request = (struct open_req *)malloc(sizeof(struct open_req)); 
//ff = (struct ffloating_field *)malloc(sizeof(struct floating_field)); 
strcpy(clientid,"admin"); 
strcpy(clientpassword,"12345"); 
request = (struct open_req *)uccebuf; 
total_bytes = 0; 
request->mhdr.messageType = 3; 
//memcpy(&uccebuf[cnt], &request->mhdr.messageType, sizeof(request->mhdr.messageType)); 
request->invokeid = invokeid++; 
request->version = 13; 
request->timeout = 0xFFFFFFFF; 
request->peripheralid = 0xFFFFFFFF; 
request->servicesrequested = 0x00000010; 
request->callmsgmask = 0x00000001 | 0x00000002 | 0x00000004 | 0x00000020 | 0x00000200 | 0x00000100 | 0x00040000 | 0x00000400 | 0x00010000; 
request->agentstatemask = 0x00000000; 
request->configmsgmask = 0x00000000; 
request->reserved_1 = 0x00000000; 
request->reserved_2 = 0x00000000; 
request->reserved_3 = 0x00000000; 
//memcpy(uccebuf,&request,sizeof(struct open_req)); 
printf("request->peripheralid: %u\n", request->peripheralid); 
printf("request->callmsgmask: %u\n", request->callmsgmask); 
p = p + sizeof(struct open_req); 
total_bytes += sizeof(struct open_req); 
ff=(struct floating_field *)p; 
ff->tag = 1; 
templen = strlen(clientid); 
ff->length = templen; 
//memset(uccebuf,&ff,sizeof(struct floating_field)); 
total_bytes +=sizeof(struct floating_field); 
p = p + sizeof(struct floating_field); 
strcpy(p, clientid); 
total_bytes += ff->length; 
p = p + ff->length; 
ff=(struct floating_field *)p; 
ff->tag = 2; 
templen = strlen(clientpassword); 
ff->length = templen; 
total_bytes +=sizeof(struct floating_field); 
p = p + sizeof(struct floating_field); 
strcpy(p, clientpassword); 
total_bytes += strlen(clientpassword); 
//memset(uccebuf,&ff,sizeof(struct floating_field)); 
request->mhdr.messageLength = (total_bytes - sizeof(struct msgheader)); 
printf("\nMessage to be send is: %s", uccebuf); 

}

當我嘗試打印的字符數組的內容,它不顯示任何東西代碼。你能告訴我我要去哪裏嗎?任何幫助都非常感謝

+2

請縮進代碼以使其可讀,並考慮更小版本是否會出現問題。 – unwind 2012-07-31 11:06:52

+0

你爲什麼要複製它們?你不能使用包裝聯盟嗎? – 2012-07-31 11:07:11

回答

1

printf()(以及對char字符串進行操作的所有其他函數,假設字符串在遇到零字節時終止。結構的第一個字節實際上是0,所以打印緩衝區將不會打印任何東西除此之外,除了零之外,其他大多數字符都是不可打印的控制字符,並且不會出現在屏幕上

如果要在屏幕上渲染緩衝區,以便可以檢查它應該循環遍歷字符並將每個字符打印爲十六進制字節。

小心使用此方法; m請確保您用於在您的陣列上操作的其他功能都不會被0清零。

1

如果你想轉儲出數組的內容,你需要告訴printf多少個字來寫,否則它會停在第一個零字節:

printf("\nMessage to be send is: %.*s", (int) sizeof(open_req), uccebuf); 

這將包含不可打印的字符,所以我建議使用十六進制轉儲:how to get hexdump of a structure data