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();
%>

See if this changes anything

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

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

try this then above the while loop

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

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!

so that timeout didn't correct the issue?

possibly try changing the

ReceiveBufferSize

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!

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

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!

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();
%>

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));
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.