DOCUMENT:Q177699 05-DEC-1997 [vbwin] TITLE :HOWTO: Use NT Simple TCP/IP Services for Winsock Testing PRODUCT :Microsoft Visual Basic for Windows PROD/VER:WINDOWS:5.0,95; WINNT:3.51,4.0 OPER/SYS:WINDOWS winnt KEYWORDS:vb5all vb5howto ====================================================================== --------------------------------------------------------------------- The information in this article applies to: - Microsoft Visual Basic Professional and Enterprise Editions for Windows, version 5.0 - Microsoft Windows 95 - Microsoft Windows NT versions 3.51, 4.0 - Microsoft Win32 Software Development Kit (SDK) --------------------------------------------------------------------- SUMMARY ======= Windows NT has a Simple TCP/IP Services service that can be used to test applications that use the Winsock API and/or the Winsock Control. Rather than having to create both a client and server in Visual Basic, you can test the client against this service. A list of well-known ports available through this service can be found on an NT machine in the text document C:\WINNT\SYSTEM32\DRIVERS\ETC\SERVICES. MORE INFORMATION ================ The following sample describes how to use the Microsoft Winsock Control that shipped with Visual Basic 5.0 to call the Echo service and the Quote Service. Verify that Simple TCP/IP Services is Running --------------------------------------------- Check to see if you can contact the server you are trying to attach to by running Ping at the command prompt. If the server responds, go to the control panel on the server and start the Services icon. Check the list that comes up for Simple TCP/IP Services. The Simple TCP/IP Services service must be running for these samples to work correctly. If Simple TCP/IP Services is not listed, start the Network icon in the Control Panel. In NT 3.51, click the Add Software button, choose TCP/IP Protocol and related components, and click OK. A dialog will appear; choose Simple TCP/IP Services and click OK. In NT 4.0, click the Add button, choose Simple TCP/IP Services and click OK. The install program may ask you to provide the path to the NT installation CD-ROM and will require that you restart the computer when it is finished. Using the Echo Service ---------------------- 1. Create a new project in Visual Basic 5.0. 2. From the Projects menu, choose Components. Make sure that Microsoft Winsock Control 5.0 is checked and click OK. 3. Add a Winsock Control to the form and leave it named Winsock1. 4. Add three CommandButtons named cmdConnect, cmdEcho, and cmdDisconnect, and then change the captions to Connect, Echo and Disconnect, respectively. 5. Add two text boxes and leave them named Text1 and Text2, but change the MultiLine property on each to True. 6. Add the following code to the form: Option Explicit Const EchoPort = 7 Private Sub cmdConnect_Click() Dim temp As String temp = InputBox$("Enter a server name...", _ "Connect to the Echo Service", Winsock1.RemoteHost) If temp <> "" Then If Winsock1.State <> sckClosed Then Winsock1.Close Winsock1.RemoteHost = temp Winsock1.RemotePort = EchoPort Winsock1.LocalPort = 0 Winsock1.Connect End If End Sub Private Sub cmdDisconnect_Click() If Winsock1.State <> sckClosed Then Winsock1.Close cmdConnect.Enabled = True cmdDisconnect.Enabled = False cmdConnect.SetFocus End Sub Private Sub cmdEcho_Click() Winsock1.SendData Text1.Text cmdEcho.Enabled = False End Sub Private Sub Winsock1_Close() If Winsock1.State <> 0 Then Winsock1.Close End Sub Private Sub Winsock1_Connect() cmdConnect.Enabled = False cmdEcho.Enabled = True cmdDisconnect.Enabled = True End Sub Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long) Dim temp As String temp = String(bytesTotal, "*") Winsock1.GetData temp, vbString, bytesTotal Text2.Text = temp cmdEcho.Enabled = True End Sub Private Sub Winsock1_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) MsgBox "Error: " & Number & vbTab & Description, vbOKOnly, _ "Winsock Control 1 Error" CancelDisplay = True End Sub Using the Quote and Time Services --------------------------------- 1. Create a new project in Visual Basic 5.0. 2. From the Projects menu, choose Components. Make sure that Microsoft Winsock Control 5.0 is checked and then click OK. 3. Add a Winsock Control to the form and leave it named Winsock1. 4. Add two CommandButtons named cmdQuote and cmdTime, then change the captions to Quote and Time, respectively. 5. Add a text box and leave it named Text1, but change the MultiLine property on it to True. 6. Add the following code to the form: Const TimePort = 13, QuotePort = 17 Private Sub cmdQuote_Click() Dim temp As String temp = InputBox$("Enter a server name...", _ "Get a Quote", Winsock1.RemoteHost) If temp <> "" Then If Winsock1.State <> sckClosed Then Winsock1.Close Winsock1.RemoteHost = temp Winsock1.RemotePort = QuotePort Winsock1.LocalPort = 0 Winsock1.Connect End If End Sub Private Sub cmdTime_Click() Dim temp As String temp = InputBox$("Enter a server name...", _ "Get the time", Winsock1.RemoteHost) If temp <> "" Then If Winsock1.State <> sckClosed Then Winsock1.Close Winsock1.RemoteHost = temp Winsock1.RemotePort = TimePort Winsock1.LocalPort = 0 Winsock1.Connect End If End Sub Private Sub Winsock1_Close() If Winsock1.State <> 0 Then Winsock1.Close End Sub Private Sub Winsock1_Connect() Beep End Sub Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long) Dim temp As String temp = String(bytesTotal, " ") Winsock1.GetData temp, vbString, bytesTotal Text1.Text = temp End Sub Private Sub Winsock1_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) MsgBox "Error: " & Number & vbTab & Description, _ vbOKOnly, "Winsock Control 2 Error" CancelDisplay = True End Sub ====================================================================== Keywords : vb5all vb5howto Version : WINDOWS:5.0,95; WINNT:3.51,4.0 Platform : WINDOWS winnt Issue type : kbhowto ============================================================================= THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY. Copyright Microsoft Corporation 1997.