Winsock Troubles

Started by
6 comments, last by edgecrusher 22 years, 10 months ago
I have a TYPE variable that I want to send through the winsock. VB returns an error saying Type Mismatch when I try to send. Here is the example: Type CHUNK A as Integer B as Integer C as String End Type Dim X as Chunk X.A = 0 X.B = 0 X.C = "Windows" ''if connected then Winsock.Senddata X Is there a way I can send X as a whole and not Winsock Senddata X.A,X.B,X.C? If u can, please show a small example. Thanks for your help.
Advertisement
i don''t know vb well
but you actually have to convert your structure into a string
the numbers too everything
just like you save it on a file..
in c it would be something like
char bla[sizeof(MYSTRUCT)];
memcpy(bla, structure, sizeof(MYSTRUCT));
send(bla...)

voila


Arkon
[QSoft Systems]
edgecrusher:

What you appear to be attempting is what the Java Stardard refers (referred?) to as Object Serialization. And it won''t work over a standard Winsock socket. If you want to use Winsock to actually transfer the object (actually, make a copy of it on another system) then you would have to 1) Provide some way for the class or structure to be recreated on the remote end (or pass the class name and assume you have the class at the other end). 2) Walk through and pass each property and value name over the socket (this is easy, just name/value pairs). After that, 3) your client has to take all that data and recreate your object and initialize it to your values.

Unless you''re actually in a language that makes this mechanism simpler, pass a string of name/value pairs to your client and write a routine that handles the string as arguments to the constructor of your class.

------------------------
-WarMage
...good programming is like good sex, but good sex is not at ALL like good programming...
To be honest it is probably better to create a class that can serialize itself into a string:

Private m_Stuff As Integer
Private m_Whatever As String

Public Function Serialize() As Function
Serialize = CStr(m_Stuff) & m_Whatever
End Function

Then you:

Winsock1.SendData MyClassInstance.Serialize()

Hope this helps,



Dire Wolf
www.digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
To be honest that''s what i actually said

geee

Arkon
[QSoft Systems]
Well actually, you'd be wrong Arkon. You don't HAVE to convert anything to a string. You can simply create a byte array and use the NT RtlMoveMemory API to put the information into the byte array.
So you don't have to put the data into a String (BSTR).

BTW Your C method of doing things is bad because you are forgetting about padding/byte alignment in a struct.

Lastly, my 'actually' part was refering to edgecrusher's method.

Geee

BTW Winsock programming in C++ is so much nicer than in VB.



Dire Wolf
www.digitalfiends.com

Edited by - Dire.Wolf on June 16, 2001 1:23:41 PM
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com
i didn''t convert anything into a string
i just send the struct as a string

Arkon
[QSoft Systems]
quote:
but you actually have to convert your structure into a string


That is what you said.

Anyhow, I don''t know why we are carrying on about this Using Winsock in C/C++ you can just send what ever you want across the network. It is as simple as casting your structure to a char*. Unfortunately, structures/classes with internal pointer members need to be deferenced before sending (else you''ll just send a 32-bit address) which is why you usually create a Serialize function/method for the class/struct.

The same applies to VB. You can''t send a user-defined data-type (TYPE) via a Variant to the Winsock control. This because there isn''t a good way in VB to automatically serialize your user-defined type, so the Winsock control doesn''t try. It doesn''t support that type of Variant. VB6 can marshall public user-defined types across process or machine boundaries but that is a different issue.

So EdgeCrusher, you will need to Serialize your Type into a byte array or String (BSTR).

Best regards,







Dire Wolf
www.digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com

This topic is closed to new replies.

Advertisement