Visual Basic UDP Sample Source Code
Network Component provides an easy-to-use development interface to a variety of IP protocols. By using Network Component, you can very easily create or enhance applications with network features.
Network Component features the following: DNS, FTP, HTTP, HTTPs, ICMP Ping, IP-to-Country, MSN, NTP, RSH, SCP, SFTP, SNMP v1/v2c (Get, GetNext, Set), SNMP Traps, SNMP MIB, SSH, TCP, Telnet, TFTP, UDP, Telnet, Wake-On-LAN and more.
Network Component can be well integrated into any development platform that supports ActiveX objects.
Step 1: Download and install the Network Component
Download Network Component from the ActiveXperts Download Site and start the installation. The installation guides you through the installation process.
Step 2: Create a new Visual Basic project
Launch 'Microsoft Visual Basic' from the Start menu, and choose 'New' from the 'File Menu'. The 'New Project' dialog appears.
Select 'Standard Exe' and click 'OK':
Step 3: Refer to the Network Component Library and create the objects
A new Project is created, with a blank form.
First, you must add a reference to Network Component in the project to be able to use the object. To do so, choose 'References...' from the 'Project' menu. In the 'References' dialog that pops up, enable the 'Network Component 3.1 Type Library' reference as shown in the following picture:
Click 'OK' to close the 'References...' dialog.
Then, select the Project form and choose 'View Code' from the context menu:
On top of your code, declare the following object:
Public objUdp As AxNetwork.Udp
Step 4: Create the object
From the Code window, select 'Form'. The Private Sub 'Form_Load()' will be displayed now. In the 'Form Load' function, create the object in the following way:
Set objUdp = CreateObject("AxNetwork.Udp")
Appendix: Full source code
Option Explicit
Public objSocketClient As AxNetwork.Udp
Public objSocketServer As AxNetwork.Udp
Public objConstants As AxNetwork.NwConstants
Public bServerListning As Boolean
Public bClientConnected As Boolean
Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Private Const MAX_PATH = 260
'///////////////////////////////////////////////////////////////////////
Private Sub Form_Load()
bServerListning = False
Set objSocketClient = CreateObject("AxNetwork.Udp")
Set objSocketServer = CreateObject("AxNetwork.Udp")
Set objConstants = CreateObject("AxNetwork.NwConstants")
SetDefaultLogFile
UpdateControls
End Sub
Private Sub UpdateControls()
btnListen.Enabled = Not bServerListning
btnServerClose.Enabled = bServerListning
txtReceived.Enabled = bServerListning
txtServerPort.Enabled = Not bServerListning
cbIp6.Enabled = Not bServerListning
txtHost.Enabled = Not bClientConnected
txtClientPort.Enabled = Not bClientConnected
btnOpen.Enabled = Not bClientConnected
txtCommand.Enabled = bClientConnected
btnSend.Enabled = bClientConnected
btnClientClose.Enabled = bClientConnected
End Sub
'///////////////////////////////////////////////////////////////////////
Private Sub btnClientClose_Click()
objSocketClient.Close
If (GetClientResult() = 0) Then
bClientConnected = False
UpdateControls
End If
End Sub
'///////////////////////////////////////////////////////////////////////
Private Sub btnListen_Click()
objSocketServer.IOTimeout = 100
objSocketServer.LogFile = txtLogFile.Text
objSocketServer.Open "localhost", CInt(txtServerPort.Text), True, cbIp6
If (GetServerResult() = 0) Then
bServerListning = True
UpdateControls
End If
End Sub
'///////////////////////////////////////////////////////////////////////
Private Sub btnOpen_Click()
objSocketClient.IOTimeout = 100
objSocketClient.LogFile = txtLogFile.Text
objSocketClient.Open txtHost.Text, CInt(txtClientPort.Text), False
If (GetClientResult() = 0) Then
bClientConnected = True
UpdateControls
End If
End Sub
'///////////////////////////////////////////////////////////////////////
Private Sub btnSend_Click()
objSocketClient.SendString (txtCommand.Text)
End Sub
'///////////////////////////////////////////////////////////////////////
Private Sub btnServerClose_Click()
objSocketServer.Close
If (GetServerResult() = 0) Then
bServerListning = False
UpdateControls
End If
End Sub
'///////////////////////////////////////////////////////////////////////
Private Sub Timer1_Timer()
Dim strReceivedLine As String
Dim strReceivedData As String
strReceivedData = objSocketServer.ReceiveString
If (Len(strReceivedData) <> 0) Then
strReceivedLine = "[" + objSocketServer.RemoteAddress + "] " + strReceivedData
txtReceived.Text = txtReceived.Text + strReceivedLine + vbCrLf
End If
End Sub
'///////////////////////////////////////////////////////////////////////
Private Sub btnView_Click()
If FileExists(txtLogFile.Text) = True Then
Shell "notepad " + txtLogFile.Text, vbNormalFocus
End If
End Sub
'///////////////////////////////////////////////////////////////////////
Public Function FileExists(sFileName As String) As Boolean
FileExists = CBool(Len(Dir$(sFileName))) And CBool(Len(sFileName))
End Function
'///////////////////////////////////////////////////////////////////////
Private Function SetDefaultLogFile()
Dim Buffer As String
Buffer = Space(MAX_PATH)
If GetTempPath(MAX_PATH, Buffer) <> 0 Then
txtLogFile.Text = Left$(Buffer, InStr(Buffer, vbNullChar) - 1) & "Udp.log"
Else
txtLogFile.Text = "C:\Udp.log"
End If
End Function
Private Sub Form_Unload(Cancel As Integer)
objSocketClient.Close
objSocketServer.Close
End Sub
'///////////////////////////////////////////////////////////////////////
Private Function GetClientResult() As Long
Dim lResult As Long
lResult = objSocketClient.LastError
txtClientResult.Text = lResult & ": " & objSocketClient.GetErrorDescription(lResult)
GetClientResult = lResult
End Function
'///////////////////////////////////////////////////////////////////////
Private Function GetServerResult() As Long
Dim lResult As Long
lResult = objSocketServer.LastError
txtServerResult.Text = lResult & ": " & objSocketServer.GetErrorDescription(lResult)
GetServerResult = lResult
End Function
You can download the complete samples here. There are many other working Network Component scripts on our site and shipped with the product.
NOTE: Demo Projects are created with Microsoft Visual Studio 2008
The Network Component project ships with a set of Microsoft Visual Studio .NET samples, including samples for Microsoft Visual C# .NET. The projects are created with Microsoft Visual Studio 2008.
Users with a later version of Microsoft Visual Studio can open such a project. The Visual Studio Conversion Wizard will guide you through the process of converting the project to the version used.



