Dragon's Course Project

GUI for TCP/IP Server:

VB Code for TCP/IP Server

' FILE: DAQServer3
' DATE: 05/26/03
' AUTH:C.Hu

Option Explicit

'Declare variables
Dim BaseAddress As Integer: ' 8255 Base Address
Dim Dummy As Integer: ' Dummy variable used with DLL
Dim data As String


Private Sub cmdExit_Click()
'Turn off serial port'
MSComm1.PortOpen = False
Form_Terminate
End
End Sub

Private Sub Form_Load()
' Set up settings for serial communication'
MSComm1.Settings = "9600,n,8,1"
MSComm1.CommPort = 1
MSComm1.PortOpen = True
MSComm1.InputLen = 0
' TCP server settings
tcpServer.LocalPort = 5010
Call tcpServer.Listen
End Sub


Private Sub Form_Terminate()
If MSComm1.PortOpen Then MSComm1.PortOpen = False
End Sub

Private Sub tcpServer_Close()
'Turn off LEDs
If MSComm1.PortOpen Then MSComm1.PortOpen = False

Call tcpServer.Close ' Client closed, server should too
txtOutput.Text = txtOutput.Text & "Client closed connection." & vbCrLf
txtOutput.SelStart = Len(txtOutput.Text)
Call tcpServer.Listen ' listen for next connection
End Sub

Private Sub tcpServer_ConnectionRequest(ByVal requestID As Long)

' Ensure that tcpServer is closed
' before accepting a new connection

If tcpServer.State <> sckClosed Then
Call tcpServer.Close
End If

Call tcpServer.Accept(requestID) ' Accept connection
' Display following message on Server Status window
txtOutput.Text = "Client from IP Address: " & _
tcpServer.RemoteHostIP & " is successful" & vbCrLf & _
"Port #: " & tcpServer.RemotePort & vbCrLf
End Sub

Private Sub tcpServer_DataArrival(ByVal bytesTotal As Long)
Dim messageFromClient As String ' The button Client clicked
Dim numericValue As Integer ' The numeric value of string

Call tcpServer.GetData(messageFromClient) ' Get Client's button click
' Convert Client's button click value to a numerica value
numericValue = Val(messageFromClient)
If numericValue = 1 Then

If MSComm1.InBufferCount > 10 Then
data = MSComm1.Input
End If

Call tcpServer.SendData(data)
' Set acknowledgement message back to client
End If

End Sub

Private Sub tcpServer_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)
Dim result As Integer
result = MsgBox(Source & ": " & Description, _
vbOKOnly, "TCP/IP Error")
End
End Sub