Socket programming
-
I had created a structure with six members of type CString. I am trying to send this structure using the 'Send' command. A Pointer object is created for the structure. At the receiving end i had created the same structure. But when i was trying to display the data , I got only a blank message box. Can anyone solve this problem? source prg .......... Struct message { CString name; ... }; message *obj=new message(); Send(obj,sizeof(struct message)); dest prog ........ Struct message { CString name; ... }; message *obj=new message(); Receive(obj,sizeof(struct message)); MessageBox(obj->name); //blank message box was displayed ? vinsankar
-
I had created a structure with six members of type CString. I am trying to send this structure using the 'Send' command. A Pointer object is created for the structure. At the receiving end i had created the same structure. But when i was trying to display the data , I got only a blank message box. Can anyone solve this problem? source prg .......... Struct message { CString name; ... }; message *obj=new message(); Send(obj,sizeof(struct message)); dest prog ........ Struct message { CString name; ... }; message *obj=new message(); Receive(obj,sizeof(struct message)); MessageBox(obj->name); //blank message box was displayed ? vinsankar
The problem with your code is that sizeof(struct message) is going to return the same number no matter what the size of the string. That is because the CString class has a pointer to a buffer which contains the actual string. You have just stumbled upon a problem that occurs very often. There are many ways around this, but the simpliest one I can think of would be to change your struct to:
struct message { char name[256]; };
sizeof(struct message) would then return 256. This method is inefficient, as the whole buffer would be transferred, and it would be tougher to work with the char arrays instead of the CString's. Greba, My lack of content on my home page should be entertaining. -
The problem with your code is that sizeof(struct message) is going to return the same number no matter what the size of the string. That is because the CString class has a pointer to a buffer which contains the actual string. You have just stumbled upon a problem that occurs very often. There are many ways around this, but the simpliest one I can think of would be to change your struct to:
struct message { char name[256]; };
sizeof(struct message) would then return 256. This method is inefficient, as the whole buffer would be transferred, and it would be tougher to work with the char arrays instead of the CString's. Greba, My lack of content on my home page should be entertaining.why can't we use CString instead of character arrays ? I am in need of CString values at the receving end.Because I have to perform some trimming operations at the receiving side. What is the real problem while displaying the values stored in structure? Is there any other way to send a structure through the 'Send' command? vinsankar