'frmServer.frm - added tcpserver_close event.
Option Explicit
Private Sub Form_Load()
' Set the LocalPort property to an integer.
' Then invoke the Listen method.
tcpserver.LocalPort = 8000
tcpserver.Listen
'frmClient.Show ' Show the client form.? I thought this was the server project??
End Sub
'Just a little addition to listen again when the client closes.
Private Sub tcpserver_Close()
'When the client closes, clear the text
txtOutput.Text = ""
If tcpserver.State <> sckClosed Then
tcpserver.Close
DoEvents
End If
'Listen again
tcpserver.Listen
End Sub
Private Sub tcpserver_ConnectionRequest(ByVal requestID As Long)
' Check if the control's State is closed. If not,
' close the connection before accepting the new
' connection.
If tcpserver.State <> sckClosed Then
tcpserver.Close
End If
' Accept the request with the requestID
' parameter.
tcpserver.Accept requestID
End Sub
Private Sub tcpserver_DataArrival(ByVal bytesTotal As Long)
' Declare a variable for the incoming data.
' Invoke the GetData method and set the Text
' property of a TextBox named txtOutput to
' the data.
Dim strData As String
tcpserver.GetData strData
txtOutput.Text = txtOutput.Text & strData & vbCrLf
End Sub
Private Sub txtSendData_Change()
' The TextBox control named txtSendData
' contains the data to be sent. Whenever the user
' types into the textbox, the string is sent
' using the SendData method.
'Only send if we're connected
If tcpserver.State = sckConnected Then
tcpserver.SendData txtSendData.Text
End If
End Sub
'frmClient.frm - added a button named cmdClose.
Option Explicit
Private Sub Form_Load()
'For testing, use the local host.
tcpClient.RemoteHost = "127.0.0.1"
tcpClient.RemotePort = 8000
End Sub
Private Sub cmdConnect_Click()
tcpClient.Connect
End Sub
'Just a little addition to close the connection.
Private Sub cmdClose_Click()
If tcpClient.State <> sckClosed Then
'Clear output.
txtOutput.Text = ""
'Close
tcpClient.Close
DoEvents
End If
End Sub
Private Sub txtSend_Change()
'Only attempt to send if we're connected.
If tcpClient.State = sckConnected Then
tcpClient.SendData txtSend.Text
End If
End Sub
Private Sub tcpClient_DataArrival _
(ByVal bytesTotal As Long)
Dim strData As String
tcpClient.GetData strData
txtOutput.Text = txtOutput.Text & vbCrLf & strData
End Sub