Search Tools Links Login

Organising Communication with WinSocket

Posted: 2002-06-01
By: ArchiveBot
Viewed: 55

Filed Under:

VB6 Code Cache

Title Uploaded Size
Organising182384112001.zip 9/3/2020 3:45:00 PM 12,643

This artical will give the user a insight as to how data transactions should occur. it will cover how to send different types of data and then getting the recipent program to understand it and do something with that data and its type, such as setting nicknames and sending messages (in a chat program) also included is a simple chat program that uses the code in the artical and shows you the code in action. the code is very well commented and it aimed at begenner level to intermediate level coders. also included is a nice way to counter act teh effect of "Data Merging" that can occur over slow connections using TCP/IP with out using the slow "Send Complete" event in winsock.

Original Author: David Meirion Hughes

Code

Organising
Communication with WinSock


[Introduction]



Hi.
This is my first tutorial so it may be a little bad, but I hope I can help you
out a little. This tutorial is for basic to intermediate coders and will explain
a little on communication between 2 programs such as a chat program. Now you
may have seen a lot of chat programs on here some are good some are just basic
and ONLY do chat, in the latter the two programmes will be just sending text
between one another. This tutorial will teach you how to organise your data
packets and allow your chat program (or any other type of communication program)
to do a lot more that just send text.



Notice: I'm sorry that teh code is not indented properly. dam word doesnt copy
and paste properly and I can add spaces using dream weaver so I hope you can
make do.


Updated
Data is in Blue: This Information is for new coders that dont quite understand
what I'm doing and I think if I was in there shoes I would agree with them.
The sections in blue will explain what exactly we are doing and how to do it.


I
have included a fully documented example project that will help you see what is
going on in the code and how it all works.

What
we are going to do. We are going to start off by learning how data packets are
formed or better put, how you the coder should organise the information you
tell your programs to tell each other. Then we will cover how to make your datatypes.
These datatypes hold the key to the data and allow the program to understand
exaclty what is has been given. thirdly we will cover how to make sending packets
of data faster and easier in the long run. we will then learn how to unscramble
merged data that can be a problems for both new and experenced Programmers.
We will then learn how out programs can quickly and easilly decipher what it
has been given.




NOTE:
the code in this artical is related to Visual Basic Version 5 and 6. it has
not been tested for any other platform






Okay so lets begin. First off let me explain how I organise my data packets



[Data Packet Structure]


When
my chat program sends data the data is in 2 parts, the Data Type and the Data.
For example:


04HELLO!


Part
1 (Data Type):


The
first two numbers tells the recipient program what the data is. Say if the number
is 04 then it could mean, "Here is a message for you" or if its 07 the it could
mean "My nick name is: "


Part
2 (Data)


This
is the data that goes with the packet. What the recipient program does with
it is dependant on the Data Type


You
may have noticed the "" symbol. Don't worry its actually part
of the data packet and not another instance of bad English / Typing. All will
be relieved later on.


[Setting
the Types]



Okay
so now we know how the packets are formed. Now we need to code the data types
so that its easier later on to do stuff. Here is how you would set your data
types in a module file (or whatever).


A
Module file is a file that is used to store code. It allows coders to organise
files alot easier and put certain code into certain, relivent files. To create
a module file goto to the menu bar, choose "Project" then choose "Add
module".



===================


Public
Enum DataTypes

MESSAGE = 0

NICKNAME = 1

End enum




===================



An Enum is like the type Boolean (True or false) with
boolean if you choose TRUE then the number is set to 1 becuase that is how its
defined. it you choose FALSE then the number is set to 0. With the Enum statement
we can make our very own boolean type, type.



Okay so you've entered in the code above, now we have a type called DataTypes,
to test it hit the enter button and type



DIM TESTVARABLE as DataTypes


Now
type in

TESTVARABLE = now a menu should
come down and you can select which one you want and it will right it in for
you.




I'm not going to put a lot of types on here. My chat program has about 22 so
far. But for this tutorial these two will do fine. Now I'm assuming you already
know how to connect two computers together using winsock so I'm not going to
go into that. If you need help then there are plenty of good examples and tutorials
on the basics of winsock on this web site.


[Making
a Fast Send Sub Routine]


So
we have our data types ready. Now we can code a really cool Sub that can really
speed up sending data (coding wise)


===================


Public
Sub Send_Data(dType As DataTypes, Optional dData As String)

Dim oData As String

Dim oType As String

Dim oAll As String



oType = dType

If Len(oType) < 2 Then

oType = "0" & dType

Else oType = dType

End If



oData = dData

oAll = "" & oType & oData



If WINSOCKCONTROL.State <> sckConnected

Then MsgBox "ERROR: Not Connected", vbCritical, "No Connection"

Exit Sub

End If



WINSOCKCONTROL.SendData (oAll)

End Sub



===================



Okay basically this is used so that if you want to send a message you can do
it with one line of code and do it really fast. It also brings up a really cool
menu for choosing the data type. It also makes sure the data type uses 2 characters
so if the number for the type is less than 10 then it adds the character "0"
to the beginning and then the single digit afterward. Its basically used so
that its always 2 characters and can be easily ripped out of the data packet
later on.



Okay so you've sent the data. Now you need something to decipher it on the other
side. But first I think its time I explained what the "" is for. Now when
I started playing around with my chat program on the Internet I found that the
data was getting merged together. Basically sometimes two data packets merged
to look something like this 02Hi04David. When this happened my program went
to find the data type "02" then sent the message "hi04david" which was annoying
because the 04david was supposed to be a nickname and not a message. So any
ways back to the point.



[Splitting merged data packets]



I came up with the idea of adding a symbol to the beginning of all the packets
then splitting the packets up after every "" symbol. It took a while to
figure out but I managed it… so here the code to do it…. By the way there is
a reference to the Incoming_Data Sub, which we will cover afterwards.



===================



Public Sub Split_Packet(iData As String)

Dim sPackS As Integer

Dim sPackE As Integer

Dim i As Integer

Dim j As Integer

Dim sLast As Integer

Dim sType As DataTypes

Dim sData As String

Dim sAllData As String



For i = 1 To Len(iData)



If Mid(iData, i, 1) = "" Then

sPackS = i + 1



For j = sPackS To Len(iData)



If (j = Len(iData)) And Mid(iData, j, 1) <> "" Then



sPackE = Len(iData)

sAllData = Mid(iData, sPackS, sPackE) '- (sPackS + 1)))



If Len(sAllData) < 3 Then

sType = sAllData

Else



sType = Mid(sAllData, 1, 2)

sData = Mid(sAllData, 3, (Len(sAllData) - 2))



End If

Call incoming_data(sType, sData)

Exit Sub



ElseIf Mid(iData, j, 1) = "" Then



sPackE = (j - 2)

sAllData = Mid(iData, sPackS, (sPackE - sPackS) + 2)



If Len(sAllData) < 3 Then

sType = sAllData

Else



sType = Mid(sAllData, 1, 2)

sData = Mid(sAllData, 3, (Len(sAllData) - 2))

End If



Call incoming_data(sType, sData)



Exit For



End If



Next j



End If



Next i



End Sub



===================


The
symbol "" is used by holding shift and then the button next to
the number 1. It can be any symbol you wish, but I chose this symbol becuase
I felt that it would not be used by the people testing the program and so I
would be safe using it.



Okay all this does it constantly loops around until it's found all the merged
packets (if any) then send it to another sub to be processed.



Now if you're actually an expert at Winsock and wonder why I just didn't just
use the "send complete" event in Winsock. Well its because it kind of freezes
up when u have 30 connections and it gets really slow doing it that way.



[Processing the Incoming Data]



Okay we've now sorted the data now we need to do something with it. This is
where incoming_data comes in. Basically all we do here is do a select case statement
on the incoming data type. Then do something with the data.



===================



Public Sub incoming_data(iType As DataTypes, iData As
String)



Select Case iType

Case DataTypes.MESSAGE

'send the data or message to the textbox

txt_dialog.Text = txt_dialog.Text & iData & vbCrLf

Case DataTypes.NICKNAME

'set the remote users nickname as the data

lbl_usernick.caption = idata

end select




===================



So you now need to know the steps what you should do on
a Data_Arival Event in winsock it is this....

>ON - DATA_ARRIVAL_WINSOCK>

>SEND THE DATA TO >

>SPLIT_PACKET >

>SEND THE PACKETS TO>

>INCOMING_DATA

>

>DO SOMETHING WITH THE DATA AND ITS TYPE.



Now I mean this is quite basic what I've shown you here. But you can add new
data types and do new things on that type of data. There is no limit to how
many you want. Although don't go over 100 type if your using my code… come to
think of it. If you manage to get over 100 individual types email me or post
a comment because I don't believe its possible…. hehe…. I have multi channels
and multi users and I've only got 22 types! Anyway I hope this tutorial has
helped you a little. or if ya want to tell me how to do sothing proberly then
please tell me becuase I've only recently started on VB



Please leave a comment if you need any help or u would like to thank me or if
you want to tell me that I'm wrong or if there is an error in the code. Thanks




oh btw, I'll be uploading the Simple Chat 2.8 as soon as I comment the code.
It uses what I have shown you above and a little more. ;) so watch out for it.
I'm also doing something I havent seen on here. so keep ya eye out.



Thanks for reading




CrAKiN-ShOt

crakinshot@hotmail.com



Comments on this post

No comments have been added for this post.

You must be logged in to make a comment.