Public Sub Main() Dim strIP As String Dim intPort As Integer Dim strSend As String Dim strReceive As String strIP = InputBox("请输入远程主机的IP地址:") intPort = InputBox("请输入远程主机的端口号:") ' 创建一个新的TCP客户端对象 Dim objClient As New Winsock ' 连接远程主机 objClient.Connect(strIP, intPort) Do Until objClient.State = 7 ' 等待与远程主机建立连接 DoEvents ' 释放CPU时间片,允许其他程序执行 Sleep 500 ' 等待500毫秒 If objClient.State = 7 Then ' 如果已经成功连接到远程主机,则跳出循环 Exit Do End If Loop If objClient.State <> 7 Then ' 如果仍然没有建立TCP/IP连接,则弹出对话框并退出子程序 MsgBox "无法连接远程主机!" & vbCrLf & "请重新再试", _ vbExclamation, "Error" Exit Sub End If strSend = InputBox("请输入要发送到远程主机的字符:") objClient.SendData strSend ' 向远程主机发送信息 Do Until objClient.State = 9 ' 检测数据是否已发送成功 Sleep 300 ' 等待300ms再尝试一次发送数据信号 If objClient.State = 9 Then ' 如果已成功发送出数据信号,则跳出循环 Exit Do End If Loop Do Until Not IsEmpty(objClient.GetData) ' 检测是否已从对方处得到数据信号 Sleep 500 ' 等待500ms再尝试一遍 If Not IsEmpty(objClient.GetData) Then Exit Do End If Loop strReceive = objClient.GetData MsgBox "你从" & _ strIP & ":" & _ intPort & _ "处得到回复如下:" & vbCrLf & vbCrLf & _ strReceive, , _ "Success" End Sub