Microsoft & .NET Visual Basic Communicating over a Network

Communicating over a Network

Have you ever wanted to send a simple string of text over your local LAN? Or maybe you have wanted to send and receive pictures and files with a friend from the other side of the world. Whatever your needs, the Winsock control can help out.

In this article you will learn the basics
of the Winsock control, and how you can use it to transfer data and
information over a network and the Internet.

The Winsock control does not provide us
with many properties, but it gives us many events and methods
to play with. The most important properties are:

  • RemoteHost – The IP/Machine name of the
    remote machine
  • RemotePort – The remote port number that
    the remote machine is listening
    to
  • LocalPort – The local port number
    to listen to
  • As you can see, these three properties
    are quite important. When connecting, you will need to set the
    RemoteHost and RemotePort, but when you are listening you will only
    need to set the local port property.

    Like the Internet Transfer Control,
    the Winsock control provides us with some constants. These include
    ErrorConstants, StateConstants and ProtocolConstants. To see a
    list of them, click View, Object Browser (or Press F2). From the
    drop down combo, select MSWinsockLib. You can now see the
    enumerations of constants. For this article, we will mainly be
    using the default TCP (Transmission Control Protocol).

    Connecting to a computer is not as
    easy as just calling the Connect method and specifying a remote
    IP and port. You need to do a fair amount of ‘handshaking’, which
    is making sure that the other computer is still there and
    wanting to connect.

    First, before starting a connection, it’s always
    good practice to close any current operations before you
    start to ensure that the Winsock control is available:

    Winsock1.Close
    While Winsock1.State
      DoEvents
    Wend
    

    See the use of the While Wend loop?
    That is to make sure that you don’t start trying to do something
    while the winsock control is still finishing an operation.

    When connecting, you will also need to check for errors:

    If Err Then
      Msgbox Err.Number, Err.Description
      Err = 0
    End If
    

    As done before, you need to make sure
    that you don’t start doing tasks before the control is ready:

    While Winsock1.Waiting
      DoEvents
    Wend
    

    Most of the code you have seen now
    forms the bulk of a Connect method. You will now learn about the Listen method. The Listen method is what the receiving computer must do to hear Connect methods.

    As before, we need to check for a
    current connection:

    If Winsock1.State Then
      Winsock1.Close
    End If
    

    Otherwise, listening is quite
    simple. Just use the Listen method of the Winsock control to
    do so:

    Winsock1.LocalPort = 1008
    Winsock1.Listen
    

    You may have noticed the use of a
    port number above. This is possible because when creating comms apps you can use
    just about any port number. Below is a list of ports to avoid
    (or use for that matter) if you are creating a program because
    if you try and listen to the same port as a mail server, then
    one of the two fail.

    • SMTP: 25
    • POP: 110
    • FTP: 21
    • HTTP: 80
    • NNTP: 119
    • Telnet: 23
    • Gopher: 70
    • IRC: 6667

    When it comes to sending text,
    things become very messy. Before starting, you need to
    sort out how your applications are going to communicate. For instance, assume you
    need to let the other end know what you are sending (text,
    file, etc). The best way to do this is to create a module that
    contains a load of constants that you can use to tell your
    program what’s going on:

    Public Const DATATEXT As Byte = 0
    

    This way, every time you want to
    send a piece of text you send the DATATEXT constant first,
    then the size of it, and finally the actual content. Using
    this standard of operation, you can make your apps work
    together very nicely. At each point along the way you need
    to do two things: check for errors and wait until the
    current operation has been completed.

    The best way to handle errors
    is to use events.

    In Visual Basic, you can
    create your own events. An example event might be when you
    click on a button:

    Private Sub Command1_Click()
    

    You call or ‘raise’ events
    using the RaiseEvent method. This method accepts one
    parameter, which is one of the events declared. You
    declare an event like this:

    Public Event TextSent(TextContent As String)
    

    The event procedure for this
    event will look like this:

    Private Sub MyModule_TextSent(TextContent As String)
    

    The last thing you will discover in this article are operation time outs. When using the
    Winsock control, or in fact any Internet comms
    control, you need to be able to detect a time out. This
    occurs, for instance, when an application you are
    talking to stops responding while you are communicating
    with it. If you didn’t watch for a time out, then you
    program would just sit around waiting for the other
    program to respond. The easiest way to watch for these
    is by using a timer control.

    By setting the Interval
    property to a reasonable length, for instance 30000, then you
    can detect time outs. Again, use events to tell the user
    what’s going on. You need to do this in the timers Timer
    event:

    Private Sub Timer1_Timer()
    
    RaiseEvent TimeOut
    
    End Sub
    

    To finish, here
    are some quick guidelines for specifying addresses:

    1. Where possible use an IP
      address (127.0.0.1) instead of computer names because
      the Winsock control has to resolve a host name to an IP
      address – this takes some time.
    2. If you connect to the net
      over a dial up connection, you can still get this to
      work. When you have logged on, use the Winsock’s LocalIP
      method to return to current IP address. This way you can
      send it to your friend and he/she can connect to you.

    Latest Posts

    Related Stories