CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2002
    Location
    California
    Posts
    4,556

    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?

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: Does This Article Seem Accurate (.NET)??

    Quote Originally Posted by MikeAThon
    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.
    I agree. Another thing is that a 'partial' XML request might still be valid XML document. A document can contain Processing Instructions after the root element and according to the spec they MUST be preserved.

    - petter

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured