' FILE: tcpServer ' DATE: 01/15/00 12:30 ' AUTH: P.Oh ' DESC: A Simple TCP Server ' REFS: Deitel p. 826 Fig. 19.10 Option Explicit Private Sub cmdSend_Click() ' Send following text data to the client Call tcpServer.SendData("Server >>> " & txtSend.Text) ' Repeat text data in server's txtOutput.Text window txtOutput.Text = txtOutput.Text & "Server >>>" & txtSend.Text & _ vbCrLf & vbCrLf ' Clear the txtSend.Text window" txtSend.Text = "" txtOutput.SelStart = Len(txtOutput.Text) End Sub Private Sub Form_Load() cmdSend.Enabled = False ' Set up local port and wait for connection tcpServer.LocalPort = 5000 Call tcpServer.Listen 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 tcpServer.Close End Sub Private Sub tcpServer_Close() cmdSend.Enabled = False Call tcpServer.Close ' client closed, server should too txtOutput.Text = txtOutput.Text & "Client closed connection." & vbCrLf & 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 cmdSend.Enabled = True Call tcpServer.Accept(requestID) ' accept connection ' Display following message on server application: txtOutput.Text = "The connection from IP Address: " & _ tcpServer.RemoteHostIP & " is successful" & vbCrLf & _ "Port #: " & tcpServer.RemotePort & vbCrLf & vbCrLf End Sub Private Sub tcpServer_DataArrival(ByVal bytesTotal As Long) Dim message As String Call tcpServer.GetData(message) ' get data from client txtOutput.Text = txtOutput.Text & message & vbCrLf & vbCrLf txtOutput.SelStart = Len(txtOutput.Text) 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