这是一个控件版的,用法比 cls 类的简单。,和winsock 几乎一样
用来代替 winsock.ocx 控件,有时候客户电脑阉割了这个控件
此时可以用这个源代码来实现网络连接的客户端,
这里有个简单的使用示例:
服务端
Option Explicit
Private Sub cmdListen_Click()
Socketsvr.CloseSck
Socketsvr.LocalPort = txtSvrPort
Socketsvr.Listen
cmdListen.Enabled = False
cmdSVRDiscon.Enabled = True
End Sub
Private Sub cmdSever_Click()
Socketsvr.SendData Socketsvr.LocalIP & ":" & Socketsvr.LocalPort & "说:" & txtSeversend.Text
End Sub
Private Sub cmdSVRDiscon_Click()
Socketsvr.CloseSck
cmdSVRDiscon.Enabled = False
cmdListen.Enabled = True
cmdSever.Enabled = False
End Sub
Private Sub Form_Load()
Form2.Show
End Sub
Private Sub Form_Unload(Cancel As Integer)
Unload Form2
End Sub
Private Sub Socketsvr_CloseSck()
cmdSever.Enabled = False
MsgBox "连接是被 客户机 关闭的", , "我是服务器"
Socketsvr.CloseSck
Socketsvr.Listen
End Sub
Private Sub Socketsvr_ConnectionRequest(ByVal requestID As Long)
If Socketsvr.State <> sckConnected Then
Socketsvr.CloseSck
Socketsvr.Accept requestID
cmdSever.Enabled = True
End If
End Sub
'Download by http://www.NewXing.com
Private Sub Socketsvr_DataArrival(ByVal bytesTotal As Long)
Dim tmp As String
Socketsvr.GetData tmp
txtSeverget.Text = IIf(txtSeverget.Text = "", tmp, txtSeverget.Text & vbCrLf & tmp)
End Sub
Private Sub Socketsvr_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 "发生错误:" & vbCrLf & Description, , "我是服务器"
Socketsvr.CloseSck
Socketsvr.Listen
End Sub
Private Sub txtSeverget_Change()
txtSeverget.SelStart = Len(txtSeverget.Text)
End Sub
客户端
Option Explicit
'Download by http://www.NewXing.com
Private Sub cmdCLIDiscon_Click()
Socketclient.CloseSck
cmdconnect.Enabled = True
cmdCLIDiscon.Enabled = False
cmdClient.Enabled = False
End Sub
Private Sub cmdClient_Click()
Socketclient.SendData Socketclient.LocalIP & ":" & Socketclient.LocalPort & "说:" & txtClientsend.Text
End Sub
Private Sub cmdconnect_Click()
Socketclient.CloseSck
Socketclient.Connect txtRemotIP.Text, txtRemotPort
cmdconnect.Enabled = False
End Sub
Private Sub Form_Load()
txtRemotIP.Text = Socketclient.LocalIP
End Sub
Private Sub Form_Unload(Cancel As Integer)
Unload Form1
End Sub
Private Sub Socketclient_CloseSck()
cmdClient.Enabled = False
cmdCLIDiscon.Enabled = False
cmdconnect.Enabled = True
MsgBox "连接是被 服务器 关闭的", , "我是客户机"
End Sub
Private Sub Socketclient_Connect()
cmdClient.Enabled = True
cmdCLIDiscon.Enabled = True
cmdconnect.Enabled = False
Me.Caption = Socketclient.RemoteHost
End Sub
Private Sub Socketclient_DataArrival(ByVal bytesTotal As Long)
Dim tmp As String
Socketclient.GetData tmp
txtClientget.Text = IIf(txtClientget.Text = "", tmp, txtClientget.Text & vbCrLf & tmp)
End Sub
Private Sub Socketclient_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)
Socketclient.CloseSck
cmdconnect.Enabled = True
MsgBox "发生错误:" & vbCrLf & Description, , "我是客户机"
End Sub
Private Sub txtClientget_Change()
txtClientget.SelStart = Len(txtClientget.Text)
End Sub
下载完整工程
Views: 120
One Comment