两个例子: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 <servername> 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, double-click Network in the Control Panel. In Windows NT 3.51, click Add Software, choose TCP/IP Protocol and related components, and click OK. In the dialog box that appears, click Simple TCP/IP Services and click OK. In Windows NT 4.0, click Add, choose Simple TCP/IP Services, and click OK. The install program may ask you to provide the path to the Windows NT installation CD-ROM and will require that you restart the computer when it is finished. Using the Echo Service
Create a new Standard EXE project in Visual Basic.
From the Projects menu, choose Components. Make sure that Microsoft Winsock Control is checked and click OK.
Add a Winsock Control to the form and leave it named Winsock1.
Add three CommandButtons named cmdConnect, cmdEcho, and cmdDisconnect, and then change the captions to Connect, Echo and Disconnect, respectively.
Add two text boxes and leave them named Text1 and Text2, but change the MultiLine property on each to True.
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
Create a new STANDARD EXE project in Visual Basic.
From the Projects menu, choose Components. Make sure that Microsoft Winsock Control is checked and then click OK.
Add a Winsock Control to the form and leave it named Winsock1.
Add two CommandButtons named cmdQuote and cmdTime, then change the captions to Quote and Time, respectively.
Add a text box and leave it named Text1, but change the MultiLine property on it to True.
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
Check to see if you can contact the server you are trying to attach to by running Ping <servername> 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, double-click Network in the Control Panel. In Windows NT 3.51, click Add Software, choose TCP/IP Protocol and related components, and click OK. In the dialog box that appears, click Simple TCP/IP Services and click OK. In Windows NT 4.0, click Add, choose Simple TCP/IP Services, and click OK. The install program may ask you to provide the path to the Windows NT installation CD-ROM and will require that you restart the computer when it is finished. Using the Echo Service
Create a new Standard EXE project in Visual Basic.
From the Projects menu, choose Components. Make sure that Microsoft Winsock Control is checked and click OK.
Add a Winsock Control to the form and leave it named Winsock1.
Add three CommandButtons named cmdConnect, cmdEcho, and cmdDisconnect, and then change the captions to Connect, Echo and Disconnect, respectively.
Add two text boxes and leave them named Text1 and Text2, but change the MultiLine property on each to True.
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
Create a new STANDARD EXE project in Visual Basic.
From the Projects menu, choose Components. Make sure that Microsoft Winsock Control is checked and then click OK.
Add a Winsock Control to the form and leave it named Winsock1.
Add two CommandButtons named cmdQuote and cmdTime, then change the captions to Quote and Time, respectively.
Add a text box and leave it named Text1, but change the MultiLine property on it to True.
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
解决方案 »
- vb WebBrowser1 互调用
- vb 实时错误453
- inet excecute 异步传输问题
- 各位 牛年牛气的 高手不好意思,又有问题要烦扰一下各位了(数组元素累计问题)
- 如何制做帮助文件呀?还有:帮助文件的内容是以什么格式存贮的?
- 我小外孙女刚出生,还没出医,求大家给取个好听名字~~~
- 怎样更改vb生成的可执行文件的图标?改完马上结贴!
- 提问!
- 不用adodc控件,直接给Datagrid的recordsource赋值为什么没反应?
- 提问的智慧 - 讨论如果提问,写得很好,对初学者的学习和提高大有裨益。
- 请问各位高手:我在网上载了两种TTS的发音,一种男声,一种女生,请问如何在VB中进行不同的调用切换???
- 各位大哥大虾,帮帮小弟的忙,怎么解决vb和intenet的问题,不要嫌分少啊!
没有人解释?