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:
SERVER: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
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

Rispondi quotando