Hi,
I am trying to send/receive data from a socket.
The call to receive does not block .
If I use sleep between send and receive things are ok.
Here is the code -
Socket m_socClient;
//create a new client socket ...
m_socClient = new Socket(AddressFamily.InterNetwork, SocketType.Stream,
ProtocolType.Tcp);
String szIPSelected = m_strServerUri;
String szPort = "800";
int alPort = System.Convert.ToInt16 (szPort, 10);
System.Net.IPAddress remoteIPAddress = System.Net.IPAddress.Parse(szIPSelected);
System.Net.IPEndPoint remoteEndPoint = new
System.Net.IPEndPoint(remoteIPAddress, alPort);
m_socClient.Connect(remoteEndPoint);
string requestHeader = "POST / HTTP/1.1\r\nContent-Type: " +
"application/vnd.\r\n" +
"Content-Length: 1000" +
"\r\nConnection: Close\r\n\r\n";
Byte[] bytesHeader = Encoding.ASCII.GetBytes (requestHeader);
// Blocks until send returns.
int nBytes = 0;
nBytes = m_socClient.Send (bytesHeader , 0, bytesHeader .Length,
SocketFlags.None);
Console.WriteLine ("No. of bytes sent" + nBytes);
byte[] response = new byte[4000];
// Doesn't block until read returns.
m_socClient.Blocking = true;
nBytes = 0;
//*************** PROBLEM HERE ************////
while ( nBytes > 0 )
{
nBytes = m_socClient.Receive (response, response.Length, 0);
}
//********************************************
Console.WriteLine ("No. of bytes recvd." + nBytes);
Always nBytes is returning as 0. If I use sleep between send and receive things
work but that's not the proper way.
What is going wrong?
Best regards,
SK