Visualizzazione dei risultati da 1 a 2 su 2

Discussione: [VB.NET] Winsock

  1. #1
    Utente di HTML.it
    Registrato dal
    Mar 2002
    Messaggi
    87

    [vb.net] winsock

    ciao a tutti, ho trovato un programma in vb6 che con il socket mi permette tramite i comandi:
    LIST
    QUIT
    RETR
    etc..etc
    di controllare quanti mess di posta elettronica sono nella mia casella.
    ho importato il progetto con il vb.net e mi ha chiesto di fare l'aggiornamento.
    dopo questo faccio girare il programma, mi collego, cancello, e mi disconnetto tranquillamente, le uniche operazioni che non riesco a fare sono quelle di ricezione dei dati!
    nel senso che se utilizzo il comando STAT (che dovrebbe ritornarmi quanti mess ho) non ricevo niente.
    ora inserisco il codice in vb.net, se potete datemi una mano grazie.

    Private Sub cmdClear_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdClear.Click

    ' clear the log window
    txtLog.Text = ""
    End Sub

    Private Sub cmdConnect_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdConnect.Click

    ' setup the winsock
    Socket.Close()
    Socket.RemoteHost = txtServer.Text
    Socket.RemotePort = CInt(txtPort.Text)

    ' initiate the connect
    lblStatus.Text = "Status: Connecting..."
    Socket.Connect()
    End Sub

    Private Sub cmdDisconnect_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdDisconnect.Click

    ' log off
    If Socket.CtlState = MSWinsockLib.StateConstants.sckConnected Then
    Socket.SendData("QUIT" & vbCrLf)
    System.Windows.Forms.Application.DoEvents()
    End If
    End Sub

    Private Sub frmMain_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
    ' fill the box with commands
    lstCommands.Items.Add("USER")
    lstCommands.Items.Add("PASS")
    lstCommands.Items.Add("STAT")
    lstCommands.Items.Add("LIST")
    lstCommands.Items.Add("RETR")
    lstCommands.Items.Add("DELE")
    lstCommands.Items.Add("NOOP")
    lstCommands.Items.Add("RSET")
    lstCommands.Items.Add("QUIT")
    End Sub

    'UPGRADE_WARNING: Form evento frmMain.Unload presenta un nuovo comportamento. Fare clic qui per ulteriori informazioni: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup2065"'
    Private Sub frmMain_Closed(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Closed

    ' if we're still signed in, then sign off
    If Socket.CtlState = MSWinsockLib.StateConstants.sckConnected Then
    Socket.SendData("QUIT" & vbCrLf)
    System.Windows.Forms.Application.DoEvents()
    End If
    End Sub

    'UPGRADE_WARNING: L'evento lstCommands.SelectedIndexChanged può essere generato quando il form è inizializzato. Fare clic qui per ulteriori informazioni: 'ms-help://MS.VSCC.2003/commoner/redir/redirect.htm?keyword="vbup2075"'
    Private Sub lstCommands_SelectedIndexChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles lstCommands.SelectedIndexChanged

    ' show a description of the clicked on command
    If lstCommands.SelectedIndex = -1 Then
    Exit Sub
    Else
    Select Case VB6.GetItemString(lstCommands, lstCommands.SelectedIndex)
    Case "USER"
    lblDescription.Text = "The USER command tells the server " & "what our username is, and therefore " & "which mail box we wish to access"
    Case "PASS"
    lblDescription.Text = "Gives the password for the username " & "transmitted previously with the USER " & "command."
    Case "LIST"
    lblDescription.Text = "The LIST command requests a list" & " of the mails in the mailbox. The" & " data returned holds the mail number" & " and size of each mail in bytes" & vbCrLf & "Also accepts one parameter - the mail number" & " and will return information about that mail."
    Case "RETR"
    lblDescription.Text = "Request a mail from the server. The" & " server will then return the entire" & " mail. This command takes one parameter" & " which is the number of the mail to retrieve."
    Case "DELE"
    lblDescription.Text = "Deletes a mail from the server. " & "This command takes one parameter" & " which is the number of the mail to delete." & " The server will mark that mail as deleted."
    Case "STAT"
    lblDescription.Text = "Displays information about the mailbox" & " Number of mails, and total mailbox size in bytes."
    Case "NOOP"
    lblDescription.Text = "This merely checks to see if the" & " connection is still ok or not."
    Case "RSET"
    lblDescription.Text = "If any messages have been marked as deleted" & " in the current session, they are unmarked."
    Case "QUIT"
    lblDescription.Text = "Instructs the POP server to close the connection."
    End Select
    End If
    End Sub

    Private Sub lstCommands_DoubleClick(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles lstCommands.DoubleClick
    Dim Cmd As String
    Dim Param As String

    ' execute the clicked on command
    If lstCommands.SelectedIndex = -1 Then
    Exit Sub
    Else
    ' grab the command from the listbox
    Cmd = VB6.GetItemString(lstCommands, lstCommands.SelectedIndex)

    ' if the command requires an extra parameter, ask for one
    If Cmd = "LIST" Or Cmd = "RETR" Or Cmd = "DELE" Then
    Param = Trim(InputBox("Enter mail number:"))
    ElseIf Cmd = "USER" Or Cmd = "PASS" Then
    Param = Trim(InputBox("Enter details:"))
    End If

    ' send it down the socket
    If Socket.CtlState = MSWinsockLib.StateConstants.sckConnected Then
    Socket.SendData(Cmd & " " & Param & vbCrLf)
    System.Windows.Forms.Application.DoEvents()
    End If

    ' display
    txtLog.Text = txtLog.Text & "-->" & Cmd & " " & Param & vbCrLf
    End If
    End Sub

    Private Sub Socket_CloseEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Socket.CloseEvent

    lblStatus.Text = "Status: Closed remotely"
    End Sub

    Private Sub Socket_ConnectEvent(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Socket.ConnectEvent
    Dim Cmd As String

    lblStatus.Text = "Status: Loggin in..."

    ' we're connected, so we have to log in
    Cmd = "USER " & txtUserName.Text & vbCrLf
    Socket.SendData(Cmd)
    System.Windows.Forms.Application.DoEvents()
    ' display
    txtLog.Text = txtLog.Text & "-->" & Cmd

    Cmd = "PASS " & txtPassword.Text & vbCrLf
    Socket.SendData(Cmd)
    System.Windows.Forms.Application.DoEvents()
    ' display
    txtLog.Text = txtLog.Text & "-->" & Cmd

    lblStatus.Text = "Status: Connected"
    End Sub

    Private Sub Socket_DataArrival(ByVal eventSender As System.Object, ByVal eventArgs As AxMSWinsockLib.DMSWinsockControlEvents_DataArrival Event) Handles Socket.DataArrival
    Dim Data As String

    ' grab the data
    Socket.GetData(Data)

    ' check it
    txtLog.Text = txtLog.Text & Data
    txtLog.SelectionStart = Len(txtLog.Text)
    End Sub

    Private Sub Socket_Error(ByVal eventSender As System.Object, ByVal eventArgs As AxMSWinsockLib.DMSWinsockControlEvents_ErrorEvent) Handles Socket.Error

    lblStatus.Text = "Status: Error - " & eventArgs.Description
    End Sub
    End Class



    spero che qualcosa si capisca grazie

  2. #2
    Il codice è illeggibile, ricordati di usare i Tag.
    Comunque non riesco a vedere dove il socket viene messo in ascolto (metodo Listen). Se non lo fai non può mai verificarsi l'evento DataArrival.

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.