Slow streaming TCP sockets

  • Thread starter Thread starter hurricane_number_one
  • Start date Start date
H

hurricane_number_one

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 when I switched to TCP it works just as fine, as
fast as UDP, so it appears to have to do with .NET. I'm using
asynchronous TCP sockets.

Here 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)

handler.noDelay=True

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
 
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?

By it's very nature TCP has more overhead than UDP. TCP has mechanisms for
ensuring all the packets are arrived and reassembled in the proper order. If
packets are lost they will be resent. UDP, on the other hand, just fires and
forgets.

You will not be able to get TCP to have the same speed as UDP. If you have
to use TCP, I recommend looking at your communications protocol and
shrinking it as much as possible. You might try one or more of the
following: Group multiple messages in to a single message, remove redundant
or unnecessary messages, shrink message size by replacing commands with an
integer or an abbreviation, compress messages before sending them, etc.

Good luck,

Andrew Faust
 
Back
Top