Visualizzazione dei risultati da 1 a 3 su 3
  1. #1
    Utente di HTML.it L'avatar di danjwo
    Registrato dal
    Aug 2007
    Messaggi
    43

    [VB.NET] Invio Messaggio via LAN

    Salve,

    ho scritto un software prendendo spunto dalla guida di TOTEM nel capitolo SOCKETS.

    Faccio partire il Server su un Computer ed il Client sull'altro..

    Invio Il Messaggio ma è come se "Si perdesse", non arriva nulla.
    Dopo un paio d'ore a mi sono rivolto a voi. Vi posto il Codice

    Il Client

    codice:
    Private Sub tmrGetData_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrGetData.Tick
            If client.Connected Then
                If client.Available > 0 Then
                    Dim bytes(client.ReceiveBufferSize) As Byte
                    netstr.Read(bytes, 0, client.ReceiveBufferSize)
                    s = System.Text.ASCIIEncoding.ASCII.GetString(bytes)
                    'If s = "calldan" Then
                    '    frmled.tmrled.Start()
                    '    else
                    '    msgbox(s)
                    'End If
                End If
            End If
        End Sub
        Sub collega()
            client.Connect("127.0.0.1", 25)
            If client.Connected Then
                netstr = client.GetStream
                MsgBox("CONNESSO")
                MsgBox(client.Available)
            Else
                MsgBox("NON CONNESSO")
            End If
        End Sub
    Ed il Server

    codice:
    Imports System.Net.Sockets
    Public Class frmmain
        Public listener As New TcpListener(25)
        Public client As New TcpClient
        Public netstr As NetworkStream
        Private Sub frmmain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            listener.Start()
    
        End Sub
    
        Private Sub tmrControlConnection_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrControlConnection.Tick
            If listener.Pending Then
                client = listener.AcceptTcpClient
                netstr = client.GetStream
                tmrControlConnection.Stop()
                listener.Stop()
                tmrGetData.Start()
            End If
        End Sub
    
        Private Sub tmrGetData_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrGetData.Tick
            If client.Connected Then
                If client.Available > 0 Then
                    Dim bytes(client.ReceiveBufferSize) As Byte
                    netstr.Read(bytes, 0, client.ReceiveBufferSize)
                    Dim s As String = System.Text.ASCIIEncoding.ASCII.GetString(bytes)
                End If
            End If
        End Sub
    
        Private Sub ESCIToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnesci.Click
            chiusura()
        End Sub
        Sub chiusura()
            Me.Dispose()
        End Sub
        Sub messaggio(ByVal msg As String)
            If client.Connected Then
                If netstr.CanWrite Then
                    Try
                        Dim bytes() As Byte = System.Text.ASCIIEncoding.ASCII.GetBytes(msg)
                        netstr.Write(bytes, 0, bytes.Length)
                        frmok.lblok.Text = "RICHIESTA INVIATA CORRETTAMENTE"
                        ListBox1.Items.Add(System.Text.ASCIIEncoding.ASCII.GetString(bytes))
                        frmok.Show()
                    Catch ex As Exception
                        frmerrore.lblerrore.Text = "ERRORE: " + ex.ToString
                        frmerrore.Show()
                    End Try
                End If
            End If
        End Sub
    
        Private Sub mntrilla_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mntrilla.Click
            messaggio("calldan")
    
        End Sub
            Public msgsend As String
        Private Sub mnmsg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnmsg.Click
            If frmmsg.ShowDialog = Windows.Forms.DialogResult.OK Then
                messaggio(msgsend)
            End If
        End Sub
    Grazie Ancora per le eventuali risposte, spero ci siano degli errori e non sia il router a bloccare tutto (io comunque in entrambi i casi ho sbloccato il processo vshost.exe)
    Sicut Nox Silentes

  2. #2
    Hai attivato tmrGetData sul lato Client?

  3. #3
    Utente di HTML.it L'avatar di danjwo
    Registrato dal
    Aug 2007
    Messaggi
    43
    Le Soluzioni più semplici sono quelle che alle 23 non ti vengono mai fuori !!!

    Comunque ora funziona al 75% ma mi da un paio di errori:

    1) Se io chiudo il client e poi tento di riaprilo mi dice "Rifiuto persistente del computer a cui ci si vuole collegare"

    2) mi stampa tutto corretto ma quando il messaggio è "calldan" dovrebbe aprirmi un form particolare e non scriverlo a video.
    codice:
    If client.Connected Then
                If client.Available > 0 Then
                    Dim bytes(client.ReceiveBufferSize) As Byte
                    netstr.Read(bytes, 0, client.ReceiveBufferSize)
                    s = System.Text.ASCIIEncoding.ASCII.GetString(bytes)
                    If s = "calldan" Then
                        frmled.Show()
                        frmled.tmrled.Start()
                    Else
                        MsgBox(s)
                    End If
                End If
            End If
    Questo è il form che deve apparire

    codice:
    Public Class frmled
        Dim lampeggio As Boolean = True
        Private Sub tmrled_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrled.Tick
            If lampeggio = True Then
                Me.Visible = True
                lampeggio = False
            Else
                Me.Visible = False
                lampeggio = True
            End If
        End Sub
    
        Private Sub imgled_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles imgled.Click
    
        End Sub
    
        Private Sub imgled_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles imgled.DoubleClick
            tmrled.Stop()
        End Sub
    
        Private Sub frmled_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim pt As Point
            pt = New Point(My.Computer.Screen.WorkingArea.Width - 210, 0)
            Me.Location = pt
    
        End Sub
    End Class
    Sicut Nox Silentes

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.