0

Hi All

I have following code running on Vista Business under IIS7. It works, but is incredibly slow (about 20+ seconds). I have the same code in Java and it's instant.

It appears to be the line
"int bytesRec = sender.Receive(bytes);" that is the killer

I'm very new to ASP .NET so it might be a noob error.

Many thanks

<%@ Page Language="C#" Debug="false" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.Net.Sockets" %>
<%@ Import Namespace="System.Text" %>
<%
	Response.AppendHeader("Content-Type", "text/xml");
	Response.AppendHeader("ContentEncoding", "UTF-8");
	String query = "?action=query&text=" + Request.QueryString["query"];
	
	IPHostEntry ipHostInfo = Dns.GetHostByAddress("192.168.0.101"); //Dns.GetHostByName
	IPAddress ipAddress = ipHostInfo.AddressList[0];
	IPEndPoint remoteEP = new IPEndPoint(ipAddress, 9000);
	
	byte[] bytes = new byte[1024];
	Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
	//sender.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 20000); //20 second timeout
	sender.Connect(remoteEP);
    
    byte[] msg = Encoding.UTF8.GetBytes(query);
    
    try{
    	int bytesSent = sender.Send(msg);
    	int bytesRec = sender.Receive(bytes); //skip the first line which contains server headers
    	while (true) {
			bytes = new byte[1024];
			bytesRec = sender.Receive(bytes);
			Response.Write(Encoding.UTF8.GetString(bytes,0,bytesRec));
			if(bytesRec <= 0){
				break;
			}
		}

    } catch(Exception){
    	Response.Write("Timed Out");
    }
    sender.Shutdown(SocketShutdown.Both);
    sender.Close();
%>
2
Contributors
10
Replies
12
Views
9 Years
Discussion Span
Last Post by dickersonka
0

See if this changes anything

while ((bytesRec = sender.Receive(bytes)) > 0) 
{
   Response.Write(Encoding.UTF8.GetString(bytes,0,bytesRec));
}
0

That cleans up the code a bit - but no speed improvement though.

0

try this then above the while loop

//Milliseconds wait time for timeout
send.ReceiveTimeout = 1000;

Yup it times out as expected, the call is taking around 25 seconds. The server says it delivered the (XML) data in 27ms!

0

so that timeout didn't correct the issue?

possibly try changing the

ReceiveBufferSize

Tried that also tried

sender.ReceiveBufferSize = 8192;
    	sender.NoDelay = true;

But no change, very frustrating isn't it!

0

also since this is an asp.net app, try running it as a regular windows or console app first, see if the problem still exists

Compiled and ran it from the console - still the same speed. Also tried it from a 2003 server with exactly the same results. This is a real puzzler!

0

Looks like sockets are a bust, it's funny how everytime I try .net it has something wrong with it. I found the following works, although it's not ideal. I would still love to solve the sockets issue.

<%@ Page Language="C#" Debug="false" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Text" %>
<%
	Response.AppendHeader("Content-Type", "text/xml");
	Response.AppendHeader("ContentEncoding", "UTF-8");
	
	String query = "/action=query&text=" + Request.QueryString["query"];
	
	WebRequest request = WebRequest.Create("http://192.168.0.101:9000" + query);
    WebResponse response = request.GetResponse();
    StreamReader input = new StreamReader(response.GetResponseStream());
    Response.Write(input.ReadToEnd());
    input.Close();
%>
0

have you tried with network stream instead?

NetworkStream netStream = new NetworkStream(sender);

//...other code
while ((bytesRec = netStream.Read(bytes, 0, bytes.Length)) > 0) 
{
 Response.Write(Encoding.UTF8.GetString(bytes,0,bytesRec));
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.