How to send a message in TCP/IP?
-
Hello! I need to send a structure via TCP/IP withuot using serialization! since the other side is unknown i cant use this feature. thanks
say what?!?! can you try and explain your problem a little bit. What do you mean by the other side is unknown? And, which part of sending over TCP/IP don't you understand? Please, be a little more specific about what you are trying to accomplish. It will help us give you the right answer. ---- www.digitalGetto.com
-
say what?!?! can you try and explain your problem a little bit. What do you mean by the other side is unknown? And, which part of sending over TCP/IP don't you understand? Please, be a little more specific about what you are trying to accomplish. It will help us give you the right answer. ---- www.digitalGetto.com
U right What I need to do is this: Take a structure and send over a TCP/IP to a client/server on the other side. The problem: I need to transform the structure to an array of bytes which I don’t know how. I can't do this with the serialize function since the other side may or may not have the unserialize function. I tried to do this with casting but it didn’t work. I tried to do this with union' but u can't make an array inside a structure. I tried to do it whit unmanaged code, but the Socket class will accept only managed Byte array. There, I told u all I know. Can u help me?
-
U right What I need to do is this: Take a structure and send over a TCP/IP to a client/server on the other side. The problem: I need to transform the structure to an array of bytes which I don’t know how. I can't do this with the serialize function since the other side may or may not have the unserialize function. I tried to do this with casting but it didn’t work. I tried to do this with union' but u can't make an array inside a structure. I tried to do it whit unmanaged code, but the Socket class will accept only managed Byte array. There, I told u all I know. Can u help me?
What format is the receiving side expecting the data to be in? Find that out, and send the data in that format.
--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ VB > soccer
-
U right What I need to do is this: Take a structure and send over a TCP/IP to a client/server on the other side. The problem: I need to transform the structure to an array of bytes which I don’t know how. I can't do this with the serialize function since the other side may or may not have the unserialize function. I tried to do this with casting but it didn’t work. I tried to do this with union' but u can't make an array inside a structure. I tried to do it whit unmanaged code, but the Socket class will accept only managed Byte array. There, I told u all I know. Can u help me?
The structure that you are trying to send.. is that something you made? Is the receiving end a program that you're writing? If not, what format does it expect the data to be in? Serialization/Deserialization is a process. That's all. There is nothing magic involved. You can always write your own functions to serialize and deserialize data. Here's an example of a simple structure that is serialized into a byte array:
struct myStruct
{
int iValue; // integer value
string strValue; // zero terminated stringpublic myStruct(byte\[\] sourceArray) { int k; if(sourceArray.Length > 2) { // reconstruct int value from lo byte and hi byte this.iValue = sourceArray\[0\] + sourceArray\[1\] \* 256; // reconstruct string value this.strValue = ""; k = 2; while(sourceArray\[k\]!=0 && k\> 8) & 0xFF; // hi byte // save the string value k = 0; while(k < this.strValue.Length) tmpArray\[k + 2\] = this.strValue.ToCharArray()\[k++\]; return tmpArray; }
}
// ...
// test codebyte[] t;
myStruct A;
myStruct B;A = new myStruct();
A.iValue = 567;
A.strValue = "test string";t = A.Serialize(); // t = serialized array of the strucutre
B = new myStruct(t); // B = a copy of A, constructed by deserializing tobviously, once you serialize your structure into an array you can send it over TCP/IP using a WinSock ---- www.digitalGetto.com
-
The structure that you are trying to send.. is that something you made? Is the receiving end a program that you're writing? If not, what format does it expect the data to be in? Serialization/Deserialization is a process. That's all. There is nothing magic involved. You can always write your own functions to serialize and deserialize data. Here's an example of a simple structure that is serialized into a byte array:
struct myStruct
{
int iValue; // integer value
string strValue; // zero terminated stringpublic myStruct(byte\[\] sourceArray) { int k; if(sourceArray.Length > 2) { // reconstruct int value from lo byte and hi byte this.iValue = sourceArray\[0\] + sourceArray\[1\] \* 256; // reconstruct string value this.strValue = ""; k = 2; while(sourceArray\[k\]!=0 && k\> 8) & 0xFF; // hi byte // save the string value k = 0; while(k < this.strValue.Length) tmpArray\[k + 2\] = this.strValue.ToCharArray()\[k++\]; return tmpArray; }
}
// ...
// test codebyte[] t;
myStruct A;
myStruct B;A = new myStruct();
A.iValue = 567;
A.strValue = "test string";t = A.Serialize(); // t = serialized array of the strucutre
B = new myStruct(t); // B = a copy of A, constructed by deserializing tobviously, once you serialize your structure into an array you can send it over TCP/IP using a WinSock ---- www.digitalGetto.com
First of all, thank u all!!! Let me tell u the all story. What I need to do is to send a message over the net. So, I have this structure (which is the message to be sent), and then I have to send it (somehow…) Then, I receive a message which I have to decode and then, according to the header, I parse it back to a structure which I will use in the program. I know that for some of u readers it may seem like a dumb question, but since it is my first time that I have to deal with communication, please accept my ignorance. Much appreciated
-
First of all, thank u all!!! Let me tell u the all story. What I need to do is to send a message over the net. So, I have this structure (which is the message to be sent), and then I have to send it (somehow…) Then, I receive a message which I have to decode and then, according to the header, I parse it back to a structure which I will use in the program. I know that for some of u readers it may seem like a dumb question, but since it is my first time that I have to deal with communication, please accept my ignorance. Much appreciated
-
What is your question exactly, how to send/receive data via TCP/IP, or how to encode/decode your data? If it is the former, start from here. Best, Jun