Socket::Receive() does not receive

P

Philipp Jacobi

Hello,

i am writing an application where a client communicates with a server using
TCP/IP and System::Net::Socket. I am using C++ .NET.
When the buffer which i will receive is greater than 8192 Byte
(Socket Standard Buffersize) and the client calls begin receive again, it
seems that Socket::Receive()
does not return. But when i stop the Thread with Thread::Sleep() before
calling Receive again, all works fine.
Here are 2 Code Snippets:
The first Example does not work when the buffer is greater than 8192 and the
second works ? !!!

Example 1: (Does not work)

if ( ( BytesReceived = this->ConnectionSocket->Receive(ReceiveBuffer,
ReceiveBuffer->Length, SocketFlags::None) ) > 0 )
{
// Decode String
sb->Append( enc->GetString(ReceiveBuffer, 0, BytesReceived) );
while ( this->ConnectionSocket->Available > 0 )
{
BytesReceived = this->ConnectionSocket->Receive(ReceiveBuffer,
ReceiveBuffer->Length, SocketFlags::None);
sb->Append(enc->GetString(ReceiveBuffer, 0, BytesReceived));
}
}

Example 2: ( Works fine )

if ( ( BytesReceived = this->ConnectionSocket->Receive(ReceiveBuffer,
ReceiveBuffer->Length, SocketFlags::None) ) > 0 )
{
// Decode String
sb->Append( enc->GetString(ReceiveBuffer, 0, BytesReceived) );
while ( this->ConnectionSocket->Available > 0 )
{
Thread::Sleep(10);
BytesReceived = this->ConnectionSocket->Receive(ReceiveBuffer,
ReceiveBuffer->Length, SocketFlags::None);
sb->Append(enc->GetString(ReceiveBuffer, 0, BytesReceived));
}
}

Any ideas about this behaviour ?

Thanks in Advance !

Philipp Jacobi
 
F

Feroze [MSFT]

To paraphrase your question, the second Receive() is not returning. This
could be due to the fact that the sender is not sending the data.

Is it that the Receive() blocks indefinitely, or does it return after some
time (even if you dont put a Thread.Sleep() before the Receive())?

It is difficult to determine the cause of the hang by looking at the code.
You should run a packet sniffer and verify if the data is coming to the
socket or not. If it is not coming, then the hang is bound to happen.

--
feroze
http://weblogs.asp.net/feroze_daud
============

Remove "user" from the email address to reply to the author.

This posting is provided "AS IS" with no warranties, and confers no rights

Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm
 
P

Philipp Jacobi

Hi,
To paraphrase your question, the second Receive() is not returning. This
could be due to the fact that the sender is not sending the data.
yes the second call to receive does not return and of course the sender
sends data.
Is it that the Receive() blocks indefinitely, or does it return after some
time (even if you dont put a Thread.Sleep() before the Receive())?
Yes it blocks indefinitely.

I've tried to step through the code with the help of the debugger and in
this case everything works fine. The Problem only occurs
if it runs very quickly (Without Thread::Sleep(10) ). To explain it a bit
more exactly:

1. Sender sends a message with i.e. 10000 Bytes.
2. Send method returns with Bytes send = 10000.
2. Receive receives 8192 Bytes and the second call blocks indefinetly.
3. Sender sends another message (for example 1 Minute later) with i.e. 155
Bytes.
4. Send method returns with Bytes send = 155 Bytes.
5. Receive receives 1963 Bytes ( The new message and the rest of the first
one ).

It seems to be a complex problem because it is impossible to determine what
is happening. To make it more difficult you could also
replace the Thread::Sleep() with a
Diagnostics::Debug::WriteLine(ReceivedMessage) and all works fine.
It is possible that the problem could only occur if the application is
running in JIT debugging mode ? Because when i run the application
directly without JIT debugging the problem (until now) does not appear.

Thanks in advance.

Philipp Jacobi
 

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