Visualizzazione dei risultati da 1 a 9 su 9
  1. #1

    [VB] Una semplice chat per 2 Client

    Vorrei costruire una semplice chat per 2 utenti in VB.NET

    cioe':

    2 client con semplicissimi che si contattano tramite l'IP sia in rete locale sia in Internet tra 2 computer remoti,
    che come interfaccia debbano solo avere una TextBox per scrivere, un Bottone per inviare e non so....... forse una TextBox - Multiline per visualizzare il testo dell'altro client.

    i miei problemi e dubbi:

    1) come si effettua una chiamata verso un altro client con l'IP ???

    2) come si puo' nel momento che premo il pulsante dell'invio del messaggio fare visualizzare il testo nel visualizzatore dell'altro client ???

    3) devo usare un WebService come motore chat???

  2. #2
    Utente bannato
    Registrato dal
    Apr 2002
    Messaggi
    546
    Devi usare il controllo winsock.

    se fai una ricerca nel forum troverai il materiale.........






    link
    homepage
    http://%77%77%77%2e%74%77%6f%72%6b%2e%69%74/
    software
    http://%77%77%77%2e%74%77%6f%72%6b%2...programmi.html
    forum
    http://%77%77%77%2e%74%77%6f%72%6b%2...um/default.asp

  3. #3
    Caspita che prontezza!!!!!!!!!!
    Ti ringrazio per ora, ciao!

  4. #4
    Ma nessuno porta ad una conclusione e tutti hanno gia una base , io poveretto non so da dove partire

    e poi come se non bastasse non conosco VB6 ma solo VB.NET

    tu mi puoi indirizzare in qualche articolo giusto??? ....magari in Italiano ???

  5. #5
    Utente di HTML.it
    Registrato dal
    Jul 2002
    Messaggi
    110

    Risposta

    Se mi dai la tua e-mail, ti posso spedire un piccolo esempio di Chat!!!

  6. #6
    Benissimo ---> webtechnical@supereva.it

    Molte grazie, solo una cosa ho trovato degli esempi di chat con Winsock , ma poi non posso eseguire gli esempi perche' non ho il controllo Winsock, ma dove vado a scaricarlo????

  7. #7
    Utente di HTML.it L'avatar di biste
    Registrato dal
    Apr 2001
    Messaggi
    877
    xkè ti devi andare a tirare dentro il winsock se stai usando il .net? ci sono le classi apposta!
    Leggiti la documentazione, cmq dovrebbero trovarsi all'interno del namespace System.Net
    UGIdotNET
    Microsoft .NET MCAD
    C++, C#, VB6, VB.NET, ASP, ASP.NET
    SQL Server 2000

  8. #8
    Si certo fino a qui ci sono, e' il NameSystem giusto quello che dici tu, ma io non so come gestire la connessione e come inviare il messaggio, e su MSDN non ci capisco nulla e' tutto troppo scheletrico e non fa esempi ma solo fa vedere la struttura della classe o del costruttore della classe, ma io che non ho mai fato un applicazione del genere com e faccio a progettare da solo una chat???

  9. #9
    Sono arrivato a questo punto:

    ho fatto un Server e un Client che comunicano tra loro e si scambiano i messaggi, solo che i messaggi di ciascuno non partono fino a che l'altro non ne invia uno a sua volta , come mai?

    io vorrei arrivare al punto che creo una chat vera e propria, ma sensa nessuna funzione particolare solo per parlare.

    Potete dare un occhiata o meglio copiare i progetti ed eseguirli per vedere dove sbaglio???

    GRAZIE MILLE !!!

    Server
    codice:
    Imports System.Net.Sockets
    Imports System.Text
    
    Public Class TCPSrv
        Inherits System.Windows.Forms.Form
    
    #Region " Windows Form Designer generated code "
    
        Public Sub New()
            MyBase.New()
    
            'This call is required by the Windows Form Designer.
            InitializeComponent()
    
            'Add any initialization after the InitializeComponent() call
    
        End Sub
    
        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub
        Friend WithEvents btnInvia As System.Windows.Forms.Button
                Friend WithEvents txtMessaggio As System.Windows.Forms.TextBox
        Friend WithEvents Label2 As System.Windows.Forms.Label
        Friend WithEvents txtDisplay As System.Windows.Forms.TextBox
            Friend WithEvents btnDisconnect As System.Windows.Forms.Button
        Friend WithEvents Label1 As System.Windows.Forms.Label
            
        'Required by the Windows Form Designer
        Private components As System.ComponentModel.Container
    
        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.btnDisconnect = New System.Windows.Forms.Button()
            Me.Label2 = New System.Windows.Forms.Label()
            Me.btnInvia = New System.Windows.Forms.Button()
            Me.txtDisplay = New System.Windows.Forms.TextBox()
            Me.txtMessaggio = New System.Windows.Forms.TextBox()
            Me.Label1 = New System.Windows.Forms.Label()
            Me.SuspendLayout()
            '
            'btnDisconnect
            '
            Me.btnDisconnect.Location = New System.Drawing.Point(8, 96)
            Me.btnDisconnect.Name = "btnDisconnect"
            Me.btnDisconnect.Size = New System.Drawing.Size(80, 24)
            Me.btnDisconnect.TabIndex = 7
            Me.btnDisconnect.Text = "Disconnetti"
            '
            'Label2
            '
            Me.Label2.AutoSize = True
            Me.Label2.Location = New System.Drawing.Point(8, 56)
            Me.Label2.Name = "Label2"
            Me.Label2.Size = New System.Drawing.Size(59, 13)
            Me.Label2.TabIndex = 4
            Me.Label2.Text = "Messaggio"
            '
            'btnInvia
            '
            Me.btnInvia.Location = New System.Drawing.Point(240, 56)
            Me.btnInvia.Name = "btnInvia"
            Me.btnInvia.Size = New System.Drawing.Size(40, 24)
            Me.btnInvia.TabIndex = 0
            Me.btnInvia.Text = "Invia"
            '
            'txtDisplay
            '
            Me.txtDisplay.Location = New System.Drawing.Point(64, 8)
            Me.txtDisplay.Multiline = True
            Me.txtDisplay.Name = "txtDisplay"
            Me.txtDisplay.Size = New System.Drawing.Size(168, 40)
            Me.txtDisplay.TabIndex = 5
            Me.txtDisplay.Text = ""
            '
            'txtMessaggio
            '
            Me.txtMessaggio.Location = New System.Drawing.Point(64, 56)
            Me.txtMessaggio.Name = "txtMessaggio"
            Me.txtMessaggio.Size = New System.Drawing.Size(168, 20)
            Me.txtMessaggio.TabIndex = 3
            Me.txtMessaggio.Text = ""
            '
            'Label1
            '
            Me.Label1.AutoSize = True
            Me.Label1.Location = New System.Drawing.Point(24, 8)
            Me.Label1.Name = "Label1"
            Me.Label1.Size = New System.Drawing.Size(41, 13)
            Me.Label1.TabIndex = 8
            Me.Label1.Text = "Display"
            '
            'TCPSrv
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(292, 125)
            Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtDisplay, Me.Label1, Me.btnDisconnect, Me.txtMessaggio, Me.Label2, Me.btnInvia})
            Me.Location = New System.Drawing.Point(10, 10)
            Me.Name = "TCPSrv"
            Me.Text = "Server"
            Me.ResumeLayout(False)
    
        End Sub
    
    #End Region
    
        'Must listen on correct port- must be same as port client wants to connect on.
        Const portNumber As Integer = 8000
        Dim tcpListener As New tcpListener(portNumber)
    
        Private Sub TCPSrv_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            tcpListener.Start()
        End Sub
    
        Private Sub btnInvia_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInvia.Click
    
            Dim responseString As String = txtMessaggio.Text
    
            'Accetta la connessione in corso del Client e restituisce TcpClient inizializzaro per la comunicazione. 
            Dim tcpClient As TcpClient = tcpListener.AcceptTcpClient()
    
            'Invia il flusso
            Dim networkStream As NetworkStream = tcpClient.GetStream()
    
            'legge il flusso nell'array "byte"
            Dim bytes(tcpClient.ReceiveBufferSize) As Byte
            networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
    
            'visualizza i dati trasmessi dal Client
            Dim clientdata As String = Encoding.ASCII.GetString(bytes)
            txtDisplay.Text = clientdata
    
            Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(responseString)
            networkStream.Write(sendBytes, 0, sendBytes.Length)
    
            txtMessaggio.Text = Nothing
    
            'Chiude TcpClient.
            tcpClient.Close()
    
        End Sub
    
        Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
            'Chiude TcpListener
            tcpListener.Stop()
            MsgBox("Connessione interrotta!")
        End Sub
    
    End Class
    Client
    codice:
    Imports System.Net.Sockets
    Imports System.Text
    
    Public Class TCPCli
        Inherits System.Windows.Forms.Form
    
    #Region " Windows Form Designer generated code "
    
        Public Sub New()
            MyBase.New()
    
            'This call is required by the Windows Form Designer.
            InitializeComponent()
    
            'Add any initialization after the InitializeComponent() call
    
        End Sub
    
        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                    components.Dispose()
                End If
            End If
            MyBase.Dispose(disposing)
        End Sub
        Friend WithEvents btnInvia As System.Windows.Forms.Button
                Friend WithEvents txtMessaggio As System.Windows.Forms.TextBox
        Friend WithEvents Label2 As System.Windows.Forms.Label
        Friend WithEvents txtDisplay As System.Windows.Forms.TextBox
        Friend WithEvents Label1 As System.Windows.Forms.Label
        Friend WithEvents txtIP As System.Windows.Forms.TextBox
        Friend WithEvents Label3 As System.Windows.Forms.Label
    
        'Required by the Windows Form Designer
        Private components As System.ComponentModel.Container
    
        'NOTE: The following procedure is required by the Windows Form Designer
        'It can be modified using the Windows Form Designer.  
        'Do not modify it using the code editor.
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.Label2 = New System.Windows.Forms.Label()
            Me.txtDisplay = New System.Windows.Forms.TextBox()
            Me.btnInvia = New System.Windows.Forms.Button()
            Me.txtMessaggio = New System.Windows.Forms.TextBox()
            Me.Label1 = New System.Windows.Forms.Label()
            Me.txtIP = New System.Windows.Forms.TextBox()
            Me.Label3 = New System.Windows.Forms.Label()
            Me.SuspendLayout()
            '
            'Label2
            '
            Me.Label2.AutoSize = True
            Me.Label2.Location = New System.Drawing.Point(8, 104)
            Me.Label2.Name = "Label2"
            Me.Label2.Size = New System.Drawing.Size(59, 13)
            Me.Label2.TabIndex = 4
            Me.Label2.Text = "Messaggio"
            '
            'txtDisplay
            '
            Me.txtDisplay.Location = New System.Drawing.Point(64, 48)
            Me.txtDisplay.Multiline = True
            Me.txtDisplay.Name = "txtDisplay"
            Me.txtDisplay.Size = New System.Drawing.Size(168, 48)
            Me.txtDisplay.TabIndex = 5
            Me.txtDisplay.Text = ""
            '
            'btnInvia
            '
            Me.btnInvia.Location = New System.Drawing.Point(240, 104)
            Me.btnInvia.Name = "btnInvia"
            Me.btnInvia.Size = New System.Drawing.Size(40, 24)
            Me.btnInvia.TabIndex = 0
            Me.btnInvia.Text = "Invia"
            '
            'txtMessaggio
            '
            Me.txtMessaggio.Location = New System.Drawing.Point(64, 104)
            Me.txtMessaggio.Name = "txtMessaggio"
            Me.txtMessaggio.Size = New System.Drawing.Size(168, 20)
            Me.txtMessaggio.TabIndex = 3
            Me.txtMessaggio.Text = ""
            '
            'Label1
            '
            Me.Label1.AutoSize = True
            Me.Label1.Location = New System.Drawing.Point(48, 8)
            Me.Label1.Name = "Label1"
            Me.Label1.Size = New System.Drawing.Size(15, 13)
            Me.Label1.TabIndex = 6
            Me.Label1.Text = "IP"
            '
            'txtIP
            '
            Me.txtIP.Location = New System.Drawing.Point(64, 8)
            Me.txtIP.Name = "txtIP"
            Me.txtIP.Size = New System.Drawing.Size(168, 20)
            Me.txtIP.TabIndex = 7
            Me.txtIP.Text = ""
            '
            'Label3
            '
            Me.Label3.AutoSize = True
            Me.Label3.Location = New System.Drawing.Point(24, 48)
            Me.Label3.Name = "Label3"
            Me.Label3.Size = New System.Drawing.Size(41, 13)
            Me.Label3.TabIndex = 8
            Me.Label3.Text = "Display"
            '
            'TCPCli
            '
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(292, 141)
            Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.txtIP, Me.txtDisplay, Me.Label3, Me.Label1, Me.txtMessaggio, Me.Label2, Me.btnInvia})
            Me.Name = "TCPCli"
            Me.Text = "Client"
            Me.ResumeLayout(False)
    
        End Sub
    
    #End Region
    
        Private Sub btnInvia_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnInvia.Click
    
            Dim messaggio As String = txtMessaggio.Text
            Dim IP As String = txtIP.Text
    
            Dim tcpClient As New System.Net.Sockets.TcpClient()
            tcpClient.Connect(IP, 8000)
    
            Dim networkStream As NetworkStream = tcpClient.GetStream()
    
            If networkStream.CanWrite And networkStream.CanRead Then
                'Faccio una semplice scrittura.
                Dim sendBytes As [Byte]() = Encoding.ASCII.GetBytes(messaggio)
                networkStream.Write(sendBytes, 0, sendBytes.Length)
    
                'lettura del NetworkStream nel byte buffer.
                Dim bytes(tcpClient.ReceiveBufferSize) As Byte
                networkStream.Read(bytes, 0, CInt(tcpClient.ReceiveBufferSize))
    
                'visualizzazione dei dati inviati dal Server
                Dim returndata As String = Encoding.ASCII.GetString(bytes)
                txtDisplay.Text = returndata
    
                txtMessaggio.Text = Nothing
            Else
                If Not networkStream.CanRead Then
                    MsgBox("Non posso leggere i dati da questo flusso")
                    tcpClient.Close()
                Else
                    If Not networkStream.CanWrite Then
                        MsgBox("Non posso leggere i dati da questo flusso")
                        tcpClient.Close()
                    End If
                End If
            End If
    
        End Sub
    End Class

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 © 2024 vBulletin Solutions, Inc. All rights reserved.