|
-
March 26th, 2006, 04:50 PM
#1
Does This Article Seem Accurate (.NET)??
The main CodeGuru page has a new article on it, "Communicating over Sockets: Blocking vs. Unblocking" by Mark Strawmyer at http://www.codeguru.com/columns/dotn...le.php/c11649/
Putting aside the incorrect usage of socket terminology (i.e., "unblocking" should be "non-blocking"), does the article give good advice and good program samples. I do not use .NET, so I am uncertain. But here's one sample of code that is said to read an XML-formatted request inside a thread of a threaded server:
Code:
private void ProcessConnection(object stats)
{
ConnectionInfo connection = (ConnectionInfo)stats;
byte[] buffer = new byte[4000];
System.Text.StringBuilder input =
new System.Text.StringBuilder();
try
{
while (true)
{
int bytesRead = connection.Socket.Receive(buffer);
if (bytesRead > 0)
{
input.Append(System.Text.Encoding.ASCII.
GetString(buffer));
// Parse the message
XmlDocument xmlInput = new XmlDocument();
xmlInput.LoadXml(input.ToString());
// Send back a preset response based on the type
XmlDocument xmlOutput = new XmlDocument();
if (xmlInput.DocumentElement.Name.Equals("uptime"))
{
xmlOutput.Load(@"C:\uptimeresponse.xml");
}
else
{
// Logic to generate desired exception here
}
connection.Socket.Send(System.Text.Encoding.
ASCII.GetBytes(
xmlOutput.DocumentElement.
OuterXml));
}
else if (bytesRead == 0) return;
}
}
catch (SocketException socketExec)
{
Console.WriteLine("Socket exception: " +
socketExec.SocketErrorCode);
}
catch (Exception exception)
{
Console.WriteLine("Exception: " + exception);
}
finally
{
connection.Socket.Close();
lock (this.connections)
this.connections.Remove(connection);
}
}
To me, this code will fail because it assumes that the call to Socket.Receive will receive a complete XML request, whereas it might not.
Comments?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|