MMOCC the isometric world scene, 2006–now
Archive. Archived from the original MMOCC Forum (2006–2010), recovered from the Internet Archive. The forum is read-only history now — the scene talks on Discord. If you posted here and want something removed, get in touch.

VB6 + Winsock.

10 posts · vBulletin era
#1
Visual Basic 6.0 + Winsock Control

This lesson will show you the BASICS of the winsock control in the visual basc 6.0 enviroment. This will show you the following:

- How to make a server listen on a port
- How to accept 1 connection
- How to send data to the client
- How to receive data
- How to connect to the server
- How to receive data from the server.

Required:
- Visual Basic 6.0
- MSWINSCK.OCX




Missing MSWINSCK.OCX?

Why do you need this control?
This is an ActiveX control, Which allows you to use the Winsock Control within VB.


How to obtain the control:
http://www.ascentive.com/support/new...b/MSWINSCK.OCX

Simply download it from the URL above, Then follow the next instructions.

Press Start >> My Computer >> C Drive (Or whatever drive your using) >> Windows

In the directory you will find quite alot of folders. Drag the MSWINSCK.OCX into system32 folder.

If it asks if you want to replace MSWINSCK.OCX, Just click yes.



Right, Now you know you can use the winsock control. Let's get cracking shall we?




Open Visual Basic 6.0

A screen will appear with a few options, Simply select 'Standard EXE'

You will now have a grey form.

The apperance of the forms does not matter at this stage...Since it's only practise.


To load the winsock control into your form simply press 'Ctrl + T'

A list of components will appear, Simply find and tick 'Microsoft Winsock Control 6.0', Then press apply and ok.


You will now have a new object in the object list...See it? Good.


The rest of the tutorial will be split into two parts.


Server Programming and Client Programming.


Server Programming:

Place 1 winsock control onto your form.

If you click the winsock control and look at it's properties, You'll see it's automatically called 'Winsock1'

Rename this control to: sckserver

Now, We're ready to start programming.

The first thing a server has todo is listen on a port for incoming connections from clients.

Todo this, Simply double click your form. And put:

sckserver.localport="1405"
sckserver.listen


What's this doing? This is telling the sckserver to listen on port 1405 for incoming connections.

Now we have set the server up to listen on port 1405 for incoming connections, Now we have to deal with accepting connections.


All connection requests will be dealt with in sckserver connectionRequest procedure.


It will look something like this:


Private Sub sckserver_ConnectionRequest(ByVal requestID As Long)



End Sub



Whenever the server receives a connectionRequest whatever code is inside the above procedure, Will be triggered.

So inside the above procedure we have to:

- Accept The Connection

- Send Data to the client to show they are connected.

And because we are only making it accept 1 connection, You will have to stop the server from listening aswell.


So, The first thing we do, Is stop the server from listening:

Which is pretty simple.

sckserver.Close

Now we have to accept the incoming connection, Which is quite simple:

sckserver.accept requestid


Now we have to send data to the client, To let the client know it's been accepted.

sckserver.SendData "Connected"

The above line is sending a packet containing the words 'Connected' to the client.


So let's recap. So far we have:

- Make the server listen on port 1405 for incoming connections

- Made the server accept connections when requested.

You should have this coding so far:


Private Sub Form_Load()
sckserver.LocalPort = "1405"
sckserver.Listen

End Sub

Private Sub sckserver_ConnectionRequest(ByVal requestID As Long)
sckserver.Close

sckserver.Accept requestID

sckserver.SendData "Connected"
End Sub



One last bit we have todo on our server, That's get it ready for receiving data from the connected client

Like the ConnectionRequest procedure, Winsock control has another procedure for receiving data, It's called:

'DataArrival'

It will look like this:


Private Sub sckserver_DataArrival(ByVal bytesTotal As Long)


End Sub


When data is received, Whatever code is in that procedure will be carried out.

Before we can do anything, We need to store all incoming data in a variable.

So first, Declare a variable, We will name it buffer.

So within the dataArrival procedure. Simply put.

Dim Buffer as string

This is the variable all the incoming data will be stored.

You may be wondering how do you get the incoming data inside the variable, Simply like this:

sckserver.GetData buffer

Now all that is doing, Is getting all the data and storing it in the buffer variable we declared not so long ago.

Now to make sure the server is receiving data, We will make it display in a message box.

msgbox buffer

You should now have this code:

Private Sub Form_Load()
sckserver.LocalPort = "1405"
sckserver.Listen

End Sub

Private Sub sckserver_ConnectionRequest(ByVal requestID As Long)
sckserver.Close

sckserver.Accept requestID

sckserver.SendData "Connected"
End Sub


Private Sub sckserver_DataArrival(ByVal bytesTotal As Long)
Dim buffer As String

sckserver.GetData buffer


msgbox buffer


End Sub



The points convered were:

- Making the server listen on port 1405

- Accepting incoming connections

- Sending data to the connecting client

- Receiving Incoming Data

- Displaying incoming data.

That's the server over and done with.

Save your code.

Client Programming:

From reading the server programming tutorial above, I'll assume you know how to load in the winsock control and rename it.

So now, Start a new project for the client.

Load in the winsock control (read server programming if you don't know how)

Rename the winsock control: sckclient


Now what do we have todo on the client?

- Connect to server

- Display Data

- Send Data

much easier than the server

Create a command button on your form.

Double click it. You will now be able to write code which will trigger when the command button is pressed... So when the command button is press we want it to connect to our server. Do this simply like this:

sckclient.Connect "[redacted-ip]","1405"

Because for this example you will be testing the server locally, You will need to use ip [redacted-ip], And because we made the server listen on port 1405, We will need to make it connect to localhost on port 1405.

You should now be connected to your server.. (Simple eh? )


The rest is pretty much the same as the server.


End result should be this:

Private Sub Command1_Click()
sckclient.Connect "[redacted-ip]", "1405"

End Sub

Private Sub sckclient_DataArrival(ByVal bytesTotal As Long)
Dim buffer As String

sckclient.GetData buffer

MsgBox buffer

sckclient.SendData "Client"

End Sub


So what's happening here?

Behind Command1 , You are connecting to ip [redacted-ip] on port 1405, Which your server will be listening on.

Then notice we're inside the sckclient_DataArrival procedure... As i explained, This will trigger all code within once data is received.

Let's take it line by line and explain yes?

Dim buffer as string

This is the variable we declare to store all incoming data.

sckclient.GetData buffer

This will get all incoming data and store it in the variable 'buffer'

msgbox buffer

This will show the contents of buffer in a message box.

sckclient.SendData "Client"

This sends data to the server, The data being sent is the word 'Client'

Save your code. Run your server first, Then run the client. Press, You should get two message boxes. One on the server and one on the client.

------------------------------------------------------

Please note, This tutorial is just a stepping stone to creating servers and clients in vb. Do not use this tutorial if your planning on making a full chat software package, Or server for games. This is just a guideline to the winsock control. There is much more to learn.

Written by: Damien
#2
Nice tutorial, but can i point out that you should ALWAYS name all your controls, from Labels to the actual Project its self.

You didn't name your command buttons !!

it makes it a lot easier to uderstand etc.
#3
At that stage i got a bit lazy with the tutorial :p So i didn't bother naming the command button.

Also to point out, When sending your winsock application to mates, Or if you use any external activeX controls in your program. Always include the files when sending to users.
#4
Yeah lol!

Nothing worst then "IT SAYS MIZZING OSX HELP PLZ"..
#5
Lmao :p Too true.
#6
Thanks Damien And you did all that for me As I was having problems

I'll give rep when possible.

Great stuff.
#7
Finally got round to looking at it eh :p
#8
Wow! What a great tutorial! +Rep
#9
Thanks I may also write another part of the tutorial. On how to handle multiple connections. And if i feel in the mood enough, I may even write a tutorial on how to protect your vb servers from basic packet editing methods without the use of encryption.
#10
+rep good for beginners

Recovered from /coding-tutorials/6208-vb6-winsock.html · captured 2008.