|
-
Mar 24th, 2003, 01:14 PM
#1
Thread Starter
Retired VBF Adm1nistrator
Winsock Tutorial 1
Winsock Tutorial 1
What is the winsock control ?
The winsock control is an ActiveX control that allows developers to easily
create applications that use TCP sockets or send and receive UDP data.
A description of TCP and UDP can be found *here*
Where can I find the winsock control ?
To use the winsock control in your application, do the following ;
Project Menubar > Components > Microsoft Winsock Control
The winsock control comes with windows, but as with most ActiveX controls that
come as default with windows, you may or may not have the latest.
The latest version available at the time of writing comes with Service Pack 5,
so for me it is listed as "Microsoft Winsock Control 6.0 (SP5)"
The control is placed on your form in exactly the same way you would any
other ActiveX control.
Just select it from the ToolBox, and then draw it onto your form.
I normally stick it in the corner of the form so its out of my way.
Lets get coding !
Right. First off, we'll start to look at a simple TCP application.
Place a single Winsock control on your form.
Double-click on the grey of your Form.
This should bring you into the code view, and VB should have entered code for
the Form_Load() event of your Form.
If it hasn't, just stick in the following ;
VB Code:
Private Sub Form_Load()
End Sub
So we're going to do something in the Form_Load() event.
I'll assume that you have not renamed the winsock control, and it is still
called Winsock1. The name is irrelevant, but in my code I'm coding for Winsock1.
The first thing we're going to do is create an application that will connect
to some remote host on some port and display any received data in a message box.
We're going to use the .Connect() method of the Winsock control.
If no parameters are passed to this method, it will connect to whatever is in
the .RemoteHost and .RemotePort properties of the control.
If you do pass parameters, they will be treated as the Host and Port respectively.
The RemoteHost is the IP Address or Hostname of the computer we wish to connect to.
The RemotePort is a value 1 <= RemotePort <= 65535, which says what port on the
remote computer we will connect to.
Common ports would be as follows, Telnet:21, FTP:23, SMTP:25, HTTP:80, POP3:110
As we haven't put any textboxes or anything on our form yet, we have no way of
asking the user what port or host they want to connect to.
So let's just hardcode something into the application for now.
I'm going to connect to mail.tcd.ie on port 25
This is the SMTP port on the mail server of my university.
You can use this, or something else if you would prefer.
In the code are also some other examples commented out.
VB Code:
Private Sub Form_Load()
Winsock1.Connect "mail.tcd.ie", 25 ' smtp
'Winsock1.Connect "mail.tcd.ie", 23 ' telnet
'Winsock1.Connect "mail.indigo.ie", 110 ' pop3
'Winsock1.Connect "ftp.indigo.ie", 21 ' ftp
End Sub
So when the form loads, the Winsock control will connect to mail.tcd.ie on port 25.
But how do we know if the control connects, or there was a problem...?
Well we use the Winsock events for this.
Add another 2 Event Subs to your code;
VB Code:
Private Sub Winsock1_Connect()
Debug.Print "Connected !", vbInformation
End Sub
Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
Debug.Print "Error occured : (" & Scode & ":" & Description & ")"
End Sub
The Connect() Event Sub will be called if the Winsock control manages to connect
to the remotehost, and the Error() Event Sub will be called if an error occurs.
We would want to put code into the Connect() Sub if the service we're connecting to
requires that we send data first (eg. HTTP, IRC).
In this case, SMTP, the SMTP server will send us some data first before we do.
Right so we're connected, and we know that the server will want to send us some data,
but how do we actually get the data ?
Well there is an Event Sub to handle this ; DataArrival().
So add the following Event Sub ;
VB Code:
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
End Sub
Right so we know there's an Event Sub to deal with it, but what do we actually do ?
Well now that we know some data has just arrived, we can use the GetData method ;
VB Code:
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim strBuff As String
Winsock1.GetData strBuff, vbString
End Sub
So we're declaring a variable to hold the received data from the winsock control,
and then we're getting the data from the winsock control and putting it into strBuff.
How would we display it though ? Easy, just use MsgBox ;
VB Code:
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim strBuff As String
Winsock1.GetData strBuff, vbString
MsgBox "We received the following data : " & vbCrLf & strBuff, vbInformation
End Sub
So the code in your form should look like this ;
VB Code:
Private Sub Form_Load()
Winsock1.Connect "mail.tcd.ie", 25 ' smtp
'Winsock1.Connect "mail.tcd.ie", 21 ' telnet
'Winsock1.Connect "mail.indigo.ie", 110 ' pop3
'Winsock1.Connect "ftp.indigo.ie", 23 ' ftp
End Sub
Private Sub Winsock1_Connect()
Debug.Print "Connected !", vbInformation
End Sub
Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
Debug.Print "Error occured : (" & Scode & ":" & Description & ")"
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim strBuff As String
Winsock1.GetData strBuff, vbString
MsgBox "We received the following data : " & vbCrLf & strBuff, vbInformation
End Sub
When you hit F5, Run->Start, or hit the blue Play button, the winsock control should
connect to mail.tcd.ie, receive a little data, and then display it to you.
If there was an error, the Error() Event Sub should pick that up and display it.
That's it for the first winsock tutorial.
If you had any problems, e-mail me at [email protected] and I'll see whats up.
Jamie Plenderleith
[email protected]
08 / 02 / 02
Last edited by plenderj; Feb 25th, 2005 at 05:46 AM.
Microsoft MVP : Visual Developer - Visual Basic [2004-2005]
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|