How do I send types instead of strings with WinSock (VB)?

Started by
11 comments, last by Roman Gorkiewicz 24 years ago
In Visual Basic you write... Dim sendStr as String sendStr = "Text" Winsock.SendData sendStr ...to send a text string. How do I send a complete type?
Advertisement
How do you write complete types to a file? And read them? Sockets are essentially treated as just another file and you send a stream of bytes down them. Do you mean a complete type such as a Class? (I''m not 100% familiar with VB.) Unless Visual Basic has a function to write the object''s binary representation out, and read such an object in at the other end (which I doubt), you''d need to do something like writing out all the important member variables for that object. This would be best done with 2 functions, one Write() which exports the data to a string, and one Read() which takes such a string and initialises an object with it.

Hope that''s some help.
The Winsock control doesn''t allow you to send user declared types. The best you can do is to send the individual data members one at a time. I would suggest something like this: (syntax may be wrong, it''s been a while since I''ve used VB)

Type SOMETYPE  t1 As Byte  t2 As Currency  t3 As LongEnd TypeSub SendSOMETYPE(d As SOMETYPE)  Winsock.SendData d.t1  Winsock.SendData d.t2  Winsock.SendData d.t3End Sub 
Speaking from own experiences, Winsock in VB is a bitch to program. Not only is it hard to send data, there is also (as far as I know) no function for seperating the incoming data if it comes in shortly after another data string. That means that if you send two chatstrings, Winsock will return these two in one string.

I solved that by using one character for the beginning of a string and one for the end of a string. Like this:

$Hi!¤$How are you?¤

It makes the interpetation of it slow and time consuming to code, but that's the only way I know. If you really want to use network communication, you should definetly look at DirectPlay, it's really divine compared to Winsock. You can send types, strings, longs and bytes, just about anything you want.

============================
Daniel Netz, Sentinel Design
"I'm not stupid, I'm from Sweden" - Unknown

Edited by - Spiff on 3/28/00 2:21:02 AM
============================Daniel Netz, Sentinel Design"I'm not stupid, I'm from Sweden" - Unknown
Just send the size of your data as a LONG and then the data.
That''s what DirectPlay does when using TCP.
quote:Original post by Spiff

Speaking from own experiences, Winsock in VB is a bitch to program. Not only is it hard to send data, there is also (as far as I know) no function for seperating the incoming data if it comes in shortly after another data string. That means that if you send two chatstrings, Winsock will return these two in one string.


Well, that''s how TCP/IP works in general. It makes no attempt to split up what you send or group it, nor should it, as you may send something in several parts anyway. It just simply sends whatever you tell it, verbatim. If you want to delineate sections, it''s up to you to use some sort of delimiter. For instance, a telnet app in C might call read() (or recv() in Windows, I think) repeatedly, and each time add whatever it gets to an ''incoming'' buffer. The only way it knows it''s read a full line is when it sees a carriage return/line feed, at which point it can clear the buffer up to that point and continue.

Of course, more high level APIs such as DirectPlay may break things up into ''messages'' or something like that, but Winsock is just a nasty wrapper for standard sockets code and does little more than let you treat a socket as another binary file. If you want to use it directly, it''s best to work out your own protocol, or borrow someone else''s, and wrap some buffering functions in a class/object.
That, I didn''t know. Thank you.

One note though: "Well, that''s how TCP/IP works in general" - don''t you mean Winsock or whatever functions that handles the incoming/outgoing TCP/IP traffic? TCP/IP is only the "delivery guy".

============================
Daniel Netz, Sentinel Design
"I'm not stupid, I'm from Sweden" - Unknown
============================Daniel Netz, Sentinel Design"I'm not stupid, I'm from Sweden" - Unknown
First, thanks for the replies!

If I use the way of SiCrane to send data, I do not know to what the data belongs.
In my program all the important data is put into a string. Commas are
separating the data pieces. The first value identifies the string.
But accumulating all the data into a string and extracting it later
consumes a lot of time. Is there an easier way to do this, without the risk
of not knowing what data has arrived? In general functions like read and write
need the same treatment like I do with strings. I do not know how DirectPlay
handles this, but DP is far to overheaded for my simple program.
Perhaps someone of you knows where to find a simple example program.
Well, let''s pretend that you have three types: Type1, Type2, Type3.

You could do the following:

Sub SendType1(data As type1)  Dim id as Byte  id = 1  Winsock.SendData id  Winsock.SendData data.t1  ...End SubSub SendType2(data As Type2)  Dim id as Byte  id = 2  Winsock.SendData id  Winsock.SendData data.t1  ...End SubSub SendType3(data As Type3)  Dim id as Byte  id = 3  Winsock.SendData id  Winsock.SendData data.t1  ...End SubSub ReadData()  Dim id as Byte  Winsock.GetData id, vbByte  If (id = 1) Then    Call GetType1  Else    If (id = 2) Then      Call GetType2    Else      Call GetType3    End If  End IfEnd Sub 



Spiff:
TCP is a stream-oriented transport protocol. It guarantees in-order transmission of data sent. Otherwise it makes no promises. As long as data is received it''s stuffed in the same buffer as all the other data already received on that port, in the same order it was sent. Yes, you can write Application, Presentation or Session layer protocols that will allow discrete messages, but there''s no support for discrete messages in TCP.
quote:Original post by Spiff

One note though: "Well, that''s how TCP/IP works in general" - don''t you mean Winsock or whatever functions that handles the incoming/outgoing TCP/IP traffic? TCP/IP is only the "delivery guy".


I guess that technically, you are right. As was I. I was referring to the fact that TCP/IP is just the delivery guy, and makes no effort to do anything with it, therefore by implication, Winsock as a very thin wrapper around it, also does no such higher-level ordering. You really are just writing a stream of bytes to a black hole, and the receiving end has the responsibility to make sense of it all You have to agree some higher-level protocol between server and client, such as Telnet, HTTP, FTP, etc. TCP/IP helps in that it makes sure data is sent at least once, only once, and is ordered. (It may not always arrive in order, but it''s the protocol''s job to re-order it before it reaches your app. Ever noticed your internet connection is receiving data at a high rate, yet your download is not proceeding? Chances are, a packet is missing, and the protocol is buffering subsequent packets until it gets the missing one and can present them in order.)
Anyone interested in learning more should see www.ecst.csuchico.edu/~beej/guide/net/ for a guide to sockets (although I think it''s mainly Unix based.

This topic is closed to new replies.

Advertisement