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

    Invio flusso dati splittati con NetworkStream

    Ciao a tutti sto studiando un po le socket con vb.net e ho creato un semplice programma client-server che colloquiano con una chat.. ora vorrei aggiungere la cam (video chat) quindi dovrei inviare il flusso dati della cam da client a server e viceversa.... come devo splittare il flusso dati? avevo pensato ad una cosa tipo...

    client:
    dim msg as string = "CHAT"
    dim separatore as string = msg.split("|")
    send (separatore, Stream)

    server:
    select case opt(0)

    case "CHAT|"

    operazioni.....
    case "CAM|"

    end select


    vi posto il codice client server....

    CLIENT:
    codice:
    Imports System.Net.Sockets
    Imports System.Text.UTF8Encoding
    Imports System.IO
    Imports System.Runtime.InteropServices
    
    Public Class frmMain
        'Webcam
        Public Touchless As New TouchlessLib.TouchlessMgr
        Public Camera1 As TouchlessLib.Camera = Touchless.Cameras.ElementAt(0)
        Dim IP As System.Net.IPHostEntry = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName)
    
        Dim client As TcpClient
        Dim Stream As NetworkStream
    
        Private Sub Client_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If System.IO.File.Exists(Application.StartupPath & "\" & "WebCamLib.dll") = False Then
                Dim B() As Byte
                Dim RM As Resources.ResourceManager
                RM = New Resources.ResourceManager("vbClient.Resources", System.Reflection.Assembly.GetExecutingAssembly)
                B = RM.GetObject("WebCamLib")
                System.IO.File.WriteAllBytes(Application.StartupPath & "\" & "WebCamLib.dll", B)
            End If
    
            Nickname.Text = My.User.Name
            TextBox2.Text = "Conversazione" & vbCrLf
            TextBox2.ReadOnly = True
            ' lblIP.Text = IP.AddressList.GetValue(0).ToString
            ' WebCam.StartFeed(Me.PictureBox1.Handle.ToInt32)
            Try
    
                Touchless.CurrentCamera = Camera1
                Touchless.CurrentCamera.CaptureHeight = 240
                Touchless.CurrentCamera.CaptureWidth = 320
                Timer2.Start()
                Call connect()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
        Public Sub connect()
            Dim Address As Net.IPAddress
            Net.IPAddress.TryParse(Me.txtip.Text, Address)
            client = New TcpClient
            client.Connect(Address, Me.txtport.Text)
            If client.Connected Then
                Stream = client.GetStream
                Me.Timer1.Start()
                Me.TextBox2.Text = Me.TextBox2.Text & "Connessione effettuata." & vbCrLf
                Stream.Write(UTF8.GetBytes("Si è connesso " & Me.Nickname.Text & "!"), 0, UTF8.GetBytes("Si è connesso " & Me.Nickname.Text & "!").Length)
            Else
                Me.TextBox2.Text = Me.TextBox2.Text & "Impossibile connettersi..." & vbCrLf
            End If
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            If client.Available > 0 Then
                Dim x(client.Available - 1) As Byte
                Stream.Read(x, 0, x.Length)
                Dim text As String = UTF8.GetString(x)
                TextBox2.Text = TextBox2.Text & text & vbCrLf
                ' Dim msg As String = "CAM|"
                ' Dim ss() As Byte = BmpToByte(pView.Image)
                ' Stream.Write(ss, 0, ss.Length)
            End If
        End Sub
    
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            TextBox2.Text = "Conversazione" & vbCrLf
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Try
                Dim y() As Byte = UTF8.GetBytes(Nickname.Text & ": " & TextBox1.Text)
                Stream.Write(y, 0, y.Length)
                TextBox2.Text = TextBox2.Text & Nickname.Text & ": " & TextBox1.Text & vbCrLf
                TextBox1.Text = Nothing
            Catch ex As NullReferenceException
                MsgBox("Non sei collegato con nessuno!")
            Catch ex As System.IO.IOException
                MsgBox("Il destinatario si è scollegato o ha conflitti di IP!")
            End Try
        End Sub
    
       
        Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
            pView.Image = Touchless.CurrentCamera.GetCurrentImage
        End Sub
    
    'Funzione di conversione da BMP a Byte
        Public Shared Function BmpToByte(ByVal _bitmap As System.Drawing.Bitmap) As Byte()
            Dim _ms As New Global.System.IO.MemoryStream()
            _bitmap.Save(_ms, System.Drawing.Imaging.ImageFormat.Bmp)
            Dim _bmpArray As Byte()
            _bmpArray = _ms.ToArray()
            _ms.Flush()
            _ms.Dispose()
            Return _bmpArray
        End Function
    
    
    End Class
    SERVER:
    codice:
    Imports System.Net.Sockets
    Imports System.Text.UTF8Encoding
    
    
    Public Class frmMain
        Dim Listener As TcpListener
        Dim Client As TcpClient
        Dim Stream As NetworkStream
        Private Sub Server_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Nickname.Text = "kerberos5"
    
            Chat.Text = "Conversazione" & vbCrLf
            Chat.ReadOnly = True
    
            Listener = New TcpListener(8080)
            Listener.Start()
    
            Timer1.Start()
    
        End Sub
    
        Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
            If Client.Available > 0 Then
                Dim x(Client.Available - 1) As Byte
                Stream.Read(x, 0, x.Length)
                Dim text As String = UTF8.GetString(x)
                Chat.Text = Chat.Text & text & vbCrLf
            End If
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            If Listener.Pending = True Then
                Timer1.Stop()
                Client = Listener.AcceptTcpClient()
                Listener.Stop()
                Stream = Client.GetStream()
                Timer2.Start()
            End If
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Try
                Dim y() As Byte = UTF8.GetBytes(Nickname.Text & ": " & Messaggio.Text)
                Stream.Write(y, 0, y.Length)
                Chat.Text = Chat.Text & Nickname.Text & ": " & Messaggio.Text & vbCrLf
                Messaggio.Text = Nothing
            Catch ex As NullReferenceException
                MsgBox("Non si è ancora connesso nessuno, aspetta e riprova!")
            Catch ex2 As System.IO.IOException
                MsgBox("Il destinatario del messaggio si è scollegato o ha conflitti di IP!")
            End Try
        End Sub
    
     
    
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            Chat.Text = "Conversazione" & vbCrLf
        End Sub
    
    'Funzione di conversione da Byte a BMP
        Public Shared Function ByteToBmp(ByVal _imgByteArray As Byte()) As System.Drawing.Bitmap
            Dim _ms As New Global.System.IO.MemoryStream(_imgByteArray, 0, _imgByteArray.Length)
            _ms.Write(_imgByteArray, 0, _imgByteArray.Length)
            Dim _bmp As New System.Drawing.Bitmap(_ms)
            _ms.Flush()
            _ms.Dispose()
            Return _bmp
        End Function
    
    
    
    End Class

  2. #2
    Mi hanno suggerito di utilizzare i canali specificando il canale.. dove diregere il tipo di flusso...
    ma non riesco a dividere i 2 flussi vi posto il codice aggiornato NON funzionante i 2 flussi sono uniti...

    SERVER
    codice:
    Imports System.Net.Sockets
    Imports System.Text.UTF8Encoding
    
    
    Public Class frmMain
        Public Const ChatM As String = "775|"
        Public Const CamM As String = "776|"
        Dim Listener As TcpListener
        Dim Client As TcpClient
        Dim Stream As NetworkStream
        Private Sub Server_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            Nickname.Text = "kerberos5"
    
            Chat.Text = "Conversazione" & vbCrLf
            Chat.ReadOnly = True
    
            Listener = New TcpListener(8080)
            Listener.Start()
    
            Timer1.Start()
    
        End Sub
    
        Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
            If Client.Available > 0 Then
                Dim x(Client.Available - 1) As Byte
                Stream.Read(x, 0, x.Length)
                Dim text As String = UTF8.GetString(x)
                Chat.Text = Chat.Text & text & vbCrLf
            End If
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            If Listener.Pending = True Then
                Timer1.Stop()
                Client = Listener.AcceptTcpClient()
                Listener.Stop()
                Stream = Client.GetStream()
                Timer2.Start()
            End If
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Try
                Dim y() As Byte = UTF8.GetBytes(Nickname.Text & ": " & Messaggio.Text)
                Stream.Write(y, 0, y.Length)
                Chat.Text = Chat.Text & Nickname.Text & ": " & Messaggio.Text & vbCrLf
                Messaggio.Text = Nothing
            Catch ex As NullReferenceException
                MsgBox("Non si è ancora connesso nessuno, aspetta e riprova!")
            Catch ex2 As System.IO.IOException
                MsgBox("Il destinatario del messaggio si è scollegato o ha conflitti di IP!")
            End Try
        End Sub
    
    
    
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            Chat.Text = "Conversazione" & vbCrLf
        End Sub
    
    
        Public Shared Function ByteToBmp(ByVal _imgByteArray As Byte()) As System.Drawing.Bitmap
            Dim _ms As New Global.System.IO.MemoryStream(_imgByteArray, 0, _imgByteArray.Length)
            _ms.Write(_imgByteArray, 0, _imgByteArray.Length)
            Dim _bmp As New System.Drawing.Bitmap(_ms)
            _ms.Flush()
            _ms.Dispose()
            Return _bmp
        End Function
    
        Public Shared Function Foo(ByVal eChannel As String, ByVal iMessage As String) 'Pass the channel and message to the subroutine
            Dim eMessage As String
            eMessage = eChannel & iMessage
        End Function
    
        Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick
            If Client.Available > 0 Then
                Dim ss(Client.Available - 1) As Byte
                Stream.Read(ss, 0, ss.Length)
                If UTF8.GetString(ss) = "776|" Then 'provo a fargli leggere il canale...??
                    pView.Image = ByteToBmp(ss)
                End If
            End If
        End Sub
    End Class
    CLIENT:
    codice:
    Imports System.Net.Sockets
    Imports System.Text.UTF8Encoding
    Imports System.IO
    Imports System.Runtime.InteropServices
    
    Public Class frmMain
        Public Const ChatM As String = "775|"
        Public Const CamM As String = "776|"
        'Webcam
        Public Touchless As New TouchlessLib.TouchlessMgr
        Public Camera1 As TouchlessLib.Camera = Touchless.Cameras.ElementAt(0)
        Dim IP As System.Net.IPHostEntry = System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName)
    
        Dim client As TcpClient
        Dim Stream As NetworkStream
    
        Private Sub Client_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            If System.IO.File.Exists(Application.StartupPath & "\" & "WebCamLib.dll") = False Then
                Dim B() As Byte
                Dim RM As Resources.ResourceManager
                RM = New Resources.ResourceManager("vbClient.Resources", System.Reflection.Assembly.GetExecutingAssembly)
                B = RM.GetObject("WebCamLib")
                System.IO.File.WriteAllBytes(Application.StartupPath & "\" & "WebCamLib.dll", B)
            End If
    
            Nickname.Text = My.User.Name
            TextBox2.Text = "Conversazione" & vbCrLf
            TextBox2.ReadOnly = True
    
            Try
    
                Touchless.CurrentCamera = Camera1
                Touchless.CurrentCamera.CaptureHeight = 240
                Touchless.CurrentCamera.CaptureWidth = 320
                Timer2.Start()
                Call connect()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End Sub
        Public Sub connect()
            Dim Address As Net.IPAddress
            Net.IPAddress.TryParse(Me.txtip.Text, Address)
            client = New TcpClient
            client.Connect(Address, Me.txtport.Text)
            If client.Connected Then
                Stream = client.GetStream
                Me.Timer1.Start()
                Me.TextBox2.Text = Me.TextBox2.Text & "Connessione effettuata." & vbCrLf
                Stream.Write(UTF8.GetBytes("Si è connesso " & Me.Nickname.Text & "!"), 0, UTF8.GetBytes("Si è connesso " & Me.Nickname.Text & "!").Length)
            Else
                Me.TextBox2.Text = Me.TextBox2.Text & "Impossibile connettersi..." & vbCrLf
            End If
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            If client.Available > 0 Then
                Dim x(client.Available - 1) As Byte
                Stream.Read(x, 0, x.Length)
                Dim text As String = UTF8.GetString(x)
                TextBox2.Text = TextBox2.Text & text & vbCrLf
    
            End If
        End Sub
    
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            TextBox2.Text = "Conversazione" & vbCrLf
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Try
                'invio il messaggio di chat specificando il canale convertito in byte
                'Dim y() As Byte = UTF8.GetBytes(Foo(ChatM, Nickname.Text & ": " & TextBox1.Text))
                Dim y() As Byte = UTF8.GetBytes(Nickname.Text & ": " & TextBox1.Text)
                Stream.Write(y, 0, y.Length)
                TextBox2.Text = TextBox2.Text & Nickname.Text & ": " & TextBox1.Text & vbCrLf
                TextBox1.Text = Nothing
            Catch ex As NullReferenceException
                MsgBox("Non sei collegato con nessuno!")
            Catch ex As System.IO.IOException
                MsgBox("Il destinatario si è scollegato o ha conflitti di IP!")
            End Try
        End Sub
    
    
        Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick
            pView.Image = Touchless.CurrentCamera.GetCurrentImage
            Dim ss() As Byte = BmpToByte(pView.Image) 'converto frame bmp in byte
            Dim ChaCam() As Byte = UTF8.GetBytes(CamM) 'carico l'identificativo del canale nella varibile
            Stream.Write(ChaCam, 0, ChaCam.Length) 'invio ???
            Stream.Write(ss, 1, ss.Length) 'invio flusso dati della cam
        End Sub
    
        'funzione converti from BMP To Byte
        Public Shared Function BmpToByte(ByVal _bitmap As System.Drawing.Bitmap) As Byte()
            Dim _ms As New Global.System.IO.MemoryStream()
            _bitmap.Save(_ms, System.Drawing.Imaging.ImageFormat.Bmp)
            Dim _bmpArray As Byte()
            _bmpArray = _ms.ToArray()
            _ms.Flush()
            _ms.Dispose()
            Return _bmpArray
        End Function
        'Pass the channel and message to the subroutine
        Public Shared Function Foo(ByVal eChannel As String, ByVal iMessage As String)
            Dim eMessage As String
            eMessage = eChannel & iMessage
        End Function
    End Class
    qualcuno mi può dare una mano... grazie in anticipo...

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.