Ray

Ray

  • NA
  • 1
  • 0

Slow streaming TCP sockets

Mar 14 2009 4:51 PM
I wrote a server application that was using UDP to receive data. The server needs to respond very quickly to the incoming data and produce near-real-time output. Using UDP, this works just fine. However I need to switch to using TCP. But when I do so, there was a noticeable lag with the incoming traffic. I played around with the buffer size, but it didn't make a difference. All the rest of the code is the same, I'm just using TCP instead of UDP. Can anything think of any reason for the slow down?

I have a similar server that I wrote for Unix that uses straight up BSD sockets, and it has no noticeable difference when I switched to TCP, so it appears to have to do with .NET.

This is my code:

Private Function startServer() As Boolean

Try

listener = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)

listener.NoDelay = True

listener.Bind(New IPEndPoint(IPAddress.Any, localPort))

listener.Listen(10)

listener.BeginAccept(New AsyncCallback(AddressOf connectionRequest), listener)

Return True

Catch ex As Exception

MsgBox(ex.Message)

Return False

End Try

End Function

Private Sub connectionRequest(ByVal ar As IAsyncResult)

Dim listener As Socket = CType(ar.AsyncState, Socket)
Dim handler As Socket = listener.EndAccept(ar)

Dim remoteEndPoint As IPEndPoint

remoteEndPoint = handler.RemoteEndPoint

listener.Listen(10)

listener.BeginAccept(New AsyncCallback(AddressOf connectionRequest), listener)

Dim state As New StateObject
state.workSocket = handler

handler.BeginReceive(state.buffer, 0, StateObject.BufferSize, 0, New AsyncCallback(AddressOf dataArrival), state)

End Sub

Public Sub dataArrival(ByVal ar As IAsyncResult)

Dim state As StateObject = CType(ar.AsyncState, StateObject)
Dim handler As Socket = state.workSocket

' Read data from the client socket.
If (handler.Connected()) Then

Dim bytesRead As Integer = handler.EndReceive(ar)

If bytesRead > 0 Then

' Do stuff

End If

End If

End Sub