This project has moved. For the latest updates, please go here.

VB.NET - Buffer full TCP-Server

Jun 11, 2015 at 1:25 PM
Edited Jun 11, 2015 at 5:56 PM
Hey, i have a problem with TCP-Server.
I can send the input from microphone with a TCP-Client but the TCP-Server cannot play, because fast cames the error: "buffer full".
I have searched but i didn't found a solution and i have asked in two forums, one user recommended me to ask here.

Code:

TCP-Server:
Private Sub MainServer()
    TCP_Server.Start()  
    Dim bytes(1024) As Byte
    Dim Received_Message As String = Nothing
    While True
        Dim client As TcpClient = TCP_Server.AcceptTcpClient()
        Dim MicrosoftADPCM_Codec As INetworkChatCodec = New MicrosoftADPCM()
        Dim stream As NetworkStream = client.GetStream

        waveOut = New WaveOut()
        waveProvider = New BufferedWaveProvider(MicrosoftADPCM_Codec.RecordFormat)
        waveOut.Init(waveProvider)
        waveOut.Play()

        Dim i As Int32 = stream.Read(bytes, 0, bytes.Length)
        While (i <> 0)

            Dim decoded As Byte() = MicrosoftADPCM_Codec.Decode(bytes, 0, i)
            waveProvider.AddSamples(decoded, 0, decoded.Length)

        End While
    End While
    TCP_Server.Stop()
End Sub
TCP-Client:
    Dim TCP_Client As New TcpClient("IP", "PORT")
    Dim stream As Stream = TCP_Client.GetStream

    Call_Stream = stream
    Dim MicrosoftADPCM_Codec As INetworkChatCodec = New MicrosoftADPCM()
    selectedCodec = MicrosoftADPCM_Codec
    waveIn = New WaveIn()
    waveIn.BufferMilliseconds = 50
    waveIn.DeviceNumber = comboBoxInputDevices.SelectedIndex
    waveIn.WaveFormat = MicrosoftADPCM_Codec.RecordFormat

    AddHandler waveIn.DataAvailable, AddressOf waveIn_DataAvailable
    waveIn.StartRecording()
waveIn_DataAvailable:
Private Sub waveIn_DataAvailable(sender As Object, e As WaveInEventArgs)
    Dim encoded As Byte() = selectedCodec.Encode(e.Buffer, 0, e.BytesRecorded)
    Call_Stream.Write(encoded, 0, encoded.Length)
End Sub
Thank your for any replies.
Coordinator
Jun 12, 2015 at 4:54 PM
are you playing from the BufferedWaveprovider? If not it will just fill up and you'll get an exception as you are seeing here
Jun 16, 2015 at 3:29 PM
Hmm, that is all.
I think that:
waveOut.Play()
refresh the bytes and playing.

What should I do? How i play right?
Coordinator
Jun 17, 2015 at 8:06 PM
you can still over-fill the buffer if you are receiving data too fast, or maybe if you have got your recording formats wrong. To see a working example of what you are trying to do, take a look at the code for the network chat demo in the NAudio demo application
Jun 18, 2015 at 1:52 PM
This code is from NAudio Network Chat, only in VB.NET and as TCP.
Therefore i asked.
Coordinator
Jun 18, 2015 at 3:14 PM
well for the buffer to fill up you are receiving audio faster than you are playing it, which is odd.
So main ways you can fix that are
  1. increase the buffer size, maybe it's just a transient issue
  2. throw away received audio when the buffer is looking full
Jun 23, 2015 at 3:23 PM
Thank you very much for your patience.
It was a problem in the while loop, but now works.
Coordinator
Jun 23, 2015 at 3:26 PM
great, glad you have it working now