Results 1 to 1 of 1

Thread: Winsock Tutorial 1

  1. #1

    Thread Starter
    Retired VBF Adm1nistrator plenderj's Avatar
    Join Date
    Jan 2001
    Location
    Dublin, Ireland
    Posts
    10,359

    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:
    1. Private Sub Form_Load()
    2.    
    3. 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:
    1. Private Sub Form_Load()
    2.     Winsock1.Connect "mail.tcd.ie", 25        ' smtp
    3.     'Winsock1.Connect "mail.tcd.ie", 23       ' telnet
    4.     'Winsock1.Connect "mail.indigo.ie", 110   ' pop3
    5.     'Winsock1.Connect "ftp.indigo.ie", 21     ' ftp
    6. 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:
    1. Private Sub Winsock1_Connect()
    2.     Debug.Print "Connected !", vbInformation
    3. End Sub
    4.  
    5. 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)
    6.     Debug.Print "Error occured : (" & Scode & ":" & Description & ")"
    7. 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:
    1. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    2.    
    3. 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:
    1. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    2.     Dim strBuff As String
    3.     Winsock1.GetData strBuff, vbString
    4. 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:
    1. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    2.     Dim strBuff As String
    3.     Winsock1.GetData strBuff, vbString
    4.     MsgBox "We received the following data : " & vbCrLf & strBuff, vbInformation
    5. End Sub

    So the code in your form should look like this ;

    VB Code:
    1. Private Sub Form_Load()
    2.     Winsock1.Connect "mail.tcd.ie", 25        ' smtp
    3.     'Winsock1.Connect "mail.tcd.ie", 21       ' telnet
    4.     'Winsock1.Connect "mail.indigo.ie", 110   ' pop3
    5.     'Winsock1.Connect "ftp.indigo.ie", 23     ' ftp
    6. End Sub
    7.  
    8. Private Sub Winsock1_Connect()
    9.     Debug.Print "Connected !", vbInformation
    10. End Sub
    11.  
    12. 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)
    13.     Debug.Print "Error occured : (" & Scode & ":" & Description & ")"
    14. End Sub
    15.  
    16. Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    17.     Dim strBuff As String
    18.     Winsock1.GetData strBuff, vbString
    19.     MsgBox "We received the following data : " & vbCrLf & strBuff, vbInformation
    20. 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
  •  



Click Here to Expand Forum to Full Width