📄 frmtcp.frm
字号:
sockServer(0).Close
cmdListen.Enabled = True
cmdCloseListen.Enabled = False
Set itemx = lstStates.ListItems.Item(2)
itemx.SubItems(2) = "-1"
End Sub
Private Sub cmdConnect_Click()
' Have the client control attempt to connect to the
' specified server on the given port number.
'
sockClient.LocalPort = 0
sockClient.RemoteHost = txtServerName.Text
sockClient.RemotePort = CInt(txtPort.Text)
sockClient.Connect
cmdConnect.Enabled = False
End Sub
Private Sub cmdDisconnect_Click()
Dim itemx As Object
' Close the client's connection, setup the command
' buttons for subsequent connections.
'
sockClient.Close
cmdConnect.Enabled = True
cmdSendData.Enabled = False
cmdDisconnect.Enabled = False
' Set the port number to -1 to indicate no connection
'
Set itemx = lstStates.ListItems.Item(1)
itemx.SubItems(2) = "-1"
End Sub
Private Sub cmdExit_Click()
Unload Me
End Sub
Private Sub cmdListen_Click()
Dim itemx As Object
' Put the server control into listening mode on the given
' port number.
'
sockServer(0).LocalPort = CInt(txtServerPort.Text)
sockServer(0).Listen
Set itemx = lstStates.ListItems.Item(2)
itemx.SubItems(2) = sockServer(0).LocalPort
cmdCloseListen.Enabled = True
cmdListen.Enabled = False
End Sub
Private Sub cmdSendData_Click()
' If we're connected send the given data to the server.
'
If (sockClient.State = sckConnected) Then
sockClient.SendData txtSendData.Text
Else
MsgBox "Unexpected error! Connection closed"
Call cmdDisconnect_Click
End If
End Sub
Private Sub Form_Load()
Dim itemx As Object
lblLocalHostname.Caption = sockServer(0).LocalHostName
lblLocalHostIP.Caption = sockServer(0).LocalIP
' Initialize the protocol property to TCP since thats
' all we'll be using.
'
ServerIndex = 0
sockServer(0).Protocol = sckTCPProtocol
sockClient.Protocol = sckTCPProtocol
' Setup the buttons
'
cmdDisconnect.Enabled = False
cmdSendData.Enabled = False
cmdCloseListen.Enabled = False
' Initialize the listview control that contains the
' current state of all Winsock controls created (not
' necessarily connected or being used though).
'
Set itemx = lstStates.ListItems.Add(1, , "Local Client")
itemx.SubItems(1) = "sckClosed"
itemx.SubItems(2) = "-1"
Set itemx = lstStates.ListItems.Add(2, , "Local Server")
itemx.SubItems(1) = "sckClosed"
itemx.SubItems(2) = "-1"
' Initialize the timer which controls the rate of refresh
' on the above socket states.
'
Timer1.Interval = 500
Timer1.Enabled = True
End Sub
Private Sub sockClient_Close()
sockClient.Close
End Sub
Private Sub sockClient_Connect()
Dim itemx As Object
' The connection was successful, enable the transfer data
' buttons.
cmdSendData.Enabled = True
cmdDisconnect.Enabled = True
Set itemx = lstStates.ListItems.Item(1)
itemx.SubItems(2) = sockClient.LocalPort
End Sub
Private Sub sockClient_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)
' An error occured on the Client control, print a message,
' and close the control. An error puts the state of the
' control to sckError which is cleared only when the Close
' method is called.
MsgBox Description
sockClient.Close
cmdConnect.Enabled = True
End Sub
Private Sub sockServer_Close(index As Integer)
Dim itemx As Object
' Close the given Winsock control
'
sockServer(index).Close
Set itemx = lstStates.ListItems.Item(index + 2)
lstStates.ListItems.Item(index + 2).Text = "---.---.---.---"
itemx.SubItems(2) = "-1"
End Sub
Private Sub sockServer_ConnectionRequest(index As Integer, _
ByVal requestID As Long)
Dim i As Long, place As Long, freeSock As Long, itemx As Object
' Search through the array to see if there is a closed
' control that we can re-use.
freeSock = 0
For i = 1 To ServerIndex
If sockServer(i).State = sckClosed Then
freeSock = i
Exit For
End If
Next i
' If freeSock is still zero there are no free controls
' so load a new one.
'
If freeSock = 0 Then
ServerIndex = ServerIndex + 1
Load sockServer(ServerIndex)
sockServer(ServerIndex).Accept requestID
place = ServerIndex
Else
sockServer(freeSock).Accept requestID
place = freeSock
End If
' If there were not free controls then we added one above
' so create an entry in the ListView control for the new
' control. In either case set the state of the new
' connection to sckConnected.
'
If freeSock = 0 Then
Set itemx = lstStates.ListItems.Add(, , _
sockServer(ServerIndex).RemoteHostIP)
Else
Set itemx = lstStates.ListItems.Item(freeSock + 2)
lstStates.ListItems.Item(freeSock + 2).Text = _
sockServer(freeSock).RemoteHostIP
End If
itemx.SubItems(2) = sockServer(place).RemotePort
End Sub
Private Sub sockServer_DataArrival(index As Integer, _
ByVal bytesTotal As Long)
Dim data As String, entry As String
' Allocate a sufficiently sized string buffer and get the
' data.
'
data = String(bytesTotal + 2, Chr$(0))
sockServer(index).GetData data, vbString, bytesTotal
' Append the clients IP address to the beginning of the
' message and add it to the list box.
'
entry = sockServer(index).RemoteHostIP & ": " & data
lstMessages.AddItem entry
End Sub
Private Sub sockServer_Error(index As Integer, _
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)
' Print the error message and close the specified control.
' An error puts the state of the control to sckError which
' is cleared only when the Close method is called.
MsgBox Description
sockServer(index).Close
End Sub
Private Sub Timer1_Timer()
Dim i As Long, index As Long, itemx As Object
' Set the state of the local client Winsock control
'
Set itemx = lstStates.ListItems.Item(1)
Select Case sockClient.State
Case sckClosed
itemx.SubItems(1) = "sckClosed"
Case sckOpen
itemx.SubItems(1) = "sckOpen"
Case sckListening
itemx.SubItems(1) = "sckListening"
Case sckConnectionPending
itemx.SubItems(1) = "sckConnectionPending"
Case sckResolvingHost
itemx.SubItems(1) = "sckResolvingHost"
Case sckHostResolved
itemx.SubItems(1) = "sckHostResolved"
Case sckConnecting
itemx.SubItems(1) = "sckConnecting"
Case sckConnected
itemx.SubItems(1) = "sckConnected"
Case sckClosing
itemx.SubItems(1) = "sckClosing"
Case sckError
itemx.SubItems(1) = "sckError"
Case Else
itemx.SubItems(1) = "unknown: " & sockClient.State
End Select
' Now set the states for the listening server control as
' well as any connected clients.
'
index = 0
For i = 2 To ServerIndex + 2
Set itemx = lstStates.ListItems.Item(i)
Select Case sockServer(index).State
Case sckClosed
itemx.SubItems(1) = "sckClosed"
Case sckOpen
itemx.SubItems(1) = "sckOpen"
Case sckListening
itemx.SubItems(1) = "sckListening"
Case sckConnectionPending
itemx.SubItems(1) = "sckConnectionPending"
Case sckResolvingHost
itemx.SubItems(1) = "sckResolvingHost"
Case sckHostResolved
itemx.SubItems(1) = "sckHostResolved"
Case sckConnecting
itemx.SubItems(1) = "sckConnecting"
Case sckConnected
itemx.SubItems(1) = "sckConnected"
Case sckClosing
itemx.SubItems(1) = "sckClosing"
Case sckError
itemx.SubItems(1) = "sckError"
Case Else
itemx.SubItems(1) = "unknown"
End Select
index = index + 1
Next i
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -