winsoce简单例子
作者:admin 日期:2008-05-16
'服务器端
Option Explicit
Private Sub Command1_Click()
If Winsock1.State <> sckClosed Then Winsock1.Close '在当前套接字未关闭时将其关闭
Winsock1.Bind '将设置值生效
Winsock1.Listen '监听端口的连接请求
MsgBox Winsock1.State
End Sub
Private Sub Form_Load()
Me.Caption = "服务器"
Command1.Caption = "连接客户端"
Text1.Text = "接收"
Text2.Text = "发送"
frmclient.Show
Winsock1.LocalPort = "900"
End Sub
Private Sub Text2_Change()
If Winsock1.State = sckConnected Then Winsock1.SendData Text2.Text '发送到客户端的内容
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
If Winsock1.State <> sckClosed Then Winsock1.Close
Winsock1.Accept requestID '在监听的端口有连接请求发生时,立即调用Accept方法,认可连接,使之生效。
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
Winsock1.GetData strData, vbString '当有数据到达时,调用GetData方法接收之
Text1.Text = strData '将接收的内容更新显示在Text1中
End Sub
'客户端
Option Explicit
Private Sub Command1_Click()
If Winsock1.State <> sckClosed Then Winsock1.Close '如果套接字未关闭,先关闭之
Winsock1.RemoteHost = "localhost" '确定服务器的主机名
'Winsock1.Bind '绑定套接字属性.不用此句,程序即可执行,用了反而执行不了。
Winsock1.Connect '向服务器发出连接请求
Command1.Enabled = False '使Command1不可用
Command2.Enabled = True '现在可以使其断开
End Sub
Private Sub Command2_Click()
Command1.Enabled = True
Command2.Enabled = False
Winsock1.Close '关闭当前套接字
End Sub
Private Sub Form_Load()
Me.Caption = "frmclient"
Me.Command1.Caption = "连接服务器"
Me.Command2.Caption = "断开"
Text1.Text = "接收"
Text2.Text = "发送"
Winsock1.RemotePort = "900"
End Sub
Private Sub Text2_Change()
'实时将Text3的内容反映到服务器的信息框
If Winsock1.State = sckConnected Then Winsock1.SendData Text2.Text
End Sub
Private Sub Winsock1_Connect()
MsgBox "connected to server!"
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim str As String
Winsock1.GetData str, vbString '按照字符串类型接收信息
Text1.Text = str '立即更新到Text2中
End Sub
Option Explicit
Private Sub Command1_Click()
If Winsock1.State <> sckClosed Then Winsock1.Close '在当前套接字未关闭时将其关闭
Winsock1.Bind '将设置值生效
Winsock1.Listen '监听端口的连接请求
MsgBox Winsock1.State
End Sub
Private Sub Form_Load()
Me.Caption = "服务器"
Command1.Caption = "连接客户端"
Text1.Text = "接收"
Text2.Text = "发送"
frmclient.Show
Winsock1.LocalPort = "900"
End Sub
Private Sub Text2_Change()
If Winsock1.State = sckConnected Then Winsock1.SendData Text2.Text '发送到客户端的内容
End Sub
Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
If Winsock1.State <> sckClosed Then Winsock1.Close
Winsock1.Accept requestID '在监听的端口有连接请求发生时,立即调用Accept方法,认可连接,使之生效。
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim strData As String
Winsock1.GetData strData, vbString '当有数据到达时,调用GetData方法接收之
Text1.Text = strData '将接收的内容更新显示在Text1中
End Sub
'客户端
Option Explicit
Private Sub Command1_Click()
If Winsock1.State <> sckClosed Then Winsock1.Close '如果套接字未关闭,先关闭之
Winsock1.RemoteHost = "localhost" '确定服务器的主机名
'Winsock1.Bind '绑定套接字属性.不用此句,程序即可执行,用了反而执行不了。
Winsock1.Connect '向服务器发出连接请求
Command1.Enabled = False '使Command1不可用
Command2.Enabled = True '现在可以使其断开
End Sub
Private Sub Command2_Click()
Command1.Enabled = True
Command2.Enabled = False
Winsock1.Close '关闭当前套接字
End Sub
Private Sub Form_Load()
Me.Caption = "frmclient"
Me.Command1.Caption = "连接服务器"
Me.Command2.Caption = "断开"
Text1.Text = "接收"
Text2.Text = "发送"
Winsock1.RemotePort = "900"
End Sub
Private Sub Text2_Change()
'实时将Text3的内容反映到服务器的信息框
If Winsock1.State = sckConnected Then Winsock1.SendData Text2.Text
End Sub
Private Sub Winsock1_Connect()
MsgBox "connected to server!"
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim str As String
Winsock1.GetData str, vbString '按照字符串类型接收信息
Text1.Text = str '立即更新到Text2中
End Sub
评论: 0 | 引用: 0 | 查看次数: 1520
发表评论
你没有权限发表留言!