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

Socket Send/Receive only works once

P: n/a
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();
Nov 15 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
ki********@hotmail.com (Kitchen Bin) wrote:
"\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.
--
Abderaware
Fine Components For .NET
Turn on, tune in, download.
zane a@t abderaware.com
Nov 15 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.