How can I an instance of any class by TPC. I've found how to send a string using System.IO.StreamWriter, but I have to send an object (instance of a class I've written). How to do that
Hello Wojteq. What you need to look into is Serialization. A tutorial can be found here Then You would need to take the output from the Binary Stream and send that through your sockets. Ideally, before sending it you would send a packet that contained header information notifying your application that you are about to send a serialized object and how much data it will be this way your application knows when the data has been fully transfered so that it can deserialize it.
You can provide any Stream you want to the IRemotingFormatter.Serialize method. Any formatter like the BinaryFormatter for example implements this method.
Create a filestream if you want to serialize it to and file.
I'd like to ask you one more question. You said that I shoud send a header information. Would You like to tell me how to do that I've got no experiance with streams and communication in the Internet.
The header would be just some binary data that identifies what object you are sending and the size of the payload. This way the socket on the receiving end knows what to expect, and how much of it to expect. e.g. you could have a number that specifies the type of object you will serializing, useful if you are going to be sending many differnet object types back and forth. And the size of the object. The tricky thing about this is that you need to convert it to a 4 byte value so that when it is sent it will always be 4 bytes e..g 0x00, 0x00, 0x2, 0xC0 for an object that is 704 bytes. I usuaully find that I have to then terminate it with an \r\n Then you would send the data. On the client side it would read the header and then set a queue so that it knows that its going to be receiving x type of object and how many bytes. When more data comes in it stores this until its received the amount of data and then act upon it accordingly. As your application gets more complex your header will probably also increase. Example you may have a header that tells the application what to do with that object once it's serialized, You might also put in some sort of CRC sum if data corruption ever becomes a problem, etc
How to send an object by TCP?
SteveJB
Then You would need to take the output from the Binary Stream and send that through your sockets. Ideally, before sending it you would send a packet that contained header information notifying your application that you are about to send a serialized object and how much data it will be this way your application knows when the data has been fully transfered so that it can deserialize it.
cs96ai
Create a filestream if you want to serialize it to and file.
Scott47
I'd like to ask you one more question. You said that I shoud send a header information. Would You like to tell me how to do that I've got no experiance with streams and communication in the Internet.
Wernerj
e.g. you could have a number that specifies the type of object you will serializing, useful if you are going to be sending many differnet object types back and forth. And the size of the object. The tricky thing about this is that you need to convert it to a 4 byte value so that when it is sent it will always be 4 bytes e..g 0x00, 0x00, 0x2, 0xC0 for an object that is 704 bytes. I usuaully find that I have to then terminate it with an \r\n Then you would send the data.
On the client side it would read the header and then set a queue so that it knows that its going to be receiving x type of object and how many bytes. When more data comes in it stores this until its received the amount of data and then act upon it accordingly. As your application gets more complex your header will probably also increase. Example you may have a header that tells the application what to do with that object once it's serialized, You might also put in some sort of CRC sum if data corruption ever becomes a problem, etc
Jay Hickerson
In the example you've shown me, there's how to serialize an object with saving it on the HD. Is it possible do do that without doing it
Uzivatel