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