473,408 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Socket locking up after inital use

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(memoryS tream.ToArray()));
throw e;
}
finally
{
stream.Close();
socket.Close();
}

Jun 29 '07 #1
3 3064
On Fri, 29 Jun 2007 13:22:05 -0700, Pain and headache <Pain and
<he******@discussions.microsoft.com>wrote:
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
Jun 29 '07 #2
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" wrote:
On Fri, 29 Jun 2007 13:22:05 -0700, Pain and headache <Pain and
<he******@discussions.microsoft.com>wrote:
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
Jun 29 '07 #3
On Fri, 29 Jun 2007 14:02:03 -0700, Pain and headache
<Pa*************@discussions.microsoft.comwrote:
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
Jun 29 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jane Austine | last post by:
Running Python 2.3 on Win XP It seems like socket is working interdependently with subprocesses of the process which created socket. ------------------------------------ #the server side >>>...
1
by: Joe | last post by:
Hi, I browsed the news and a few seem to have this problem too, but no solution ! I have a client server application where in case the connection gets bad (crashes or whatever) client or...
4
by: zbcong | last post by:
Hello: I write a multithread c# socket server,it is a winform application,there is a richtextbox control and button,when the button is click,the server begin to listen the socket port,waiting for a...
5
by: John Sheppard | last post by:
Hi all, I am not sure that I am posting this in the right group but here it goes anyway. I am new to socket programming and I have been searching on the internet to the questions I am about to pose...
4
by: Chris Tanger | last post by:
Context: C# System.Net.Sockets Socket created with constructor prarmeters Internetwork, Stream and TCP everything else is left at the default parameters and options except linger may be changed...
15
by: z. f. | last post by:
Hi, i have an ASP.NET project that is using a (Class Library Project) VB.NET DLL. for some reason after running some pages on the web server, and trying to compile the Class Library DLL, it...
6
by: Rik | last post by:
Hello Experts, I have a communication server in VB.NET. It was working fine from last 6 months, but now start giving error message like that. 21-03-2005 07:58:27...
4
by: Macca | last post by:
I am writing an application that uses asynchronous sockets to get data over ethernet from embedded devices, up to 30 concurrent devices.(These devices are written in C). My application...
2
by: kodart | last post by:
Introduction Performance is the main concern to most server application developers. That’s why many of them anticipate using .NET platform to develop high performance server application regardless...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.