VERSION 5.00 Object = "{248DD890-BB45-11CF-9ABC-0080C7E7B78D}#1.0#0"; "MSWINSCK.OCX" Begin VB.Form frmTCPServer BackColor = &H00FF0000& Caption = "Simple TCP Server" ClientHeight = 3585 ClientLeft = 60 ClientTop = 345 ClientWidth = 7725 LinkTopic = "Form1" ScaleHeight = 3585 ScaleWidth = 7725 StartUpPosition = 3 'Windows Default Begin VB.CommandButton cmdSend Caption = "Send Text" Height = 375 Left = 5880 TabIndex = 2 Top = 480 Width = 1155 End Begin VB.TextBox txtOutput Height = 1935 Left = 240 MultiLine = -1 'True TabIndex = 1 Top = 960 Width = 6855 End Begin VB.TextBox txtSend Height = 375 Left = 240 TabIndex = 0 Top = 480 Width = 5535 End Begin MSWinsockLib.Winsock tcpServer Left = 120 Top = 3000 _ExtentX = 741 _ExtentY = 741 _Version = 393216 End End Attribute VB_Name = "frmTCPServer" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False ' 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