这段代码能实现基本的socket通讯吗?帮朋友问的(表怪偶不懂提问的艺术Orz)解决方法
这段代码能实现基本的socket通讯吗?帮朋友问的(表怪偶不懂提问的艺术Orz)
数据接收程序
Option Explicit
Private Const winsk_local_port = 8888
Private Const max_sessions = 10
Private Sub Form_Load()
Dim i As Integer
With Winsk(0)
.Protocol = sckTCPProtocol
.LocalPort = winsk_local_port
.Listen
End With
For i = 1 To max_sessions
Load Winsk(i)
Winsk(i).Protocol = sckTCPProtocol
Winsk(i).LocalPort = winsk_local_port
Next
End Sub
Private Sub Winsk_ConnectionRequest(Index As Integer, ByVal requestID As Long)
Dim str As String
Dim i As Integer
MsgBox "aaa "
If Index = 0 Then
For i = 0 To max_sessions
With Winsk(i)
If .State = sckClosed Then
.Accept requestID
Exit For
End If
End With
Next
End If
End Sub
Private Sub Winsk_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim str As String
Dim rc As Integer
Dim i As Integer
MsgBox "收到! "
Winsk(Index).GetData str, , bytesTotal
messages.AddItem str
For i = 1 To max_sessions
With Winsk(i)
If .State = sckConnected Then
.SendData str
DoEvents
End If
End With
Next
End Sub
------解决方案--------------------
这是单方向。
------解决方案--------------------
这个是服务器端 的代码 可以允许10个连接饿服务器代码 缺少客户端的
数据接收程序
Option Explicit
Private Const winsk_local_port = 8888
Private Const max_sessions = 10
Private Sub Form_Load()
Dim i As Integer
With Winsk(0)
.Protocol = sckTCPProtocol
.LocalPort = winsk_local_port
.Listen
End With
For i = 1 To max_sessions
Load Winsk(i)
Winsk(i).Protocol = sckTCPProtocol
Winsk(i).LocalPort = winsk_local_port
Next
End Sub
Private Sub Winsk_ConnectionRequest(Index As Integer, ByVal requestID As Long)
Dim str As String
Dim i As Integer
MsgBox "aaa "
If Index = 0 Then
For i = 0 To max_sessions
With Winsk(i)
If .State = sckClosed Then
.Accept requestID
Exit For
End If
End With
Next
End If
End Sub
Private Sub Winsk_DataArrival(Index As Integer, ByVal bytesTotal As Long)
Dim str As String
Dim rc As Integer
Dim i As Integer
MsgBox "收到! "
Winsk(Index).GetData str, , bytesTotal
messages.AddItem str
For i = 1 To max_sessions
With Winsk(i)
If .State = sckConnected Then
.SendData str
DoEvents
End If
End With
Next
End Sub
------解决方案--------------------
这是单方向。
------解决方案--------------------
这个是服务器端 的代码 可以允许10个连接饿服务器代码 缺少客户端的