FILE: tcpClient ' DATE: 01/15/00 12:45 ' AUTH: P.Oh ' DESC: A Simple TCP Client ' REFS: Deitel p. 829 Fig. 19.12 Option Explicit Private Sub cmdSend_Click() ' Send data to server Call tcpClient.SendData("Client >>> " & txtSend.Text) txtOutput.Text = txtOutput.Text & _ "Client >>> " & txtSend.Text & vbCrLf & vbCrLf txtOutput.SelStart = Len(txtOutput.Text) txtSend.Text = "" End Sub Private Sub Form_Load() cmdSend.Enabled = False ' Set up local port and wait for connection tcpClient.RemoteHost = InputBox("Enter the remote host IP Address", _ "IP Address", "localhost") If tcpClient.RemoteHost = "" Then tcpClient.RemoteHost = "localhost" End If tcpClient.RemotePort = 5000 ' server port Call tcpClient.Connect ' connect to RemoteHost address End Sub Private Sub Form_Resize() On Error Resume Next Call cmdSend.Move(ScaleWidth - cmdSend.Width, 0) Call txtSend.Move(0, 0, ScaleWidth - cmdSend.Width) Call txtOutput.Move(0, txtSend.Height, ScaleWidth, _ ScaleHeight - txtSend.Height) End Sub Private Sub Form_Terminate() Call tcpClient.Close End Sub Private Sub tcpClient_Close() cmdSend.Enabled = False Call tcpClient.Close ' server closed, client should too txtOutput.Text = txtOutput.Text & "Server closed connection." & vbCrLf txtOutput.SelStart = Len(txtOutput.Text) End Sub Private Sub tcpClient_Connect() ' When connection occurs, display a message cmdSend.Enabled = True txtOutput.Text = "Connected to IP address: " & _ tcpClient.RemoteHostIP & vbCrLf & vbCrLf End Sub Private Sub tcpClient_DataArrival(ByVal bytesTotal As Long) Dim message As String Call tcpClient.GetData(message) ' get data from server txtOutput.Text = txtOutput.Text & message & vbCrLf & vbCrLf txtOutput.SelStart = Len(txtOutput.Text) End Sub Private Sub tcpClient_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) ' If the client fails to connect to server, then this code executes Dim result As Integer result = MsgBox(Source & ": " & Description & vbCrLf & "Doh! Can't connect to server!", _ vbOKOnly, "TCP/IP Error") ' Source variable is the control (winsock in this case) causing the error ' Description variable cites the error message ' vbOKOnly is the control button ' TCP/IP Error is the MsgBox caption End End Sub