Tipp 0248 MultiUser-Chat
Autor/Einsender:
Datum:
  Kilian Meyer
16.06.2002
Entwicklungsumgebung:   VB 6
In diesem Beispiel wird gezeigt, wie es mittels des Winsock-Controls und einer Client-Server Lösung möglich ist, einen MultiUser-Chat mit fast unendlich vielen Teilnehmern, zu realisieren.
Dieses Beispiel besteht aus 2 Projekten, und dem Downloadprojekt liegt eine ausführliche Anleitung bei.
Code im Codebereich der Form des Projekts Client.vbp
 
Option Explicit

Dim intMax As Integer

Private Sub Form_Load()
  intMax = 1

  Winsock(0).LocalPort = 1001
  Winsock(0).Listen
End Sub

Private Sub Winsock_ConnectionRequest(Index As Integer, _
      ByVal requestID As Long)
  If Index = 0 Then
    intMax = intMax + 1
    Load Winsock(intMax)
    Winsock(intMax).LocalPort = 1001
    Winsock(intMax).Accept requestID
  End If
End Sub

Private Sub Winsock_DataArrival(Index As Integer, _
      ByVal bytesTotal As Long)

  Dim data As String
  Dim i As Integer

  Winsock(Index).GetData data

  For i = 0 To intMax
    If Winsock(i).State = 7 Then
      Winsock(i).SendData data
      DoEvents
    End If
  Next i

  Text1.Text = Text1.Text & data & vbCrLf
  Text1.SelLength = Len(Text1.Text)
End Sub
 
Code im Codebereich der Form des Projekts Client.vbp
 
Option Explicit

Dim connected As Boolean

Private Sub Form_Load()
  connected = False
End Sub

Private Sub Command1_Click()
  If connected = True Then
    Winsock1.Close
    Command1.Caption = "&Verbinden"
    Text2.Text = ""
    connected = False
  Else
    Winsock1.RemoteHost = Text1.Text
    Winsock1.RemotePort = 1001
    Winsock1.Connect
    Command1.Caption = "&Trennen"
    Screen.MousePointer = vbHourglass
  End If
End Sub

Private Sub Text3_KeyPress(KeyAscii As Integer)
  If KeyAscii = 13 Then
    If connected = True Then
      If Text1 <> "" Then
        Winsock1.SendData "<" & Text4.Text & ">: " & Text3.Text
        Text3.Text = ""
        KeyAscii = 0
      End If
    Else
      MsgBox "Noch keine Verbindung vorhanden!", _
            vbInformation, "Fehler"
    End If
  End If
End Sub

Private Sub Winsock1_Connect()
  connected = True
  Screen.MousePointer = vbNormal
  Text2.Text = Text2.Text & _
        "Verbindung aufgebaut!!!" & vbCrLf & vbCrLf
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
  Dim data As String

  Winsock1.GetData data

  Text2.Text = Text2.Text & data & vbCrLf
  Text2.SelLength = Len(Text2.Text)
End Sub

Private Sub Winsock1_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)

  Screen.MousePointer = vbNormal
  MsgBox "Fehler in der Verbindung:" & vbCrLf & vbCrLf & _
        Description, vbExclamation, "Verbindungsfehler"
  Winsock1.Close
  Command1.Caption = "&Verbinden"
  connected = False
End Sub
 
Hinweis
Um diesen Tipp ausführen zu können, muss das Microsoft Winsock Control als Komponente in das Projekt eingebunden werden.
Weitere Links zum Thema
Chat (DirectX 7)
Multi-Player Spiele mit Winsock realisieren

Windows-Version
95
98/SE
ME
NT
2000
XP
Vista
Win 7
VB-Version
VBA 5
VBA 6
VB 4/16
VB 4/32
VB 5
VB 6


Download  (8,1 kB) Downloads bisher: [ 6778 ]

Vorheriger Tipp Zum Seitenanfang Nächster Tipp

Startseite | Projekte | Tutorials | API-Referenz | VB-/VBA-Tipps | Komponenten | Bücherecke | VB/VBA-Forum | VB.Net-Forum | DirectX-Forum | Foren-Archiv | DirectX | VB.Net-Tipps | Chat | Spielplatz | Links | Suchen | Stichwortverzeichnis | Feedback | Impressum

Seite empfehlen Bug-Report
Letzte Aktualisierung: Dienstag, 30. August 2011