<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://forums.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>XML Web Services</title><link>http://forums.asp.net/28.aspx</link><description>All about building XML Web Services with ASP.NET - SOAP, WSDL, WCF, etc. &lt;a href="http://aspadvice.com/SignUp/list.aspx?l=66&amp;c=17" target="_blank"&gt;Email List&lt;/a&gt;</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Re: TcpServer acting strangely</title><link>http://forums.asp.net/thread/2347816.aspx</link><pubDate>Fri, 09 May 2008 10:24:28 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:2347816</guid><dc:creator>ParrotBoy</dc:creator><author>ParrotBoy</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/2347816.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=28&amp;PostID=2347816</wfw:commentRss><description>&lt;p&gt;Great stuff - thanks!&amp;nbsp;&lt;/p&gt;</description></item><item><title>Re: TcpServer acting strangely</title><link>http://forums.asp.net/thread/2347787.aspx</link><pubDate>Fri, 09 May 2008 10:10:33 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:2347787</guid><dc:creator>kamii47</dc:creator><author>kamii47</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/2347787.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=28&amp;PostID=2347787</wfw:commentRss><description>&lt;p&gt;IT would be better if you post this in Microsoft Forum [Network section] &lt;/p&gt;&lt;p&gt;&lt;a href="http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=40&amp;amp;SiteID=1" target="_blank"&gt;http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=40&amp;amp;SiteID=1&lt;/a&gt;&amp;nbsp;&lt;/p&gt;</description></item><item><title>TcpServer acting strangely</title><link>http://forums.asp.net/thread/2347734.aspx</link><pubDate>Fri, 09 May 2008 09:37:23 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:2347734</guid><dc:creator>ParrotBoy</dc:creator><author>ParrotBoy</author><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/2347734.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=28&amp;PostID=2347734</wfw:commentRss><description>&lt;p&gt;Hi everyone, apologies that this is not strictly a webservice question, it seemed like the forum that fit the most.&lt;/p&gt;&lt;p&gt;&amp;nbsp;I&amp;#39;m building a multi-threaded TcpServer class, which will take XML data over a certain port, process it and return a result. Here is my class (with non-relevant bits removed):&lt;/p&gt;&lt;pre class="coloredcode"&gt;&lt;span class="kwd"&gt;public class&lt;/span&gt; TcpListener
    {
        &lt;span class="kwd"&gt;private readonly&lt;/span&gt; System.Net.Sockets.TcpListener _TcpListener;
        &lt;span class="kwd"&gt;private readonly&lt;/span&gt; Thread _ListenThread;

        &lt;span class="kwd"&gt;public&lt;/span&gt; TcpListener(ILogWriter LogWriter)
        {
            &lt;span class="kwd"&gt;this&lt;/span&gt;.LogWriter = LogWriter;
            WriteToLog(10, &lt;span class="st"&gt;&amp;quot;TcpListener Initialized.&amp;quot;&lt;/span&gt;);
            _TcpListener = &lt;span class="kwd"&gt;new&lt;/span&gt; System.Net.Sockets.TcpListener(IPAddress.Any, 3000);
            _ListenThread = &lt;span class="kwd"&gt;new&lt;/span&gt; Thread(ListenForClients);
            _ListenThread.Start();
        }

        &lt;span class="cmt"&gt;// Public properties removed for clarity&lt;/span&gt;

        &lt;span class="kwd"&gt;public void&lt;/span&gt; Stop()
        {
            WriteToLog(10, &lt;span class="st"&gt;&amp;quot;TcpListener.Stop.&amp;quot;&lt;/span&gt;);
            _ListenThread.Abort();
            _TcpListener.Stop();
        }

        &lt;span class="cmt"&gt;/// &amp;lt;summary&amp;gt;
        ///     Blocks until a client has connected to the server. (Blocking means that the AcceptTcpClient method waits for data to be
        ///     sent before it returns.) Then creates a thread to handle communication with the connected client.
        /// &amp;lt;/summary&amp;gt;&lt;/span&gt;
        &lt;span class="kwd"&gt;private void&lt;/span&gt; ListenForClients()
        {
            WriteToLog(10, &lt;span class="st"&gt;&amp;quot;ListenForClients entered.&amp;quot;&lt;/span&gt;);
            &lt;span class="kwd"&gt;try&lt;/span&gt; 
            {
                _TcpListener.Start();
            }
            &lt;span class="kwd"&gt;catch&lt;/span&gt;(Exception e)
            {
                WriteToLog(5, &lt;span class="st"&gt;&amp;quot;ERROR Starting Inner Listener: &amp;quot;&lt;/span&gt; + e.Message);
            }
            WriteToLog(10, &lt;span class="st"&gt;&amp;quot;Started inner listener&amp;quot;&lt;/span&gt;);

            &lt;span class="kwd"&gt;while&lt;/span&gt;(&lt;span class="kwd"&gt;true&lt;/span&gt;)
            {
                WriteToLog(10, &lt;span class="st"&gt;&amp;quot;ListenForClients - loop starting.&amp;quot;&lt;/span&gt;);
                TcpClient objClient = _TcpListener.AcceptTcpClient();
                Thread objClientThread = &lt;span class="kwd"&gt;new&lt;/span&gt; Thread(HandleClientCommunication);
                WriteToLog(10, &lt;span class="st"&gt;&amp;quot;ListenForClients - about to start HandleClientCommunication thread.&amp;quot;&lt;/span&gt;);
                objClientThread.Start(objClient);
            }
        }

        &lt;span class="kwd"&gt;private void&lt;/span&gt; HandleClientCommunication(&lt;span class="kwd"&gt;object&lt;/span&gt; Client)
        {
            WriteToLog(10, &lt;span class="st"&gt;&amp;quot;HandleClientCommunication entered.&amp;quot;&lt;/span&gt;);

            WriteToLog(10, &lt;span class="st"&gt;&amp;quot;HandleClientCommunication entered a bit more.&amp;quot;&lt;/span&gt;);

            &lt;span class="kwd"&gt;try&lt;/span&gt;
            {
                &lt;span class="kwd"&gt;if&lt;/span&gt; (!(Client &lt;span class="kwd"&gt;is&lt;/span&gt; TcpClient))
                {
                    WriteToLog(5, &lt;span class="st"&gt;&amp;quot;HandleClientCommunication - Throwing ArgumentException - Client must be a TcpClient object&amp;quot;&lt;/span&gt;);
                    &lt;span class="kwd"&gt;throw new&lt;/span&gt; ArgumentException(&lt;span class="st"&gt;&amp;quot;Client must be a TcpClient object.&amp;quot;&lt;/span&gt;);
                }
                &lt;span class="kwd"&gt;else&lt;/span&gt;
                    WriteToLog(10, &lt;span class="st"&gt;&amp;quot;HandleClientCommunication - Client is of type &amp;quot;&lt;/span&gt; + Client.GetType() + &lt;span class="st"&gt;&amp;quot;, continuting processing&amp;quot;&lt;/span&gt;);
            }
            &lt;span class="kwd"&gt;catch&lt;/span&gt;
            {
                WriteToLog(5, &lt;span class="st"&gt;&amp;quot;HandleClientCommunication - ERROR OCCURED&amp;quot;&lt;/span&gt;);
                &lt;span class="kwd"&gt;return&lt;/span&gt;;
            }

            TcpClient objClient = (TcpClient) Client;
            NetworkStream objClientStream = objClient.GetStream();

            &lt;span class="kwd"&gt;byte&lt;/span&gt;[] bteMessage = &lt;span class="kwd"&gt;new byte&lt;/span&gt;[4096];

            &lt;span class="kwd"&gt;while&lt;/span&gt;(&lt;span class="kwd"&gt;true&lt;/span&gt;)
            {
                WriteToLog(10, &lt;span class="st"&gt;&amp;quot;HandleClientCommunication loop started.&amp;quot;&lt;/span&gt;);
                &lt;span class="kwd"&gt;int&lt;/span&gt; intBytesRead = 0;
                &lt;span class="kwd"&gt;string&lt;/span&gt; strResponse;

                &lt;span class="kwd"&gt;try&lt;/span&gt;
                {
                    &lt;span class="cmt"&gt;// Blocks until a client sends a message.&lt;/span&gt;
                    intBytesRead = objClientStream.Read(bteMessage, 0, 4096);
                    WriteToLog(10, &lt;span class="st"&gt;&amp;quot;HandleClientCommunication - found client message with length of &amp;quot;&lt;/span&gt; + intBytesRead + &lt;span class="st"&gt;&amp;quot;.&amp;quot;&lt;/span&gt;);
                }
                &lt;span class="kwd"&gt;catch&lt;/span&gt;
                {
                    WriteToLog(7, &lt;span class="st"&gt;&amp;quot;HandleClientCommunication - socket error.&amp;quot;&lt;/span&gt;);
                    &lt;span class="cmt"&gt;// A socket error has occured.&lt;/span&gt;
                    &lt;span class="kwd"&gt;break&lt;/span&gt;;
                }

                &lt;span class="cmt"&gt;// If 0 then the client has disconnected from the server.&lt;/span&gt;
                &lt;span class="kwd"&gt;if&lt;/span&gt; (intBytesRead == 0)
                {
                    WriteToLog(7, &lt;span class="st"&gt;&amp;quot;HandleClientCommunication - 0 bytes read error.&amp;quot;&lt;/span&gt;);
                    &lt;span class="kwd"&gt;break&lt;/span&gt;;
                }

                &lt;span class="cmt"&gt;// Message has successfully been recieved.&lt;/span&gt;
                ASCIIEncoding objEncoder = &lt;span class="kwd"&gt;new&lt;/span&gt; ASCIIEncoding();
                &lt;span class="kwd"&gt;string&lt;/span&gt; strRequestMessage = objEncoder.GetString(bteMessage, 0, intBytesRead);
                WriteToLog(10, &lt;span class="st"&gt;&amp;quot;HandleClientCommunication - recieved string &amp;#39;&amp;quot;&lt;/span&gt; + strRequestMessage + &lt;span class="st"&gt;&amp;quot;&amp;#39;.&amp;quot;&lt;/span&gt;);

                &lt;span class="cmt"&gt;// When this point is reached the XML is always processed properly and the test succeeds.
                // Actual business logic removed for clarity - this bit is definitely working!

                // Send the response back&lt;/span&gt;
                WriteToLog(10, &lt;span class="st"&gt;&amp;quot;HandleClientCommunication - returning response - &amp;quot;&lt;/span&gt; + strResponse);
                NetworkStream objReturnStream = objClient.GetStream();
                &lt;span class="kwd"&gt;byte&lt;/span&gt;[] bteResponse = objEncoder.GetBytes(strResponse);
                objReturnStream.Write(bteResponse, 0, bteResponse.Length);
                objReturnStream.Flush();
            }

            WriteToLog(10, &lt;span class="st"&gt;&amp;quot;HandleClientCommunication - closing client.&amp;quot;&lt;/span&gt;);
            objClient.Close();
        }
    }&lt;/pre&gt;&lt;p&gt;&amp;nbsp;&lt;br /&gt;&amp;nbsp;I&amp;#39;m testing this with a unit test named SendExampleRequest1, but the results are very unpredictable. My tests are under the NUnit framework, running in resharper&amp;#39;s client withing VS2005. Heres the test code:&lt;/p&gt;&lt;pre class="coloredcode"&gt;&lt;span class="kwd"&gt;string&lt;/span&gt; strIP = &lt;span class="st"&gt;&amp;quot;127.0.0.1&amp;quot;&lt;/span&gt;; &lt;span class="cmt"&gt;// Local address&lt;/span&gt;
OrderProcessingMiddleware.Listeners.TcpListener objListener = &lt;span class="kwd"&gt;null&lt;/span&gt;;

Console.WriteLine(&lt;span class="st"&gt;&amp;quot;*********************\nCONNECTING TO LISTENER ON LOCAL PC\n************************&amp;quot;&lt;/span&gt;);

&lt;span class="cmt"&gt;// Start the listener&lt;/span&gt;
objListener = &lt;span class="kwd"&gt;new&lt;/span&gt; OrderProcessingMiddleware.Listeners.TcpListener(&lt;span class="kwd"&gt;new&lt;/span&gt; SqlServerLogWriter());

Console.WriteLine(&lt;span class="st"&gt;&amp;quot;Creating client&amp;quot;&lt;/span&gt;);
TcpClient objClient = &lt;span class="kwd"&gt;new&lt;/span&gt; TcpClient();
IPEndPoint objEndPoint = &lt;span class="kwd"&gt;new&lt;/span&gt; IPEndPoint(IPAddress.Parse(strIP), 3000);
objClient.Connect(objEndPoint);
NetworkStream stmClient = objClient.GetStream();
Console.WriteLine(&lt;span class="st"&gt;&amp;quot;Client connected and stream obtained&amp;quot;&lt;/span&gt;);

ASCIIEncoding objEncoder = &lt;span class="kwd"&gt;new&lt;/span&gt; ASCIIEncoding();
&lt;span class="kwd"&gt;byte&lt;/span&gt;[] bteBuffer = objEncoder.GetBytes(RequestText);
stmClient.Write(bteBuffer, 0, bteBuffer.Length);
stmClient.Flush();
Console.WriteLine(&lt;span class="st"&gt;&amp;quot;Data written to stream&amp;quot;&lt;/span&gt;);

&lt;span class="kwd"&gt;byte&lt;/span&gt;[] bteResponse = &lt;span class="kwd"&gt;new byte&lt;/span&gt;[256];
&lt;span class="kwd"&gt;int&lt;/span&gt; intBytes = stmClient.Read(bteResponse, 0, bteResponse.Length);
&lt;span class="kwd"&gt;string&lt;/span&gt; strResponseData = objEncoder.GetString(bteResponse, 0, intBytes);
Console.WriteLine(&lt;span class="st"&gt;&amp;quot;Recieved: &amp;#39;&amp;quot;&lt;/span&gt; + strResponseData + &lt;span class="st"&gt;&amp;quot;&amp;#39;&amp;quot;&lt;/span&gt;);

Console.WriteLine(&lt;span class="st"&gt;&amp;quot;About to abort thread&amp;quot;&lt;/span&gt;);
stmClient.Close();

Console.WriteLine(&lt;span class="st"&gt;&amp;quot;Closing listener&amp;quot;&lt;/span&gt;);
objListener.Stop();
&lt;/pre&gt;&lt;p&gt;&amp;nbsp; All this is doing is checking that we get a response, any response, from the listener. I&amp;#39;m getting very erratic results, here are just the last few, they state the last WriteToLog item which worked (the logs themselves are in an SQL Server table):&lt;/p&gt;&lt;p&gt;13) SUCCESS! - Response returned - TCP client stopped!&lt;br /&gt;14) ListenForClients - loop starting.&lt;br /&gt;15) HandleClientCommunication entered.&lt;br /&gt;16) ListenForClients - about to start HandleClientCommunication thread&lt;br /&gt;17) HandleClientCommunication entered.&lt;br /&gt;18) ListenForClients - loop starting.&lt;br /&gt;&lt;br /&gt;## Restarted test environment to ensure that the TcpListener was closed&lt;br /&gt;&lt;br /&gt;19) ListenForClients - loop starting.&lt;br /&gt;&lt;br /&gt;## Added extra console write statements to unit test code for when a client is created, connected, and written to&lt;br /&gt;## Restarted test environment to ensure that the TcpListener was closed&lt;br /&gt;&lt;br /&gt;20) SUCCESS! - Response returned - TCP client stopped!&lt;br /&gt;21) ListenForClients - loop starting.&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Last Console comment: Data written to stream&lt;br /&gt;22) ListenForClients - loop starting.&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Last Console comment: Data written to stream&lt;br /&gt;&amp;nbsp;&lt;/p&gt;&lt;p&gt;I really don&amp;#39;t understand how it can seemingly randomly just stop working. For example - check out test 15 - the last log was &amp;quot;HandleClientCommunication entered&amp;quot;, however in the code the next statement which would be executed is simply another log of &amp;quot;HandleClientCommunication entered a bit more&amp;quot;, but it just stops instead of moving to the next statement!&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&amp;nbsp;I REALLY REALLY will appreciate any help anybody could give me. I have all the logs if it will help you out at all.&lt;br /&gt;&amp;nbsp;&lt;/p&gt;</description></item></channel></rss>