By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,119 Members | 2,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,119 IT Pros & Developers. It's quick & easy.

Getting truncated data from sockets

P: n/a
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

Nov 28 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
<bi******@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
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/

--
Ken Halter - MS-MVP-VB (visiting from VB6 world) - http://www.vbsight.com
Please keep all discussions in the groups..
Nov 28 '05 #2

P: n/a
<bi******@gmail.com> schrieb im Newsbeitrag
news:11**********************@g44g2000cwa.googlegr oups.com...
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

Nov 28 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.