•      Sign In
• Create Account

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

12 replies to this topic

#1Roman Gorkiewicz  Members

122
Like
Likes
Like

Posted 26 March 2000 - 10:06 PM

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?

#2Kylotan  Moderators

6510
Like
Likes
Like

Posted 26 March 2000 - 10:10 PM

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.

#3SiCrane  Moderators

11526
Like
Likes
Like

Posted 27 March 2000 - 07:54 PM

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

#4Spiff  Members

122
Like
Likes
Like

Posted 27 March 2000 - 08:18 PM

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

#5Jeff K  Members

122
Like
Likes
Like

Posted 28 March 2000 - 02:04 AM

Just send the size of your data as a LONG and then the data.
That''s what DirectPlay does when using TCP.

#6Kylotan  Moderators

6510
Like
Likes
Like

Posted 29 March 2000 - 01:56 AM

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.

#7Spiff  Members

122
Like
Likes
Like

Posted 29 March 2000 - 02:10 AM

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

#8Roman Gorkiewicz  Members

122
Like
Likes
Like

Posted 29 March 2000 - 04:40 AM

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.

#9SiCrane  Moderators

11526
Like
Likes
Like

Posted 29 March 2000 - 06:20 AM

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.

#10Kylotan  Moderators

6510
Like
Likes
Like

Posted 29 March 2000 - 11:41 PM

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.

#11Roman Gorkiewicz  Members

122
Like
Likes
Like

Posted 29 March 2000 - 11:42 PM

Thanks SiCrane.

This helped.

Though I use UDP your example should work anyway. Even if a message gets lost.

#12DrJohnB  Members

122
Like
Likes
Like

Posted 04 April 2000 - 03:12 AM

In a real-time game, you don''t want the guaranteed message delivery of TCP. I mean, it''s not good to loose a bunch of packets, but if a packet does wander astray in transit, you certainly don’t want to spend time trying to recover that data because by the time you finally receive it you could end up with a player’s location from a few moves/seconds ago. Then what are you going to do with that data?

It’s best just to move on and hopefully get the next packet and then just speculate the path say from move 1 to move 3. This is only for real-time of course not including chat programs. In turn-based games it’s more crucial to receive every packet.

#13Spiff  Members

122
Like
Likes
Like

Posted 04 April 2000 - 03:19 AM

Thank god for DirectPlay.

Some "fun" TCP/IP reading: http://www.microsoft.com/technet/deploy/tcpintro1.asp

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

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.