Socket locking up after inital use

G

Guest

Hi,

I am trying to get a webpage using a TcpSocket instead of a standard
Webrequest. Initial, it works fine but after the 2 or 3 request the
tcpclient I start to get the following error:

A connection attempt failed because the connected party did not properly
respond after a period of time, or established connection failed because
connected host has failed to respond

Here is the code making the call, this is the third iteration after trying
several others with out sucess:

string contents = "GET " + pathQuery + " HTTP/1.1\r\n" +
"Host: " + host + ":" + port + "\r\n" +
"User-Agent: Mozilla/5.0 (Windows; U; Windows
NT 6.0; en-US; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4\r\n" +
"Accept:
text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n" +
"Accept-Language: en-us,en;q=0.5\r\n" +
"Accept-Encoding: gzip,deflate\r\n" +
"Accept-Charset:
ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n" +
"Keep-Alive: 300\r\n" +
"Connection: keep-alive\r\n\r\n";

Byte[] requestObject = Encoding.ASCII.GetBytes(contents.ToCharArray());
try
{
stream = socket.GetStream();

if (stream.CanWrite)
{
stream.Write(requestObject, 0, contents.Length);
}

MemoryStream memoryStream = new MemoryStream();

if (stream.CanRead)
{
int data = stream.ReadByte();
while (data != -1)
{
memoryStream.WriteByte((byte) data);
data = stream.ReadByte();
}
results =
Encoding.ASCII.GetString(memoryStream.ToArray());
}

}
catch (Exception e)
{

Console.WriteLine(Encoding.ASCII.GetString(memoryStream.ToArray()));
throw e;
}
finally
{
stream.Close();
socket.Close();
}
 
P

Peter Duniho

I am trying to get a webpage using a TcpSocket instead of a standard
Webrequest. Initial, it works fine but after the 2 or 3 request the
tcpclient I start to get the following error:

A connection attempt failed because the connected party did not properly
respond after a period of time, or established connection failed because
connected host has failed to respond [...]

Some thoughts:

* You say you get an error trying to make the connection. But you
didn't bother to post any code related to creating or connecting your TCP
socket.

* As far as I know, there's no such thing as a TcpSocket, nor is there
a Socket.GetStream() method. You can create a Socket instance that
encapsulates a TCP socket, and you can create a NetworkStream instance
using a Socket instance. Is that what you did? If so, why does your post
and code say something else? And if not, what _did_ you do?

All that said, assuming your code normally works, you may be running into
some kind of anti-DoS defense, depending on how you're using the code. If
you are repeatedly trying to make the same request to the same HTTP
server, it may detect that case and stop responding, at least for some
time.

If you can post a concise-but-complete example of code, client _and_
server, that reliably reproduces the problem, someone here may be able to
provide better advice. Absent that, there's not much in your post to go
on, and in fact what you posted doesn't make much sense in the context of
regular .NET/C# programming (since you mention a class and a method that
are simply not present in the basic .NET Framework).

Pete
 
G

Guest

I am very sorry for the confusing. I was trying to be brief. Here is the
class, and I renamed the socket to client for easyier reading. I am using
TCPTrace at the moment to test this, and it gets and sends the proper
responses so I do not believe it is a Dos prevention of some kind.

using System;
using System.IO;
using System.Net.Sockets;
using System.Text;

namespace ReanneCorp.Library.V2.Common.WebUtils
{
public class PullWebsiteClass
{
private Uri websiteUri;
private TcpClient client = null;
private string results = "";
private int port;
private string host;
private string pathQuery;

public Uri WebsiteUri
{
get { return websiteUri; }
set { websiteUri = value; }
}

public TcpClient Client
{
get { return client; }
set { client = value; }
}

public NetworkStream Stream
{
get { return stream; }
set { stream = value; }
}

public string Results
{
get { return results; }
set { results = value; }
}


public PullWebsiteClass(Uri websiteUri)
{
this.websiteUri = websiteUri;
InitConnection();
}
private void InitConnection()
{
port = websiteUri.Port;
host = websiteUri.Host;
pathQuery = websiteUri.PathAndQuery;

client = new TcpClient();
client.ReceiveTimeout = 10000;
try
{
client.Connect(host, port);
}
catch(SocketException ex)
{
throw ex;
}
}
public void MakePageRequest()
{
string contents = "GET " + pathQuery + " HTTP/1.1\r\n" +
"Host: " + host + ":" + port + "\r\n" +
"User-Agent: Mozilla/5.0 (Windows; U; Windows
NT 6.0; en-US; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4\r\n" +
"Accept:
text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n" +
"Accept-Language: en-us,en;q=0.5\r\n" +
"Accept-Encoding: gzip,deflate\r\n" +
"Accept-Charset:
ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n" +
"Keep-Alive: 300\r\n" +
"Connection: keep-alive\r\n\r\n";
MemoryStream memoryStream = new MemoryStream();

Byte[] requestObject =
Encoding.ASCII.GetBytes(contents.ToCharArray());
try
{
stream = client.GetStream();

if (stream.CanWrite)
{
stream.Write(requestObject, 0, contents.Length);
}
if (stream.CanRead)
{
int data = stream.ReadByte();
while (data != -1)
{
memoryStream.WriteByte((byte) data);
data = stream.ReadByte();
}
results =
Encoding.ASCII.GetString(memoryStream.ToArray());
}

if (results.Contains("HTTP/1.1 302 Moved Temporarily"))
{
client.Close();
string redirect = results.Remove(0,
results.IndexOf("Location: ") + 10);


redirect = redirect.Substring(0,

redirect.IndexOf("Content-Length:") - 1).Trim();

PullWebsiteClass newRequest = new PullWebsiteClass(new
Uri(redirect));
newRequest.MakePageRequest();
results = newRequest.Results;
}
}
catch (Exception e)
{
throw e
}
finally
{
memoryStream.Close();
client.Close();
}
}
}
}


Here is the calling code:

[Test]
public void CheckHeaders()
{

PullWebsiteClass getPage = new PullWebsiteClass(new
Uri("http://localhost:8081/test.aspx"));
getPage.MakePageRequest();
string page = getPage.Results;
Console.WriteLine(page);
Assert.AreNotEqual(page.Length, 0);
}




Peter Duniho said:
I am trying to get a webpage using a TcpSocket instead of a standard
Webrequest. Initial, it works fine but after the 2 or 3 request the
tcpclient I start to get the following error:

A connection attempt failed because the connected party did not properly
respond after a period of time, or established connection failed because
connected host has failed to respond [...]

Some thoughts:

* You say you get an error trying to make the connection. But you
didn't bother to post any code related to creating or connecting your TCP
socket.

* As far as I know, there's no such thing as a TcpSocket, nor is there
a Socket.GetStream() method. You can create a Socket instance that
encapsulates a TCP socket, and you can create a NetworkStream instance
using a Socket instance. Is that what you did? If so, why does your post
and code say something else? And if not, what _did_ you do?

All that said, assuming your code normally works, you may be running into
some kind of anti-DoS defense, depending on how you're using the code. If
you are repeatedly trying to make the same request to the same HTTP
server, it may detect that case and stop responding, at least for some
time.

If you can post a concise-but-complete example of code, client _and_
server, that reliably reproduces the problem, someone here may be able to
provide better advice. Absent that, there's not much in your post to go
on, and in fact what you posted doesn't make much sense in the context of
regular .NET/C# programming (since you mention a class and a method that
are simply not present in the basic .NET Framework).

Pete
 
P

Peter Duniho

I am very sorry for the confusing. I was trying to be brief. Here is
the
class, and I renamed the socket to client for easyier reading. I am
using
TCPTrace at the moment to test this, and it gets and sends the proper
responses so I do not believe it is a Dos prevention of some kind.

Well, I don't see anything obvious. As I mentioned, you really need to
post a _complete_ sample of code that reliably reproduces the problem if
anyone is to actually try to look at your specific problem. You've only
posted the client side of things.

There do happen to be some odd things in your code, even without me
inspecting each line carefully (which I didn't do). Nothing that seems
like it would be causing the issue, but which are to me a sign of
less-than-perfect code:

-- When you write to the NetworkStream, you use the length of the
original string, not the byte array you're passing. In this case, they
should be the same, but there are potential situations in which they
wouldn't be. You should pass the length of the actual data you expect to
send, not some other length that is related but not necessarily identical.

-- You check the CanWrite and CanRead properties of the stream, but
don't do anything sensible if they aren't what you expect. One particular
issue is that if CanWrite is false but CanRead is true, you skip trying to
write but go ahead and try to read a response to something you never
sent. That makes no sense. Presumably, both CanWrite and CanRead are
true in this situation, so the oddity shouldn't affect you. But the code
is wrong, nevertheless.

-- You are catching exceptions that you then do nothing with other
than to throw them again. What's the point?

Have you tried not setting the timeout? It's not supposed to affect
connecting, but maybe it does, or maybe having a timeout on a previous
connection somehow interferes with the subsequent one. I admit, I'm just
making wild guesses here...I don't see anything obvious. Even more reason
for you to construct self-contained server/client sample code that
reliably reproduces the problem. Just looking at the code doesn't suggest
anything obvious, at least not to me.

Pete
 

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