我意识到这可能被视为重复,但我查看了其他回复,他们没有为我解决问题。 我试过:禁用防火墙;使用random sleep()检查两个线程是否同时开始工作;我在vb中写了一个Windows应用程序视频聊天。当我运行我的程序时,此行出错:
r.Receive(br)
错误是:
远程主机强行关闭现有连接。
我的代码:
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