Need any of you fine gentlemen to help me figure out why this is breaking.
I'm working on a networking framework.
Link To DL Framework:
http://www.planetchili.net/forum/download/file.php?id=1393
Note: Keep in mind that right after the client connects, the function for sending a data structure containing strings is called in the main.cpp of the ServerFramework project. In order to debug this, you will have to change the IP the client is connecting to to be the same as your local ip (you can find this by running cmd and typing /ipconfig
You need to go into your main.cpp for your client to change the connecting ip. Look at the line 17
I have functions who use nearly identical logic
int ReceiveAndFillStructureWithStrings(int ID, int NumberofStrings, char * StructureToReceive, string * FirstStringReference)
void SendDataStructureWithStrings(int ID, int NumberofStrings, char * StructureToSend, string * FirstStringReference)
This is the prototype for the functions for the server. The client's functions are exactly the same, except they don't have the ID parameter since they will only send information to the server.
The Receive function works fine. The SendData function works in the sense that it does send the data, however it does something in my memory that breaks the code afterwards.
These structures are meant to be used with strings that are set up with all of the other variables first and then strings last.
Ex.
1 2 3 4 5 6
|
struct ExampleStruct
{
int type, x, y;
string MyString1;
string MyString2;
};
|
In this example structure, all of the data besides strings comes first, and then strings are last.
Using this example, i'll declare an object to show how it is meant to be passed into the function and the current logic i'm using.
1 2 3 4
|
ExampleStruct Test;
Test.MyString1="hello";
Test.MyString2="hello again";
SendDataStructureWithStrings(1, 2, (char*)&Test, &Test.MyString1);
|
So this is how I set up my object and call my function to send the data structure containing strings. I call 1 for the first parameter, because in this example i'm saying that the Client's Connection ID is 1. I'm calling 2 for the second parameter since the structure contains two strings. I'm calling (char*)&Test for the third parameter, because I need to pass a char ptr to a reference to the struct. My last parameter is a reference to the first string inside of the data structure's layout.
SO
Here is the logic behind how this function works
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
void SendDataStructureWithStrings(int ID, int NumberofStrings, char * StructureToSend, string * FirstStringReference)
{
int DistanceToStrings = (int)FirstStringReference - (int)StructureToSend;
SendData(ID, StructureToSend, DistanceToStrings); //Sends all data before the strings
for (int i=0;i<NumberofStrings;i++)
{
string FirstString;
memcpy(&FirstString,FirstStringReference,sizeof(string));
int size = FirstString.size()+1;
char * stringbuffer = new char[size];
strcpy(stringbuffer,FirstString.c_str());
stringbuffer[size-1]='\0';
char IntBuffer[4];
memcpy(&IntBuffer,&size,4);
SendData(ID, IntBuffer, 4); // send size of string
SendData(ID,stringbuffer,size);
if (i<(NumberofStrings-1)) FirstStringReference++;
delete[] stringbuffer;
}
};
|
Before the for loop, I calculate the DistanceToStrings( Distance in bytes from the address to the structure to the first string found in the structure) and then I send all the data before you reach the string in the structure. Next, I have the for loop to send the string(s). I declare a new string object, copy my stringreference into it, find the size of the string for the char buffer, allocate my char buffer, copy my C++ string to a C-String, send a packet (int) containing the size of the string to receive, and then I send the char buffer for the string over.
Here is what is confusing the shit out of me.
This all works as I expected in the sense that it is properly sending the data and I can call the function more than once in the same scope and it wont break until I leave that scope it seems.
Attached is the project.
Look in the ServerFramework main.cpp to see what i'm talking about
In the actual project I have, I use the ChatMessageStruct instead of the example struct I typed earlier in the post.
Summary: The functionality is working fine for this in the sense that I can send and receive a data structure containing strings. It is failing in the sense that it seems to corrupt my memory after exiting the scope from which I had called the function.
Thanks so much for looking into this if anyone has any idea of why this is functioning the way it is it would be a huge relief.