Question TCP socket.BeginReceive error?

groadsvb

Well-known member
Joined
Nov 13, 2006
Messages
75
Programming Experience
Beginner
I am trying to write a TCP server application that I can use to test a tcp client. Receiving the connection works but the code to begin receive fails with the error below. All the examples that I found pass a byte array in just like this.

{"Unable to cast object of type 'System.Byte[]' to type 'System.Collections.Generic.IList`1[System.ArraySegment`1[System.Byte]]'."}

I think this is all you need to understand what I am doing. The error occurs on the BeginReceive method call.
VB.NET:
Private _recvBuffer(1024) As Byte

  Dim s As System.Net.Sockets.Socket = _connections(0)
.
.
  s.BeginReceive(_recvBuffer, SocketFlags.None, New AsyncCallback(AddressOf EndRead), s)
 
The BeginReceive method is overloaded and you are trying to mix and match. Your last three arguments are OK but your first is not. If you want to pass a Byte array then it needs to be followed by two Integers, specifying the offset and size of the part of the array you want to use If you want to use the whole array then that would be:
s.BeginReceive(_recvBuffer, 0, _recvBuffer.Length, SocketFlags.None, New AsyncCallback(AddressOf EndRead), s)
 
Back
Top