vb6 - 将消息发送到winsock服务器聊天

时间:2013-06-18 12:28:11

标签: vb6 winsock

我有一个简单的winsock服务器聊天,这是代码:

Private Sub Form_Load()
Winsock1.LocalPort = 5100
Winsock1.Listen
End Sub

Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
Winsock1.Close
Winsock1.Accept requestID
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim sData As String
Winsock1.GetData sData
Text1.Text = Text1.Text & sData
End Sub

我从ios应用程序收到消息,但我无法发送带有“sendData”的消息。

如何向所有客户发送消息?我需要使用客户端吗?

感谢。

1 个答案:

答案 0 :(得分:1)

如果您想向多个客户端发送消息,那么最好的方法是关闭您的监听winsock1,并使用它来接受请求,以创建一个接受请求的新winsock控件。这样,您就可以接受来自多个来源的连接。

示例:

第1次将winsock1的属性索引更改为0,以创建控件数组。现在所有事件的签名都会更改为包含Index参数。

Dim NumSockets As Integer

Private Sub Form_Load()
    Winsock1(0).LocalPort = 5100
    Winsock1(0).Listen
End Sub

Private Sub Winsock1_Close(Index As Integer)
    Winsock1(Index).Close
End Sub

Private Sub Winsock1_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    NumSockets = NumSockets + 1
    Load Winsock1(NumSockets)                'create a new winsock control
    Winsock1(NumSockets).Accept requestID    'use that one to accept the request
End Sub

Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    Dim vtData As String
    Winsock1(Index).GetData vtData, vbString
    Print vtData
End Sub