Socket Send/Receive only works once

K

Kitchen Bin

Hi.
I am trying to use Sockets to do multiple Send and Receives via HTTP
(not simultaneously). A first pair of Send/Receives works fine and
sure enough I receive HTML back, but the next Send/Receive pair (even
if identically the same as the first pair) returns no data.

eg. The following simplified code will receive some bytes:
s.Send(ByteGet, ByteGet.Length, SocketFlags.None);
bytes = s.Receive(RecvBytes, RecvBytes.Length, 0); //
(snip...loop)

but running the same code immediately a second time returns nothing:
s.Send(ByteGet, ByteGet.Length, SocketFlags.None);
bytes = s.Receive(RecvBytes, RecvBytes.Length, 0); //
(snip...loop)
s.Send(ByteGet, ByteGet.Length, SocketFlags.None);
bytes = s.Receive(RecvBytes, RecvBytes.Length, 0); // Nothing
returned

Please can someone tell me what I'm doing wrong?


(please note the following C# code is merely to demonstrate the
problem)

Int32 bytes;
//Sets up variables and a string to write to the server
Encoding ASCII = Encoding.ASCII;
string Get = "GET " + "/" + " HTTP/1.1\r\nHost: " +
"www.hitquarters.com" +
"\r\nConnection: Close\r\n\r\n";
Byte[] ByteGet = ASCII.GetBytes(Get);
Byte[] RecvBytes = new Byte[256];
String strRetPage = null;

// IPAddress and IPEndPoint represent the endpoint that will
// receive the request.
// Get the first IPAddress in the list using DNS.
IPAddress hostadd =
Dns.Resolve("www.hitquarters.com").AddressList[0];
IPEndPoint EPhost = new IPEndPoint(hostadd, 80);
//Creates the Socket for sending data over TCP.
Socket s = new Socket(AddressFamily.InterNetwork,
SocketType.Stream,
ProtocolType.Tcp );

// Connects to the host using IPEndPoint.
s.Connect(EPhost);

// ---------------------------
// ---- Send/Receive once ----
// ---------------------------
// Sends the GET text to the host.
s.Send(ByteGet, ByteGet.Length, SocketFlags.None);
// Receives the page, looping until all bytes are received
do
{
bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0,
bytes);
} while (bytes > 0);


// *PROBLEM*
// *PROBLEM*
// *PROBLEM*
// *PROBLEM*
// ---------------------------
// ---- Now an identical Send/Receive a second time, but
// ---- this time nothing is received... Why?!
// ---------------------------
// Sends the GET text to the host.
s.Send(ByteGet, ByteGet.Length, SocketFlags.None);
// Receives the page, looping until all bytes are received.
do
{
// *PROBLEM* : We receive ZERO bytes
bytes = s.Receive(RecvBytes, RecvBytes.Length, 0);
strRetPage = strRetPage + ASCII.GetString(RecvBytes, 0,
bytes);
} while (bytes > 0);

s.Shutdown(SocketShutdown.Both);
s.Close();
 
Z

Zane Thomas [.NET/C# MVP]

"\r\nConnection: Close\r\n\r\n";

You will need to either Connect between each send/receive, our you can use the
Keep-Alive header:

Connection: Keep-Alive

Also, please be sure to check return values from Send and Receive, if you see a
zero the connection has been closed.
 

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