Toon posts:

[Visual Basic] Meerdere connecties met Winsock

Pagina: 1
Acties:

Verwijderd

Topicstarter
Ik ben nu met een 2de stap bezig op winsock gebied. In de eerste stap heb ik het voor mekaar gekregen om 1 verbinding te verkrijgen met een client, en van beide kanten data te ontvangen en te versturen.

Nu wil ik dit gaan proberen met meerdere connecties en ben begonnen met een voorbeeld wat ik op het web vond. sckConn is een winsock component wat ik erbij heb geplaatst zodat sckConn data kan gaan sturen en Winsock fungeert als erm.. listen ding :+

Nu wordt dus telkens bij een nieuwe vraag naar een connectie de index van de Winsock control verhoogd zodat elke gebruiker zijn eigen control krijgt. Nu wil ik ook data gaan versturen vandaar het stukje Private Sub sckConn_DataArrival, maar hoe weet ik nu welke index ik moet meegeven zodat de data bij de juiste connectie komt? Kan ik zomaar een willekeurige socket aanmaken die gaat versturen?
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
Option Explicit
'*|*|*|*|*|*|*|*|*|*| MultiConnect |*|*|*|*|*|*|*|*|*|*|*
'*|*|*|*|*|*|*|*|*|**|*|  Notes  |*|*|*|*|*|*|*|*|*|*|*|*
'The Winsock control on the Server form is in an array
'The reason for the array is because Winsock does not
'support multi-connections alone (simply at least)
'So what we have to do instead is load a new winsock control
'Winsock(0) will always be listening for someone wanting to connect
'while Winsock(x) will establish the actual connection
'You can have an infinite number of connections

Dim intConnection As Integer

Private Sub Form_Load()
    'centers form
    Left = (Screen.Width - Me.Width) \ 2
    Top = (Screen.Height - Me.Height) \ 2
    
    'displays the connection status
    lblConnected.Caption = "0"

    'winsock will listen on port 55
    Winsock(0).LocalPort = 6000
    Winsock(0).Listen
End Sub

Private Sub sckConn_DataArrival(Index As Integer, ByVal bytesTotal As Long)
    Dim strData As String
    sckConn().GetData strData, vbString, bytesTotal
    strData = "stuur dit terug" & Chr(0)
    sckConn().SendData strData
End Sub

Private Sub Winsock_Close(Index As Integer)
    lblConnected.Caption = lblConnected.Caption - 1 'takes one off the connection status
End Sub

Private Sub Winsock_ConnectionRequest(Index As Integer, ByVal requestID As Long)
    'adds 1 to the intConnection
    'to keep winsock(0) listening on port 55
    intConnection = intConnection + 1
    outConnection = outConnection + 1
    
    'loads a NEW winsock with an array > 0
    Load Winsock(intConnection)
    Load sckConn(outConnection)
    'lets winsock connect to client
    Winsock(intConnection).Accept requestID
    sckConn(outConnection).Accept requestID
    'shows how many computers are connected
    lblConnected.Caption = lblConnected.Caption + 1
End Sub

Verwijderd

Topicstarter
Ik heb zojuist de beste informatie sinds dagen gevonden.

http://www.vbcity.com/forums/topic.asp?tid=865&highlight=winsock

Qua behulpzaamheid is dit forum 10++