Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I realise this could be seen as a duplicate but i have looked at the other responses and they didn't fix the problem for me. I have tried: Disable firewall; use random sleep() for Checking that two threads dont start to working at the same time; I wrote a windows application video chat in vb. When i ran my program, there is an error at this line:
r.Receive(br)

The error is:
An existing connection was forcibly closed by the remote host.
my code:
Imports System.Net.Sockets
Imports System.Threading
Imports TouchlessLib
Imports System.Net
Imports Voice

Public Class Form1

Private m_RecBuffer As Byte()
Private m_Player As WaveOutPlayer
Private m_Recorder As WaveInRecorder
Private m_PlayBuffer As Byte()
Dim m_Fifo As New FifoStream()
Dim r As New Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp)
Private t As New Thread(New ThreadStart(AddressOf Voice_In))
#Region "Voice_In()"
Private Sub Voice_In()
    Dim br As Byte()
    r.Bind(New IPEndPoint(IPAddress.Any, 0))
    While True
        br = New Byte(16383) {}
        r.Receive(br)
        m_Fifo.Write(br, 0, br.Length)
    End While
End Sub
#End Region
#Region "Voice_Out()"

Private Sub Voice_Out(data As IntPtr, size As Integer)
    'for Recorder
    If m_RecBuffer Is Nothing OrElse m_RecBuffer.Length < size Then
        m_RecBuffer = New Byte(size - 1) {}
    End If
    System.Runtime.InteropServices.Marshal.Copy(data, m_RecBuffer, 0, size)
    'Microphone ==> data ==> m_RecBuffer ==> m_Fifo
     Dim ip As IPAddress = IPAddress.Parse("192.168.0.105")
    Dim RemoteIpEndPoint As New IPEndPoint(ip, 0)
    r.SendTo(m_RecBuffer, RemoteIpEndPoint)
End Sub

#End Region
'UDP Defines
Dim subscriber As New UdpClient()
Dim publisher As New UdpClient()
Dim mycomputername As String = Environment.MachineName
Dim mycomputerIP() As System.Net.IPAddress = System.Net.Dns.GetHostAddresses(mycomputername)
'Webcam Defines
Dim Touchless As New TouchlessLib.TouchlessMgr
Dim Camera1 As TouchlessLib.Camera = Touchless.Cameras.ElementAt(0)
Dim picsize As Size = New Size(600, 500)

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    publisher.Client.Blocking = False
    subscriber.Client.ReceiveTimeout = 100
    subscriber.Client.Blocking = False
    subscriber.ExclusiveAddressUse = False
    publisher.ExclusiveAddressUse = False
    'TextBox1.Text = Environment.MachineName

    Label2.Text = "My Name: " & Environment.MachineName

    Touchless.CurrentCamera = Camera1
    Touchless.CurrentCamera.CaptureWidth = picsize.Width
    Touchless.CurrentCamera.CaptureHeight = picsize.Height

End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

    'Send
    Try

        Dim bitmapz As Bitmap = New Bitmap(picsize.Width, picsize.Height)
        bitmapz = Touchless.CurrentCamera.GetCurrentImage
        PictureBox1.Image = bitmapz
        Dim sendbytes(0) As Byte
        bytesfromimage(sendbytes, bitmapz)
        publisher.Send(sendbytes, sendbytes.Length)
    Catch ex As Exception
    End Try

    'Retrieve
    Try
        Dim ep As System.Net.IPEndPoint = New System.Net.IPEndPoint(System.Net.IPAddress.Any, 0)
        Dim rcvbytes() As Byte = subscriber.Receive(ep)
        Dim bitmapz As Bitmap = New Bitmap(picsize.Width, picsize.Height)
        imagefrombytes(rcvbytes, bitmapz)
        PictureBox2.Image = bitmapz

    Catch ex As Exception
    End Try

End Sub

Private Sub imagefrombytes(ByRef bytez() As Byte, ByRef piccolor As Bitmap)
    Dim rect As New Rectangle(0, 0, piccolor.Width, piccolor.Height)
    Dim bmpData As System.Drawing.Imaging.BitmapData = piccolor.LockBits(rect, _
        Drawing.Imaging.ImageLockMode.ReadWrite, Imaging.PixelFormat.Format32bppRgb)
    Dim ptr As IntPtr = bmpData.Scan0
    Dim bytes As Integer = bmpData.Stride * piccolor.Height
    Dim rgbValues(bytes - 1) As Byte
    System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes)

    Dim secondcounter As Integer
    Dim tempred As Integer
    Dim tempblue As Integer
    Dim tempgreen As Integer
    Dim tempalpha As Integer
    secondcounter = 0

    While secondcounter < rgbValues.Length
        tempblue = rgbValues(secondcounter)
        tempgreen = rgbValues(secondcounter + 1)
        tempred = rgbValues(secondcounter + 2)
        tempalpha = rgbValues(secondcounter + 3)
        tempalpha = 255

        tempred = bytez(((secondcounter * 0.25) * 3) + 0)
        tempgreen = bytez(((secondcounter * 0.25) * 3) + 1)
        tempblue = bytez(((secondcounter * 0.25) * 3) + 2)

        rgbValues(secondcounter) = tempblue
        rgbValues(secondcounter + 1) = tempgreen
        rgbValues(secondcounter + 2) = tempred
        rgbValues(secondcounter + 3) = tempalpha

        secondcounter = secondcounter + 4
    End While


    System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes)

    piccolor.UnlockBits(bmpData)

End Sub

Private Sub bytesfromimage(ByRef bytez() As Byte, ByRef piccolor As Bitmap)
    Dim rect As New Rectangle(0, 0, piccolor.Width, piccolor.Height)
    Dim bmpData As System.Drawing.Imaging.BitmapData = piccolor.LockBits(rect, _
        Drawing.Imaging.ImageLockMode.ReadWrite, Imaging.PixelFormat.Format32bppRgb)
    Dim ptr As IntPtr = bmpData.Scan0
    Dim bytes As Integer = bmpData.Stride * piccolor.Height
    Dim rgbValues(bytes - 1) As Byte
    System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValues, 0, bytes)

    Dim secondcounter As Integer
    Dim tempred As Integer
    Dim tempblue As Integer
    Dim tempgreen As Integer
    Dim tempalpha As Integer
    secondcounter = 0
    Dim bytelist As List(Of Byte) = New List(Of Byte)

    While secondcounter < rgbValues.Length
        tempblue = rgbValues(secondcounter)
        tempgreen = rgbValues(secondcounter + 1)
        tempred = rgbValues(secondcounter + 2)
        tempalpha = rgbValues(secondcounter + 3)
        tempalpha = 255

        bytelist.Add(tempred)
        bytelist.Add(tempgreen)
        bytelist.Add(tempblue)

        rgbValues(secondcounter) = tempblue
        rgbValues(secondcounter + 1) = tempgreen
        rgbValues(secondcounter + 2) = tempred
        rgbValues(secondcounter + 3) = tempalpha

        secondcounter = secondcounter + 4
    End While


    System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes)

    piccolor.UnlockBits(bmpData)

    Dim bytearray(bytelist.Count - 1) As Byte
    For i = 0 To bytelist.Count - 1
        bytearray(i) = bytelist(i)
    Next
    bytez = bytearray

End Sub
Private Sub Filler(data As IntPtr, size As Integer)
    If m_PlayBuffer Is Nothing OrElse m_PlayBuffer.Length < size Then
        m_PlayBuffer = New Byte(size - 1) {}
    End If
    If m_Fifo.Length >= size Then
        m_Fifo.Read(m_PlayBuffer, 0, size)
    Else
        For i As Integer = 0 To m_PlayBuffer.Length - 1
            m_PlayBuffer(i) = 0
        Next
    End If
    System.Runtime.InteropServices.Marshal.Copy(m_PlayBuffer, 0, data, size)
    ' m_Fifo ==> m_PlayBuffer==> data ==> Speakers
End Sub
'Update Button

Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
    t.Start()
    publisher.Connect(TextBox1.Text, 2013)
    subscriber.Client.Bind(New Net.IPEndPoint(Net.IPAddress.Any, 2013))
    Dim fmt As New WaveFormat(44100, 16, 2)
    m_Player = New WaveOutPlayer(-1, fmt, 16384, 3, New BufferFillEventHandler(AddressOf Filler))
    m_Recorder = New WaveInRecorder(-1, fmt, 16384, 3, New BufferDoneEventHandler(AddressOf Voice_Out))

End Sub
End Class
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900