socket programming

  • Thread starter Thread starter Paul Fi
  • Start date Start date
P

Paul Fi

i have little knowledge of socket programming, but what i know is that
using sockets we exchange text between endpoints of the socket but
first we convert the text to array of bytes and we send it to the other
side of the comm channel and in the other side we get the original
string by converting from array of bytes to text

my question is instead of having text exchanged is it possible to have
objects or complex types converted to array of bytes and vice versa with
sockets?
 
Paul Fi said:
i have little knowledge of socket programming, but what i know is that
using sockets we exchange text between endpoints of the socket but
first we convert the text to array of bytes and we send it to the other
side of the comm channel and in the other side we get the original
string by converting from array of bytes to text

my question is instead of having text exchanged is it possible to have
objects or complex types converted to array of bytes and vice versa with
sockets?

Absolutely. Sockets aren't fundamentally about exchanging text -
they're about sending streams of bytes. What you do with those bytes is
up to you.
 
The methods for the socket classes actually send and receive arrays of bytes. What you put into the byte arrays is up to you.

Depending on what state of an object you want to exchange you can use serialization to obtain a state of a class (either an XmlSerializer or BinarySerializer) and then convert a stream into an array of bytes to send it via sockets.
 
can u show me how this can be done by a simple code how to transform a
complex data type into an array of bytes?
 
i have little knowledge of socket programming, but what i know is that
using sockets we exchange text between endpoints of the socket but
first we convert the text to array of bytes and we send it to the other
side of the comm channel and in the other side we get the original
string by converting from array of bytes to text

my question is instead of having text exchanged is it possible to have
objects or complex types converted to array of bytes and vice versa with
sockets?
Yes, you can use an array of bytes and this is what Indy
(http://www.indyproject.org). That passes an array of bytes (TBytes) as a
parameter to the Socket.Send and Socket.SendTo methods (see the DotNET
SDK). In fact, recently, I actually was working on some code in DotNET for
the SNTP protocol (Simple Network Time Protocol) which sits on UDP. That
uses a fixed structure including binary bytes and integers.

I must say something about unsigned word and double-word values. Many
protocols where those are sent in binary should be sent in Network byte
order (not the byte order on your machine or in DotNET). For reading a
binary integer from an endpoint, you would use
IPAddress.HostToNetworkOrder. For sending a binary integer to an endpoint,
you would use IPAddress.HostToNetworkOrder. You often have to do this
with simple UDP query/response protocols or with raw sockets.

HTH.

--
J. Peter Mugaas - Indy Pit Crew
Internet Direct (Indy) Website - http://www.nevrona.com/Indy
Personal Home Page - http://www.wvnet.edu/~oma00215
If I want to do business with you, I will contact you. Otherwise, do not
contact me.
 
Paul Fi said:
can u show me how this can be done by a simple code how to transform a
complex data type into an array of bytes?

It varies with each kind of object. We cannot say without knowing your object
type. But .net has serialization which is a generic way to make most classes
"poof" into a text format. You might investigate that.


--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"


ELKNews - Get your free copy at http://www.atozedsoftware.com
 
Back
Top