VERSION 5.00 Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX" Begin VB.Form frmTCPClient BackColor = &H0000FFFF& Caption = "Simple TCP Client" ClientHeight = 3150 ClientLeft = 60 ClientTop = 345 ClientWidth = 6990 LinkTopic = "Form1" ScaleHeight = 3150 ScaleWidth = 6990 StartUpPosition = 3 'Windows Default Begin VB.CommandButton cmdSend Caption = "Send Text" Height = 495 Left = 5520 TabIndex = 2 Top = 120 Width = 1335 End Begin VB.TextBox txtOutput Height = 1815 Left = 120 MultiLine = -1 'True TabIndex = 1 Top = 720 Width = 6735 End Begin VB.TextBox txtSend Height = 495 Left = 120 TabIndex = 0 Top = 120 Width = 5295 End Begin MSWinsockLib.Winsock tcpClient Left = 240 Top = 2640 _ExtentX = 741 _ExtentY = 741 _Version = 393216 End End Attribute VB_Name = "frmTCPClient" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False ' 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