Internet Programming--Visual Basic and Socket Wrench controls

Below is a piece of Visual Basic code I wrote to drive a proprietary socket control, in an echo (ping) client-and-server application.

I have also done course work using Berkeley socket calls in C directly, and am currently studying the Visual C++ wrappers to berkeley sockets (winsock), which also interfaces to windows messaging.

I would love the chance to be doing threaded socket-level internet programming all the time.

 previous      next (UT supercomputer center)     back up to work samples
 

VERSION 4.00
Begin VB.Form Form1 
   Caption         =   "Form1"
   ClientHeight    =   4170
   ClientLeft      =   1680
   ClientTop       =   2910
   ClientWidth     =   4635
   Height          =   4575
   Left            =   1620
   LinkTopic       =   "Form1"
   ScaleHeight     =   4170
   ScaleWidth      =   4635
   Top             =   2565
   Width           =   4755
   Begin VB.TextBox Text2 
      Enabled         =   0   'False
      Height          =   375
      Left            =   840
      TabIndex        =   6
      Top             =   960
      Width           =   3495
   End
   Begin VB.CommandButton Command1 
...(some control definitions skipped)...
   End
   Begin SocketWrenchCtrl.Socket Socket1 
      Left            =   240
      Top             =   2880
      _Version        =   65536
      _ExtentX        =   741
      _ExtentY        =   741
      _StockProps     =   0
      AutoResolve     =   -1  'True
      Backlog         =   5
      Binary          =   0   'False
      Blocking        =   0   'False
      Broadcast       =   0   'False
      BufferSize      =   1024
      HostAddress     =   ""
      HostFile        =   ""
      HostName        =   ""
      InLine          =   0   'False
      Interval        =   0
      KeepAlive       =   0   'False
      Library         =   ""
      Linger          =   0
      LocalPort       =   0
      LocalService    =   ""
      Protocol        =   0
      RemotePort      =   0
      RemoteService   =   ""
      ReuseAddress    =   0   'False
      Route           =   -1  'True
      Timeout         =   0
      Type            =   1
      Urgent          =   0   'False
   End
End
Attribute VB_Name = "Form1"
Attribute VB_Creatable = False
Attribute VB_Exposed = False
Option Explicit
Dim LastSocket As Integer

Private Sub Command1_Click()
  Dim Host As String
  Socket1.Disconnect
  Host = Trim$(Text1.Text)
  If Host = "0.0.0.0" Or Host = "" Then
    Socket1.HostAddress = Socket2(0).AdapterAddress(0)
    Text1.Text = Socket1.HostAddress   ' (to loop back)
  Else
    Socket1.HostName = Host
  End If
  Socket1.RemotePort = IPPORT_ECHO
  If Socket1.Connect <> 0 Then
    MsgBox "Unable to connect to remote host"
  End If
End Sub

Private Sub Form_Load()
  Socket1.AddressFamily = AF_INET
  Socket1.Protocol = IPPROTO_IP
  Socket1.SocketType = SOCK_STREAM
  Socket1.Binary = False
  Socket1.Blocking = False
  Socket1.BufferSize = 1024
  
  Socket2(0).AddressFamily = AF_INET
  Socket2(0).Protocol = IPPROTO_IP
  Socket2(0).SocketType = SOCK_STREAM
  Socket2(0).Blocking = False
  Socket2(0).LocalPort = IPPORT_ECHO
  Socket2(0).Action = SOCKET_LISTEN
  LastSocket = 0
  
  lblStatus = "Listening on " + Socket2(0).AdapterAddress(0)
'To enable trace of sockets API calls.
  'Socket2(0).TraceFile = ".\tracelog.txt"
  'Socket2(0).Trace = True
End Sub

Private Sub Socket1_Connect()
    Text2.Enabled = True
    Text2.SetFocus
    Text3.Enabled = True
End Sub

Private Sub Socket1_Read(DataLength As Integer, IsUrgent As Integer)
    Dim strBuffer As String
    Socket1.Read strBuffer, DataLength
    Text3.Text = strBuffer
End Sub


Private Sub Socket2_Accept(Index As Integer, SocketId As Integer)
  
'Note: Index=0.  The (connection request) event
'(misnamed an 'Accept' event) handled here, comes in on
'Socket2(0).  Socket2(0) remains the listening socket.
'SocketId is the "handle" of Socket2(0), which is passed to
'to the Accept property (which should be a method) of
'_another_ socket in the array,
'which is the means of causing this new socket to
'accept the connection request...
    
'Find or create a free free socket in the array
'to accept new connection
    Dim I As Integer
    For I = 1 To LastSocket
        If Not Socket2(I).Connected Then Exit For
    Next I
    If I > LastSocket Then
        LastSocket = LastSocket + 1: I = LastSocket
        Load Socket2(I)
    End If
    
'Initialize new socket
    Socket2(I).AddressFamily = AF_INET
    Socket2(I).Protocol = IPPROTO_IP
    Socket2(I).SocketType = SOCK_STREAM
    Socket2(I).Binary = True
    Socket2(I).BufferSize = 1024
    Socket2(I).Blocking = False
    
' Accept connection on
' newly activated socket array element
    Socket2(I).Accept = SocketId
    
End Sub

Private Sub Socket2_Disconnect(Index As Integer)
    Socket2(Index).Action = SOCKET_CLOSE
End Sub

Private Sub Socket2_Read(Index As Integer, DataLength As Integer, IsUrgent As Integer)
    Socket2(Index).RecvLen = DataLength
    Socket2(Index).SendLen = DataLength
    Socket2(Index).SendData = Socket2(Index).RecvData
End Sub

Private Sub Text2_KeyPress(KeyAscii As Integer)
    Dim strText As String
    If KeyAscii = 13 Then
        strText = Text2.Text
        Socket1.Write strText, Len(strText)
        KeyAscii = 0: Text2.Text = ""
    End If
End Sub

 previous      next (UT supercomputer center)     back up to work samples