Jump to content
xisto Community
ghostrider

An Indepth Look At The Winsock Control (part Iv) Part IV - New Module and the ID system

Recommended Posts

An Indepth Look at the Winsock Control

Part IV - New Module and the ID system

 

Whats Covered in Part IV:

I want to cover a lot in Part IV, considering I was grounded for a month and that really delayed the next part of my tutorial. There's going to be a lot covered. We're going to create the form where people talk to each other, create a module for data such as the name of the room and which people are in it, and also place a few more Winsock controls on the main form for UDP purposes. This is going to be a rather long part,because I'm introducing a lot of new stuff we haven't covered, and reviewing a lot too. If it gets too long I'm going to just make the original stuff planned for Part IV in Part V .

 

If you haven't read parts I, II, and III, click here and read them. What you need for Part IV:I'm not going to post all properties and code from Part III. You caneither download the project from Part III in ZIP format, or go here and have the fun of entering all the properties and code yourself.

The Module:

Create a module in your project and call it modData. This module will be used for storing important data such as the name of the room, and the people in the room, along with their names and IDs.If that didn't make any sense to you at all, this is what I mean. When the user connects to the server, the server will assign them an ID, and send a message to all the other users in the room telling them that a new user has enterted the room and what there ID number is.

 

Each Winsock control can only handle one connection. This means that we need to have an array of Winsock controls on the server. The Client only needs to have 2 Winsock controls, one for TCP communications, which is going to be used for actually sending the data, and one for UDP connections, which will be used for when a user enters and leaves the rooms. Just in case this is still somewhat confusing, I've drawn some pictures using Paint (or at least tried to draw) to aid with explaining this.

 

This first picture shows how all the users in the chat room are connected to each other.

 

Posted Image

http://forums.xisto.com/no_longer_exists/404.png

 

The red line in these pictures are the connections between the computers. Notice that Clients 1-4 all connect to the server. In this case Client 1 would have an ID of 1, Client 2 would have an ID of two, client 3 has and ID of 3 and so on and so forth. With the system I am going to use, out chat room can support one server, and 255 clients per chat room.

 

Advantages of connecting this way:

The model I have showed you is the way many programs with one server and many clients connect. Direct X uses the same scheme for its networking, and I'm sure many other programs utilize it, too. The first reason is to save RAM and resources on the computer. TCP connections are NOT like UDP connections, they take up space and computing power. Also if every computer was connected to one another, every time someone said something, that computer would need to send that data to all the different computers and remember which computers it sent it to. That would mean un-neccessary work for us as programmers, and for the itself. The second reason is security. When every computer is connected to each other, they all know each other's IP addresses, which is a security risk. In our model, each client knows two IP addresses, its own IP address and the server's.

 

How data is sent and the ID system:

Our ID system is used for determining which computer sent what, and so that the other users in

 

the room can figure out who said what. Below is the process of sending text.

 

1. User types the text and presses the send button

2. EasyChat takes the text and puts the User's ID in front of it. The ID will always be one byte long, and the server's ID is always equal to 0.

3. The data is sent to the server.

4. The server sends the data to all the other clients

 

Below is another picture describing this process. Assume that Client 1 is the one sending the data, and Client 2 and Client 3 are receiving it.

 

Posted Image

http://forums.xisto.com/no_longer_exists/404.png

 

Why only 256 people can be in one room:

The ID system uses only one byte. Each bite is comprised of 8 bits, each being either zero or one.

 

If you look at binary (base 2), you will see this pattern

00000001 - 1

00000010 - 2

00000100 - 4

00001000 - 8

00010000 - 16

00100000 - 32

01000000 - 64

10000000 - 128

 

If you know your exponents, you will notice that each bit is an exponent of two. The first one is 2 the the zeroth power, the next is 2 to the first, the 3rd is 2 squared, the 4th is 2 cubed and so on.

 

If you add up 2^7 + 2^6 + 2^5 ... + 2^0 you get 256 possible values for each byte.

 

Finally some code:

Put this code in modData

Option Explicit 'Always have this in any module!Public IsServer As BooleanPublic RoomName As String * 64 'Room name can only be 64 characters long.Public TotalPeople As Byte 'While it may be easier for us to use'an integer, a byte saves on whole byte of memory, and saving'memory is always important.Type UserDataUserName As String * 16ID As Byte 'The byte values has 256 possible values, which is'perfect for the ID.IPAddress As String * 4 'When your not using the WinSock control,'an IP address is just 4 characters long; it has no periods.'This will save a HUGE amount of space if a lot of people are in'the room.  Its also important to know how to use different types'not supported in Visual Basic, such as the word (2 bytes), and the'dword (double word - 4 bytes).  In VB these are named the integer'and the long, respectively.  THE IP ADDRESS FIELD IS ONLY FILLED'IN WHEN THE PROGRAM IS THE SERVER.Available As Boolean 'ONLY USED BY SERVER.  This allows the server'to quickly find a new ID for a person who enters the room.End TypePublic Users(0 To 255) As UserData 'Our big array of users
This code isn't that complex, it has a couple variables that tell the program what mode to run it in, and then the array for all the possible users that could be in our room. Notice that the array begins with zero. In languages other than VB, most of the time things start on zero instead of one.

 

Go into Option1_Click() in frmStart and add this line just before End Sub

IsServer = True

Go into Option2_Click() in frmStart and add this line at the end of the sub

IsServer = False

These two options tell our program which way to operate.

 

This part of my tutorial has gotten much longer than I expected, however it is very important that you understand ALL of this information before you go on to Part V. The rest of the stuff planned for Part IV will be finished in Part V, including the new forms. Part VI is going to cover some new concepts using a different type of networking technology, called the Windows Sockets. Part VII will be a lot of code to write, and hopefully by Part VIII we will have a working program.

 

Happy Holidays

Edited by ghostrider (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.