PDA

Click to See Complete Forum and Search --> : How to send & receive user defined class objects through winsock control


Jan 1st, 2001, 09:26 AM
I am developing a small chat application. Sending and receiving string, but how can I send and received a formated data using a user defined class objects.

I will be very much thankful if anyone of u can help me out.

itay222
Jan 1st, 2001, 10:31 AM
you need to implement it yourself.
for example, if you want to pass an integer,
you can convert it to string, send it,
and then reconvert it back to integer (this is for demonstration, yeah?).
however,
since you are talking about classes,
it might be a nice idea to add to each class you define
two methods: decode/encode.

example:
(forgive my incorrect syntax)
class person
name as string
age as integer
method: decode (person) returns string
method: encode (string) returns person
end class

function encode returns string
return name+"ZZ"+cstr(age)
end function

function decode (str) returns person
split str on "ZZ"
return new person (name=arrStr[0], age=arrStr[1])
end function

itay.

Ceri
May 9th, 2001, 11:11 AM
Winsock can send any type of data, because in the end it's only binary data that gets sent. If you mean you want to send a User Defined Class - as in a UDT :-

Public Type myType
MyString as String * 20
MyNum as Long
MyBytes(255) as Byte
End Type

then you can use the CopyMemory API call to put the UDT into a byte array and then send the data, and then on the other side you can reverse the proccess by using the CopyMemory API call again. But you cannot use Variable Length Strings or un-dimentioned array's because when you try to get the size of the UDT it will be incorrect.

ccoder
May 9th, 2001, 12:20 PM
See the following thread.
http://forums.vb-world.net/showthread.php?s=&threadid=71122

There is a link to an example client/server app (CS.ZIP) that passes UDTs. The source has lots of comments explaining what is happening and some pitfalls to be aware of.