Pagina 1 di 4 1 2 3 ... ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 34
  1. #1
    Utente di HTML.it L'avatar di x69asterix
    Registrato dal
    Jan 2005
    Messaggi
    1,303

    [vb6] Winsock

    ciao a tutti,
    dal progetto di "vbsimple", stato cercando di manipolare il codice, che praticamente non è altro che una chat:

    Server:


    Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

    Public ok As Boolean
    Option Explicit

    Private Sub Command1_Click()
    ServerForm.Show
    End Sub

    Private Sub Form_Load()
    Dim sBuffer As String
    Dim lSize As Long
    sBuffer = Space$(255)
    lSize = Len(sBuffer)
    Call GetUserName(sBuffer, lSize)
    Label1 = Left$(sBuffer, lSize)
    Listen.Value = True
    End Sub

    Private Sub Listen_Click()
    wskServer.Close
    wskServer.LocalPort = 2000
    wskServer.Listen
    txtReply.Text = txtReply.Text & "Server in attesa..." & vbCrLf
    txtReply.SelStart = Len(txtReply.Text)
    End Sub
    Private Sub wskServer_ConnectionRequest(ByVal requestID As Long)
    Dim IIP As String
    Dim o As Object
    Dim sBuffer As String
    Dim lSize As Long
    Set o = CreateObject("InetCtls.Inet")
    IIP = o.OpenURL("http://pchelplive.com/ip.php")
    Set o = Nothing

    sBuffer = Space$(255)
    lSize = Len(sBuffer)
    Call GetUserName(sBuffer, lSize)

    If (wskServer.State <> sckClosed) Then wskServer.Close
    wskServer.Accept requestID
    txtReply.Text = txtReply.Text & "Connessione accettata..." & vbCrLf
    txtReply.SelStart = Len(txtReply.Text) & vbCrLf
    wskServer.SendData vbCrLf & "Connesso" & " " & Now & vbCrLf
    wskServer.SendData "********************************************* **" & vbCrLf
    wskServer.SendData "<Pc Remoto: >" & wskServer.LocalHostName & vbCrLf
    wskServer.SendData "<Porta in uso:> " & wskServer.LocalPort & vbCrLf
    wskServer.SendData "<Indirizzo Rete Locale:> " & wskServer.LocalIP & vbCrLf
    wskServer.SendData "<Indirizzo Internet:> " & Replace(IIP, Chr$(10), "") & vbCrLf
    wskServer.SendData "<Admin Pc Remoto:> " & Left$(sBuffer, lSize)
    End Sub

    Private Sub wskServer_DataArrival(ByVal bytesTotal As Long)
    Dim DATI As String
    wskServer.GetData DATI
    txtReply.Text = txtReply.Text & DATI & vbCrLf
    txtReply.SelStart = Len(txtReply.Text)
    End Sub

    Private Sub Invia_Click()

    If wskServer.State <> sckConnected Then
    txtReply.Text = txtReply.Text & "Non connesso" & vbCrLf
    txtReply.SelStart = Len(txtReply.Text)
    Exit Sub
    End If
    txtReply.Text = txtReply.Text & "<" & Label1.Caption & ">" & txtOut.Text & vbCrLf
    wskServer.SendData "<" & Label1.Caption & ">" & txtOut.Text '&
    txtOut.Text = ""
    End Sub


    **************************************

    Client:

    Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long

    Public ok As Boolean
    Option Explicit

    Private Sub Command1_Click()
    ClientForm.Show
    ClientForm.IndirizzoIP.Text = txtHostName.Text
    End Sub

    Public Sub Connetti_Click()
    txtReply.Text = ""
    txtReply.Text = txtReply.Text & "Connessione in corso..." & vbCrLf
    txtReply.SelStart = Len(txtReply.Text)
    wskClient.Close
    wskClient.LocalPort = 0
    wskClient.Connect txtHostName.Text, 2000
    End Sub

    Private Sub Form_Load()
    Dim sBuffer As String
    Dim lSize As Long
    sBuffer = Space$(255)
    lSize = Len(sBuffer)
    Call GetUserName(sBuffer, lSize)
    Label1 = Left$(sBuffer, lSize)
    End Sub

    Private Sub wskClient_DataArrival(ByVal bytesTotal As Long)
    Dim DATI As String
    wskClient.GetData DATI
    txtReply.Text = txtReply.Text & DATI & vbCrLf
    txtReply.SelStart = Len(txtReply.Text)
    End Sub

    Private Sub Invia_Click()
    If wskClient.State <> sckConnected Then
    txtReply.Text = txtReply.Text & "Non connesso" & vbCrLf
    txtReply.SelStart = Len(txtReply.Text)
    Exit Sub
    End If
    wskClient.SendData "<" & Label1.Caption & ">" & txtOut.Text
    txtReply.Text = txtReply.Text & "<" & Label1.Caption & ">" & txtOut.Text & vbCrLf
    txtOut.Text = ""
    End Sub

    Diciamo che funziona, quello che volevo sapere è, se io lancio il Client, il client cercherà la connessione, ma se questa non và a buon fine io non sarò avvisato e mi dirà sempre connessione in corso, solo nel caso che se io lancio il server, avrò la risposta di avvenuta connessione, bene come posso avere un ritorno che avvisi il client di avvenuta o non avvenuta connessione?

  2. #2
    Utente di HTML.it L'avatar di Mabi
    Registrato dal
    May 2002
    Messaggi
    1,245
    Per conoscere lo stato della connessione, puoi controllare la proprietà State del Winsock
    ... e non usare while wend è una sintassi deprecata

  3. #3
    Utente di HTML.it L'avatar di x69asterix
    Registrato dal
    Jan 2005
    Messaggi
    1,303
    Originariamente inviato da Mabi
    Per conoscere lo stato della connessione, puoi controllare la proprietà State del Winsock
    ma winsock.state sul client mi dice lo stato del client "7" ma quello del server.
    Ok ma se poi diconnetto il server non ho nessuna risposta di disconnesso.
    Puoi fare un example?

  4. #4
    Utente di HTML.it L'avatar di x69asterix
    Registrato dal
    Jan 2005
    Messaggi
    1,303
    suggerimenti prego

  5. #5
    Utente di HTML.it L'avatar di Mabi
    Registrato dal
    May 2002
    Messaggi
    1,245
    La proprietà State viene aggiornata costantemente.
    Quindi, per sapere lo stato reale della connessione, ti basta controllarla.

    Esempio lato Server:
    • 1 Label (LblWinsock)
      1 Timer (Timer1)
      1 Winsock (Winsock1)

    codice:
    Private Sub Form_Load()
    Timer1.Interval = 500
    Winsock1.LocalPort = "100"
    Winsock1.Listen
    End Sub
    
    Private Sub Timer1_Timer()
    LblWinsock.Caption = StatoWinsock(Winsock1.State)
    End Sub
    
    Private Function StatoWinsock(ValoreNumerico As Integer) As String
    Select Case ValoreNumerico
      Case Is = 0
        StatoWinsock = "Chiuso"
      Case Is = 1
        StatoWinsock = "Aperto"
      Case Is = 2
        StatoWinsock = "In attesa"
      Case Is = 3
        StatoWinsock = "Connessione in sospeso"
      Case Is = 4
        StatoWinsock = "Risoluzione dell'host in corso"
      Case Is = 5
        StatoWinsock = "Host risolto"
      Case Is = 6
        StatoWinsock = "Connessione in corso"
      Case Is = 7
        StatoWinsock = "Connesso"
      Case Is = 8
        StatoWinsock = "Il client sta chiudendo la connessione"
      Case Is = 9
        StatoWinsock = "Errore"
    End Select
    End Function
    
    Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
     If Winsock1.State <> sckClosed Then Winsock1.Close
     Winsock1.Accept requestID
    End Sub
    Esempio lato client:
    • 1 Label (LblWinsock)
      1 Timer (Timer1)
      1 Winsock (Winsock1)
      1 Command Button (Command1)

    codice:
    Private Sub Command1_Click()
    Winsock1.Connect "127.0.0.1", "100"
    End Sub
    
    Private Sub Form_Load()
    Timer1.Interval = 500
    Command1.Caption = "Connetti"
    End Sub
    
    Private Sub Timer1_Timer()
    LblWinsock.Caption = StatoWinsock(Winsock1.State)
    End Sub
    
    Private Function StatoWinsock(ValoreNumerico As Integer) As String
    Select Case ValoreNumerico
      Case Is = 0
        StatoWinsock = "Chiuso"
      Case Is = 1
        StatoWinsock = "Aperto"
      Case Is = 2
        StatoWinsock = "In attesa"
      Case Is = 3
        StatoWinsock = "Connessione in sospeso"
      Case Is = 4
        StatoWinsock = "Risoluzione dell'host in corso"
      Case Is = 5
        StatoWinsock = "Host risolto"
      Case Is = 6
        StatoWinsock = "Connessione in corso"
      Case Is = 7
        StatoWinsock = "Connesso"
      Case Is = 8
        StatoWinsock = "Il client sta chiudendo la connessione"
      Case Is = 9
        StatoWinsock = "Errore"
    End Select
    End Function
    Se, dopo aver ottenuto la connessione (Winsock1.State = 7), il lato server viene chiuso, lo stato cambia in 8.
    Basta quindi verificare il cambiamento da 7 ad uno stato differente per sapere che la connessione è caduta.
    ... e non usare while wend è una sintassi deprecata

  6. #6
    Utente di HTML.it L'avatar di x69asterix
    Registrato dal
    Jan 2005
    Messaggi
    1,303
    Originariamente inviato da Mabi
    La proprietà State viene aggiornata costantemente.
    Quindi, per sapere lo stato reale della connessione, ti basta controllarla.

    Esempio lato Server:
    • 1 Label (LblWinsock)
      1 Timer (Timer1)
      1 Winsock (Winsock1)

    codice:
    Private Sub Form_Load()
    Timer1.Interval = 500
    Winsock1.LocalPort = "100"
    Winsock1.Listen
    End Sub
    
    Private Sub Timer1_Timer()
    LblWinsock.Caption = StatoWinsock(Winsock1.State)
    End Sub
    
    Private Function StatoWinsock(ValoreNumerico As Integer) As String
    Select Case ValoreNumerico
      Case Is = 0
        StatoWinsock = "Chiuso"
      Case Is = 1
        StatoWinsock = "Aperto"
      Case Is = 2
        StatoWinsock = "In attesa"
      Case Is = 3
        StatoWinsock = "Connessione in sospeso"
      Case Is = 4
        StatoWinsock = "Risoluzione dell'host in corso"
      Case Is = 5
        StatoWinsock = "Host risolto"
      Case Is = 6
        StatoWinsock = "Connessione in corso"
      Case Is = 7
        StatoWinsock = "Connesso"
      Case Is = 8
        StatoWinsock = "Il client sta chiudendo la connessione"
      Case Is = 9
        StatoWinsock = "Errore"
    End Select
    End Function
    
    Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
     If Winsock1.State <> sckClosed Then Winsock1.Close
     Winsock1.Accept requestID
    End Sub
    Esempio lato client:
    • 1 Label (LblWinsock)
      1 Timer (Timer1)
      1 Winsock (Winsock1)
      1 Command Button (Command1)

    codice:
    Private Sub Command1_Click()
    Winsock1.Connect "127.0.0.1", "100"
    End Sub
    
    Private Sub Form_Load()
    Timer1.Interval = 500
    Command1.Caption = "Connetti"
    End Sub
    
    Private Sub Timer1_Timer()
    LblWinsock.Caption = StatoWinsock(Winsock1.State)
    End Sub
    
    Private Function StatoWinsock(ValoreNumerico As Integer) As String
    Select Case ValoreNumerico
      Case Is = 0
        StatoWinsock = "Chiuso"
      Case Is = 1
        StatoWinsock = "Aperto"
      Case Is = 2
        StatoWinsock = "In attesa"
      Case Is = 3
        StatoWinsock = "Connessione in sospeso"
      Case Is = 4
        StatoWinsock = "Risoluzione dell'host in corso"
      Case Is = 5
        StatoWinsock = "Host risolto"
      Case Is = 6
        StatoWinsock = "Connessione in corso"
      Case Is = 7
        StatoWinsock = "Connesso"
      Case Is = 8
        StatoWinsock = "Il client sta chiudendo la connessione"
      Case Is = 9
        StatoWinsock = "Errore"
    End Select
    End Function
    Se, dopo aver ottenuto la connessione (Winsock1.State = 7), il lato server viene chiuso, lo stato cambia in 8.
    Basta quindi verificare il cambiamento da 7 ad uno stato differente per sapere che la connessione è caduta.
    ok grazie per la risposta, metto in pratica e poi ti faccio sapere

  7. #7
    Utente di HTML.it L'avatar di Mabi
    Registrato dal
    May 2002
    Messaggi
    1,245
    OK
    ... e non usare while wend è una sintassi deprecata

  8. #8
    Utente di HTML.it L'avatar di x69asterix
    Registrato dal
    Jan 2005
    Messaggi
    1,303
    Originariamente inviato da Mabi
    OK
    ciao Mabi,
    ottimo listato, funziona bene!

  9. #9
    Utente di HTML.it L'avatar di x69asterix
    Registrato dal
    Jan 2005
    Messaggi
    1,303
    è possibile autenticare la connessione?

  10. #10
    Utente di HTML.it L'avatar di Mabi
    Registrato dal
    May 2002
    Messaggi
    1,245
    Intendi dire tramite una password ?
    Se è così, potresti farlo dopo che è avvenuta la connessione.
    Quindi, la password, sarebbe il primo dato che il client invia al server.
    Continuando con l'esempio precedente...

    Lato server :
    codice:
    Private Sub Form_Load()
    Timer1.Interval = 500
    Winsock1.LocalPort = "100"
    Winsock1.Listen
    End Sub
    
    Private Sub Timer1_Timer()
    LblWinsock.Caption = StatoWinsock(Winsock1.State)
    End Sub
    
    Private Function StatoWinsock(ValoreNumerico As Integer) As String
    Select Case ValoreNumerico
      Case Is = 0
        StatoWinsock = "Chiuso"
      Case Is = 1
        StatoWinsock = "Aperto"
      Case Is = 2
        StatoWinsock = "In attesa"
      Case Is = 3
        StatoWinsock = "Connessione in sospeso"
      Case Is = 4
        StatoWinsock = "Risoluzione dell'host in corso"
      Case Is = 5
        StatoWinsock = "Host risolto"
      Case Is = 6
        StatoWinsock = "Connessione in corso"
      Case Is = 7
        StatoWinsock = "Connesso"
      Case Is = 8
        StatoWinsock = "Il client sta chiudendo la connessione"
      Case Is = 9
        StatoWinsock = "Errore"
    End Select
    End Function
    
    Private Sub Winsock1_ConnectionRequest(ByVal requestID As Long)
     If Winsock1.State <> sckClosed Then Winsock1.Close
     Winsock1.Accept requestID
    End Sub
    
    Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
    Const Password As String = "abcd"
    Static Autenticato As Boolean
    Dim Dati As String
    
    Winsock1.GetData Dati, vbString
    
    If Autenticato = False Then
      If Dati = Password Then
        Autenticato = True
      Else
        Winsock1.Close
      End If
    End If
    End Sub
    Lato client :
    codice:
    Private Sub Command1_Click()
    Winsock1.Connect "127.0.0.1", "100"
    End Sub
    
    Private Sub Form_Load()
    Timer1.Interval = 500
    Command1.Caption = "Connetti"
    End Sub
    
    Private Sub Timer1_Timer()
    LblWinsock.Caption = StatoWinsock(Winsock1.State)
    End Sub
    
    Private Function StatoWinsock(ValoreNumerico As Integer) As String
    Select Case ValoreNumerico
      Case Is = 0
        StatoWinsock = "Chiuso"
      Case Is = 1
        StatoWinsock = "Aperto"
      Case Is = 2
        StatoWinsock = "In attesa"
      Case Is = 3
        StatoWinsock = "Connessione in sospeso"
      Case Is = 4
        StatoWinsock = "Risoluzione dell'host in corso"
      Case Is = 5
        StatoWinsock = "Host risolto"
      Case Is = 6
        StatoWinsock = "Connessione in corso"
      Case Is = 7
        StatoWinsock = "Connesso"
      Case Is = 8
        StatoWinsock = "Il client sta chiudendo la connessione"
      Case Is = 9
        StatoWinsock = "Errore"
    End Select
    End Function
    
    Private Sub Winsock1_Connect()
    Const Password As String = "abcd"
    
    Winsock1.SendData Password
    End Sub
    ... e non usare while wend è una sintassi deprecata

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.