WinSock - Obousm∞rnost

Ji₧ d°φve jsme vytvo°ili dv∞ aplikace - jednu, kterß odesφlß zprßvy a druhou, kterß je p°ijφmß. Ale aplikace, kterß odesφlß data nem∙₧e zatφm data p°ijφmat a p°ijφmacφ aplikace nenφ schopna data odesφlat. A Φlov∞k zrovna nemusφ b²t Einstein aby v∞d∞l, ₧e sv∞tem cloumß oboustrannß komunikace. ICQ by rozhodn∞ nem∞lo mili≤ny Φlen∙, kdyby umo₧≥ovalo pouze zasφlßnφ zprßv jednφm sm∞rem.

╚ili: zalo₧te projekt, p°idejte odkaz na WinSock, na formulß° p°idejte WinSock (WinSock1), tlaΦφtko pro odesφlßnφ zprßv (cmdSendMessage) a t°i TextBoxy - txtAdress (Adresa), txtOutGoingMessage (Odesφlanß zprßva) a txtIncommingMessage. Pak zapiÜte tento k≤d:

Private Sub Form_Load()

   On Error GoTo PortErr

 Winsock1.LocalPort = 201
 Winsock1.Listen

' Pou₧φvßme port 201. Aby to pracovalo, nem∙₧ete ji₧ testovat tuto aplikaci
' na jednΘ a tΘ samΘ maÜin∞ - jinak dojde ke konfliktu p°i obsazovßnφ portu.

 Exit Sub
    
PortErr:

    MsgBox "Jinß aplikace pou₧φvß port 201. " & , vbCritical
        
    End

End Sub

Private Sub cmdSendMessage_Click()

    If Winsock1.State = sckConnected Then
        'Pokud jsme p°ipojenφ, poÜleme data
        Winsock1.SendData (txtOutgoingMessage.Text)
    Else
        If MsgBox("Nejste p°ipojeni. " & _
            "P°ejete si p°ipojit se k " & txtAddress.Text & " ?", _
            vbYesNo + vbQuestion) = vbYes Then
            'P°ipojenφ na vzdßlen² poΦφtaΦ
            Winsock1.Close
            Winsock1.RemotePort = 201
            Winsock1.RemoteHost = txtAddress.Text
            Winsock1.Connect

            '╚ekßnφ na p°ipojenφ
            Do Until Winsock1.State = sckConnected
                DoEvents: DoEvents: DoEvents: DoEvents
                If Winsock1.State = sckError Then
                    MsgBox "Chyba p°ipojenφ !"
                    Exit Sub
                End If
            Loop

            'Odeslßnφ dat
            Winsock1.SendData (txtOutgoingMessage)
        Else
            Call MsgBox("OK - nep°ipojeno !", vbInformation)
        End If
    End If
        
End Sub

Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)

    'Akceptovßnφ p°φchozφho po₧adavku na spojenφ
    
    If Winsock1.State <> sckClosed Then Winsock1.Close
    Winsock1.Accept requestID

End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    
    'Zobrazenφ doÜl²ch dat
    
    Dim strIncoming As String
    Winsock1.GetData strIncoming
    txtIncomingMessage.Text = strIncoming
    
End Sub

Zp∞t DalÜφ

Autor: The Bozena