Getting truncated data from sockets

B

bil.shah

Hi,

I am listening to a port for data but I am not able to recieve whole
data, I only get truncated data. Client sends me data that exceeds 40K
and the data I recieve in my callback function is always 8K to 9K. I
dont get the rest of the data. I think the data size is too big to come
in one go and hence it comes in 2-3 sub-packages but my call back
function only gets called once hence I am getting truncated data. I am
sending the code...Is there anyway I can recieve complete data sent by
client


Public Function ConnecttSocket(byVal iPort as integer)

Dim _sHostName As String

Dim _iLocalPort As Integer = iPort

Dim LocalIp() As IPAddress

Dim _oIPHostEntry As IPHostEntry

Dim IPLocalEndPoint As IPEndPoint

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

_sHostName = Dns.GetHostName()

_oIPHostEntry = Dns.GetHostByName(_sHostName)

LocalIp = _oIPHostEntry.AddressList()

IPLocalEndPoint = New IPEndPoint(LocalIp(0), _iLocalPort)

oListenerSocket.Bind(IPLocalEndPoint)

oListenerSocket.Listen(4)

oListenerSocket.BeginAccept(New AsyncCallback(AddressOf
onClientConnect), Nothing)

End Function

Private Sub onClientConnect(ByVal asyn As IAsyncResult)

Try

workerSocket = oListenerSocket.EndAccept(asyn)

SocketWaitForData(workerSocket)

oListenerSocket.BeginAccept(New AsyncCallback(AddressOf
onClientConnect), Nothing)

Catch ex As Exception

MessageBox.Show("onClientConnect " + ex.Message.ToString())

End Try

End Sub

Private Sub SocketWaitForData(ByVal soc As Socket)

Try

If SocketpfnCallBack Is Nothing Then

SocketpfnCallBack = New AsyncCallback(AddressOf SocketOnDataRcvd)

End If

result = soc.BeginReceive(SocketDataBuffer, 0, SocketDataBuffer.Length,
SocketFlags.None, SocketpfnCallBack, Nothing)

Catch ex As Exception

MessageBox.Show("SocketWaitForData " + ex.Message.ToString())

End Try

End Sub

Private Sub SocketOnDataRcvd(ByVal asyn As IAsyncResult)

Dim iRx As Integer = 0

Dim sSocketMessage As System.String



Try



iRx = workerSocket.EndReceive(asyn)


'iRx always shows 9K or 8K, I think this is the maximum capacity of
sockets to 'transfer data in one go

Dim chars(iRx + 1) As Char

Dim d As System.Text.Decoder = System.Text.Encoding.UTF8.GetDecoder()

Dim charlen As Integer = d.GetChars(SocketDataBuffer, 0, iRx, chars, 0)

sSocketMessage = New System.String(chars)

end sub
 
K

Ken Halter

Hi,

I am listening to a port for data but I am not able to recieve whole
data, I only get truncated data. Client sends me data that exceeds 40K

I can't help with the code (no .Net experience to share yet <g>) but I can
tell you that you can never base your app on the hopes of getting full
packets (serial or TCP/IP) so you always have to buffer the data.

fwiw, if full packets is all you ever want to deal with.... and are sure
that you can control blocking, etc, in a meaningful way, you can use
SocketWrench... their new version supports fixed sized transfers where both
sides wait until the entire packet is transferred. It's a bit tricky because
of the blocking and handshaking but it's possible. This functionality may be
in their freeware version as well so.....

http://www.catalyst.com/
 
A

Armin Zingler

Hi,

I am listening to a port for data but I am not able to recieve whole
data, I only get truncated data. Client sends me data that exceeds 40K
and the data I recieve in my callback function is always 8K to 9K. I
dont get the rest of the data. I think the data size is too big to come
in one go and hence it comes in 2-3 sub-packages but my call back
function only gets called once hence I am getting truncated data. I am
sending the code...Is there anyway I can recieve complete data sent by
client

I have never done it on my own, but in the documentation on the EndReceive
method, there is an example where BeginReceive is called again.


Armin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top