Socket Blocked on Socket.Receive

  • Thread starter Thread starter Qindong Zhang
  • Start date Start date
Q

Qindong Zhang

My socket application blocked at socket.receiver() after received all
information from sender. Should socket.Receive() return 0 after no
more data available? Note: My socket object was not close on both
client/server side. My cached for further use.

While bytesReceived > 0
byteBuffer = New Byte(1024) {}
bytesReceived = handler.Receive(byteBuffer)
If bytesReceived > 0 Then
ReDim Preserve m_data(bytesTotalReceived + bytesReceived - 1)
Array.Copy(byteBuffer, 0, m_data, bytesTotalReceived,
bytesReceived)
bytesTotalReceived = bytesTotalReceived + bytesReceived
End If
End While
 
My socket application blocked at socket.receiver() after received all
information from sender. Should socket.Receive() return 0 after no
more data available? Note: My socket object was not close on both
client/server side. My cached for further use.

While bytesReceived > 0
byteBuffer = New Byte(1024) {}
bytesReceived = handler.Receive(byteBuffer)
If bytesReceived > 0 Then
ReDim Preserve m_data(bytesTotalReceived + bytesReceived - 1)
Array.Copy(byteBuffer, 0, m_data, bytesTotalReceived,
bytesReceived)
bytesTotalReceived = bytesTotalReceived + bytesReceived
End If
End While

Socket.Receive is a blocking call. If there is no data in the pipe, it
will wait. It returns 0 if the remote endpoint shuts down.

Basically, you need to have a transmision protocol defined between the
client and server so that you know how much data to read. Commonly, if
the data is varying lengths the stream will be delimited with a control
sequence - such as CR+LF - but that is not a requirement of TCP, that is
an application layer function. Then when you read from the socket, you can
check for the presence of this sequence to determine if you have reached
the end of the current transmission.
 
if i need to break my blocked receive call to do a send (which is an
event for button click) and then go back again on a blocked receive,
how would i do that ?
thanks
 

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

Back
Top