<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Program With Dave &#187; Dot Net</title>
	<atom:link href="http://www.daveandrews.org/category/dot-net/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.daveandrews.org</link>
	<description>My Programming Tutorials and Experiments</description>
	<lastBuildDate>Thu, 29 Jul 2010 06:00:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Sending an Email in C# When Server Uses GREET_PAUSE</title>
		<link>http://www.daveandrews.org/2010/02/19/sending-an-email-in-c-when-server-uses-greet_pause/</link>
		<comments>http://www.daveandrews.org/2010/02/19/sending-an-email-in-c-when-server-uses-greet_pause/#comments</comments>
		<pubDate>Fri, 19 Feb 2010 19:40:54 +0000</pubDate>
		<dc:creator>Dave Andrews</dc:creator>
				<category><![CDATA[CSharp]]></category>
		<category><![CDATA[Dot Net]]></category>

		<guid isPermaLink="false">http://www.daveandrews.org/?p=339</guid>
		<description><![CDATA[I wrote a process for a client which would send an email through their email server when a process completed. Their server did not require any kind of authentication or SSL, so the code to do this was very simple: 1 2 3 4 5 6 7 8 9 10 public static void SendEmail&#40;string to, [...]]]></description>
			<content:encoded><![CDATA[<p>I wrote a process for a client which would send an email through their email server when a process completed. Their server did not require any kind of authentication or SSL, so the code to do this was very simple:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> SendEmail<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> to, <span style="color: #FF0000;">string</span> from, <span style="color: #FF0000;">string</span> subject, 
                             <span style="color: #FF0000;">string</span> message, <span style="color: #FF0000;">string</span> host, <span style="color: #FF0000;">int</span> port<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    SmtpClient c <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SmtpClient<span style="color: #000000;">&#40;</span>host, port<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    MailMessage m <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MailMessage<span style="color: #000000;">&#40;</span>from, to, subject, message<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    m.<span style="color: #0000FF;">IsBodyHtml</span> <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span>
&nbsp;
    c.<span style="color: #0000FF;">Send</span><span style="color: #000000;">&#40;</span>m<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Simple, eh? But it would fail. If I telnet&#8217;ed into their server and typed the SMTP commands to send an email, I would get a 550 5.0.0 Command Rejected&#8221; error message at the point where I try to send a &#8220;RCPT TO:&#8221; SMTP command. What&#8217;s the deal?!</p>
<p>Well, apparently the server did have a restriction that I had so far not known about. It had been configured with a GREET_PAUSE value for all emails. Typically that setting should only be set for incoming emails from external servers, and not for internal emails like this. But it was set for all emails.</p>
<p>GREET_PAUSE means that once an SMTP connection is made, the client who is connecting must WAIT until the server recognizes their connection before &#8220;blasting&#8221; SMTP commands through. Apparently SMTPClient just blasts the commands through. If a SINGLE character is received in that timespan, the server will reply as normal, but will reject any and all emails that are sent through.</p>
<p>I tried a couple fancy ways to make SMTPClient wait for a few seconds before sending data, but could not find an easy way. Apparently it does not support this capability out of the box (such that I could find.) So I resolved to just writing the email code myself. It&#8217;s not hard to do.</p>
<p>I added a greet_delay_ms integer to my parameters on my function:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> SendEmail<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> to, <span style="color: #FF0000;">string</span> from, <span style="color: #FF0000;">string</span> subject, <span style="color: #FF0000;">string</span> message, 
                    <span style="color: #FF0000;">string</span> host, <span style="color: #FF0000;">int</span> port, <span style="color: #FF0000;">int</span> greet_delay_ms<span style="color: #000000;">&#41;</span></pre></td></tr></table></div>

<p>And replaced the simple SMTPClient code with this more complex code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> SendEmail<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> to, <span style="color: #FF0000;">string</span> from, <span style="color: #FF0000;">string</span> subject, <span style="color: #FF0000;">string</span> message, 
                       <span style="color: #FF0000;">string</span> host, <span style="color: #FF0000;">int</span> port, <span style="color: #FF0000;">int</span> greet_delay_ms<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    TcpClient c <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> TcpClient<span style="color: #000000;">&#40;</span>host, port<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    StreamWriter sw <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StreamWriter<span style="color: #000000;">&#40;</span>c.<span style="color: #0000FF;">GetStream</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    StreamReader sr <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StreamReader<span style="color: #000000;">&#40;</span>c.<span style="color: #0000FF;">GetStream</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #FF0000;">string</span> data <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// Wait the specified amount of time before sending an email</span>
    <span style="color: #008080; font-style: italic;">// could probably also just do a sr.ReadLine() and let it block</span>
    <span style="color: #000000;">System.<span style="color: #0000FF;">Threading</span></span>.<span style="color: #0000FF;">Thread</span>.<span style="color: #0000FF;">Sleep</span><span style="color: #000000;">&#40;</span>greet_delay_ms<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    sw.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;helo &quot;</span> <span style="color: #008000;">+</span> from.<span style="color: #0000FF;">Substring</span><span style="color: #000000;">&#40;</span>from.<span style="color: #0000FF;">IndexOf</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;@&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    sw.<span style="color: #0000FF;">Flush</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    data <span style="color: #008000;">=</span> sr.<span style="color: #0000FF;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    sw.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;MAIL FROM:&quot;</span> <span style="color: #008000;">+</span> from<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    sw.<span style="color: #0000FF;">Flush</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    data <span style="color: #008000;">=</span> sr.<span style="color: #0000FF;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    sw.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;RCPT TO:&quot;</span> <span style="color: #008000;">+</span> to<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    sw.<span style="color: #0000FF;">Flush</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    data <span style="color: #008000;">=</span> sr.<span style="color: #0000FF;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    sw.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;DATA&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    sw.<span style="color: #0000FF;">Flush</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    data <span style="color: #008000;">=</span> sr.<span style="color: #0000FF;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    sw.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Subject: &quot;</span> <span style="color: #008000;">+</span> subject<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    sw.<span style="color: #0000FF;">Flush</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    sw.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span>message<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    sw.<span style="color: #0000FF;">Flush</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    sw.<span style="color: #0000FF;">WriteLine</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;.&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    sw.<span style="color: #0000FF;">Flush</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    data <span style="color: #008000;">=</span> sr.<span style="color: #0000FF;">ReadLine</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    c.<span style="color: #0000FF;">Client</span>.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    c.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Once I passed in 8000 or so as the time to wait, the emails poured through just fine. 8 seconds is a while to wait, but apparently that&#8217;s what this server requires.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveandrews.org/2010/02/19/sending-an-email-in-c-when-server-uses-greet_pause/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Binding a Dropdown List in ASP.Net Inside a ListView</title>
		<link>http://www.daveandrews.org/2009/12/08/binding-a-dropdown-list-in-asp-net-inside-a-listview/</link>
		<comments>http://www.daveandrews.org/2009/12/08/binding-a-dropdown-list-in-asp-net-inside-a-listview/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 20:54:54 +0000</pubDate>
		<dc:creator>Dave Andrews</dc:creator>
				<category><![CDATA[CSharp]]></category>
		<category><![CDATA[Dot Net]]></category>

		<guid isPermaLink="false">http://www.daveandrews.org/?p=283</guid>
		<description><![CDATA[I like to blog things that I have to look up. Here&#8217;s a quickie which solves a problem I was having. In my table, I have a field &#8220;PayType&#8221; which is a char(1) and has valid values &#8220;S&#8221; and &#8220;H&#8221;. These mean &#8220;Salary&#8221; and &#8220;Hourly&#8221;, respectively. I am showing this in a ListView in my [...]]]></description>
			<content:encoded><![CDATA[<p>I like to blog things that I have to look up. Here&#8217;s a quickie which solves a problem I was having.</p>
<p>In my table, I have a field &#8220;PayType&#8221; which is a char(1) and has valid values &#8220;S&#8221; and &#8220;H&#8221;. These mean &#8220;Salary&#8221; and &#8220;Hourly&#8221;, respectively.</p>
<p>I am showing this in a ListView in my screen I&#8217;m developing. So, when editing the record, I naturally wanted a dropdown box where you can choose &#8220;Hourly&#8221; or &#8220;Salary&#8221;. Note this will only work if you know what values you want in your dropdown box. If the selections are dynamic, then you will probably have to have a databound event and build the box manually.</p>
<p>I wanted it to default to the selected type for the record I&#8217;m editing when I click on edit, but could not find anything in the intellisence that looked correct for my Bind() call.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;asp:DropDownList 
      ID=&quot;ddPayType&quot; 
      runat=&quot;server&quot; 
      SelectedValue=&lt;%# Bind(&quot;PayType&quot;) %&gt;  &gt;;
          &lt;asp:ListItem Text=&quot;Salary&quot; Value = &quot;S&quot; /&gt;
          &lt;asp:ListItem Text=&quot;Hourly&quot; Value = &quot;H&quot; /&gt;
&lt;/asp:DropDownList&gt;</pre></td></tr></table></div>

<p>The answer was to Bind on the &#8220;SelectedValue&#8221; parameter. I was thrown off because SelectedValue does not seem to pull up in Visual Studio 2008 as an option on the list when writing code in the markup editor. But it correctly updates the value when the Update command is executed.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveandrews.org/2009/12/08/binding-a-dropdown-list-in-asp-net-inside-a-listview/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Chat Client/Server Part 3: The Connection Class</title>
		<link>http://www.daveandrews.org/2009/11/24/chat-clientserver-part-3-the-connection-class/</link>
		<comments>http://www.daveandrews.org/2009/11/24/chat-clientserver-part-3-the-connection-class/#comments</comments>
		<pubDate>Wed, 25 Nov 2009 02:05:42 +0000</pubDate>
		<dc:creator>Dave Andrews</dc:creator>
				<category><![CDATA[CSharp]]></category>
		<category><![CDATA[Dot Net]]></category>

		<guid isPermaLink="false">http://www.daveandrews.org/?p=142</guid>
		<description><![CDATA[If you have not read Part 1 and Part 2 of this network tutorial series, please do so now. The code in this post will build upon code from those posts. The Connection Class: Beginning Network Code We are going to get into the fun part of the network code in this part of the [...]]]></description>
			<content:encoded><![CDATA[<p>If you have not read <a href="http://www.daveandrews.org/2009/09/14/chat-clientserver-part-1-network-programming-primer-and-packet-base-class/">Part 1</a> and <a href="http://www.daveandrews.org/2009/09/27/chat-clientserver-part-2-packet-extension-and-server/">Part 2</a> of this network tutorial series, please do so now. The code in this post will build upon code from those posts.</p>
<h3>The Connection Class: Beginning Network Code</h3>
<p>We are going to get into the fun part of the network code in this part of the series. This post will teach you how to use TcpClient to send and receive data over the network. TcpClient is a class in Microsoft Dot Net which instantiates a TCP connection and can send and receive data in the form of byte arrays.</p>
<p><strong>Step 1: Create our connection class.</strong> Right-click on our Chat class library and add a new class. Call that class &#8220;Connection&#8221;.  This class will use our Packet class to send and receive data.</p>
<p><strong>Step 2: Include the correct using statements.</strong> We need to include certain dot net classes to make our coding job easier. Add these using statements to the top of Connection.cs.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Net.Sockets</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Net</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>The TcpClient class we are going to use is included by using these statements. This will make our coding easier since we don&#8217;t have to fully qualify each time we use the TcpClient class.</p>
<p><strong>Step 3: Instantiate our TcpClient object.</strong> Inside of your Connection class, add the following line of code which will create our TcpClient class. By creating one here, it will be instantiated with our Connection object and always be available.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Connection
    <span style="color: #000000;">&#123;</span>
        TcpClient c <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> TcpClient<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>We now have a client object named &#8220;c&#8221; to work with in our network code.</p>
<p><strong>Step 4: Constructors.</strong> Let&#8217;s create a couple of constructors for our connection class. One constructor should take a TcpClient object as a parameter, so that it can accept one that&#8217;s already created. Another constructor should just take a server to connect to and the port to connect to, and open the connection.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">        <span style="color: #0600FF;">public</span> Connection<span style="color: #000000;">&#40;</span>TcpClient client<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            c <span style="color: #008000;">=</span> client<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span>
&nbsp;
        <span style="color: #0600FF;">public</span> Connection<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> server, <span style="color: #FF0000;">int</span> port<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            c.<span style="color: #0000FF;">Connect</span><span style="color: #000000;">&#40;</span>server, port<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>As you can see from the second constructor, opening a TCP connection to a given server with a given port is very easy to do. The call to Connect is a &#8220;blocking&#8221; operation, which as you should remember from Part 1 of this series means it will wait until the connection has been created successfully before continuing with any further code.</p>
<p>If the connection fails for any reason, an exception will be thrown. Any client code written using the Connection class should be able to handle that exception. As a matter of fact, every function in this class could possibly throw an exception, so your client code should always wrap network calls in a try/catch statement.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveandrews.org/2009/11/24/chat-clientserver-part-3-the-connection-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Chat Client/Server Part 2: Packet Extension Class</title>
		<link>http://www.daveandrews.org/2009/09/27/chat-clientserver-part-2-packet-extension-and-server/</link>
		<comments>http://www.daveandrews.org/2009/09/27/chat-clientserver-part-2-packet-extension-and-server/#comments</comments>
		<pubDate>Mon, 28 Sep 2009 03:19:39 +0000</pubDate>
		<dc:creator>Dave Andrews</dc:creator>
				<category><![CDATA[CSharp]]></category>
		<category><![CDATA[Dot Net]]></category>

		<guid isPermaLink="false">http://www.daveandrews.org/?p=91</guid>
		<description><![CDATA[                                               ]]></description>
			<content:encoded><![CDATA[<p>If you have not read <a href="http://www.daveandrews.org/2009/09/14/chat-clientserver-part-1-network-programming-primer-and-packet-base-class/">Part 1 of this Multi-Post tutorial</a>, check it out now. This tutorial will rely on the code written in that tutorial.</p>
<p>This is the second installment of a multi-post tutorial on writing network software using the dot net framework and the C# programming language. In this installment we will be going over the ChatPacket class which implements the Packet abstract class.</p>
<h3>The ChatPacket Class</h3>
<p>If you recall, our Packet class had three important elements.</p>
<ol>
<li><strong>Type Code</strong>: This is an integer value which identifies the type of packet. It is transmitted across the connection for every packet, so that the receiving end knows how to interpret the packet it is receiving.</li>
<li><strong>Converts to Byte Array</strong>: Every packet which inherits from our base packet class should be able to convert itself into a byte array.</li>
<li><strong>Parses a Byte Array</strong>: The packet classes which inherit from the base class should implement a parse function which will take a byte array and read it into the proper packet structure.</li>
</ol>
<p>These capabilities should be implemented by any class which inherits from Packet, so that the library stays consistent.</p>
<p>Let&#8217;s begin by creating a new class. Right-click on your Chat class library and add a new class called ChatPacket.cs. This file will be our main class for sending chat data back and forth.</p>
<p><strong>Step 1: Inherit from Packet base class.</strong> Every packet class we create should inherit from the base Packet class. This is because the server and client code we will be using will use the base class in all of its operations.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> ChatPacket <span style="color: #008000;">:</span> Packet</pre></td></tr></table></div>

<p><strong>Step 2: Create the chat data property.</strong> Chat data is going to be just a string in our chat packet class. Let&#8217;s add a new property to our ChatPacket class.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> ChatData <span style="color: #000000;">&#123;</span> get<span style="color: #008000;">;</span> set<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Chatdata is going to basically be the text of each chat packet that gets sent around. When you type in text to send out to the server (and the server sends it to the other clients) then this is the data that gets sent out.</p>
<p><strong>Step 3: Constructor should set the Type of Packet.</strong> The next thing that must be done is that each packet must identify its type. This is so that the end point which receives every packet needs to know how to decode it into the correct class. In our constructor for each packet, we are going to tell the packet which type it is.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">        <span style="color: #0600FF;">public</span> ChatPacket<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> 
        <span style="color: #000000;">&#123;</span>
            _packetTypeCode <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Keep in mind that the type code should be unique to this type of packet. If we created another packet type in this application, its typecode should be set to 2 for each instance. ChatPacket is going to be packet type 1. </p>
<p>We are only going to send around ChatPackets, but it would be a good idea in a more complex program to use constants to make sure your packet types are consistent integer values.</p>
<p><strong>Step 4: Implement ToByteArray() method.</strong> This is the method that will be called by our server to convert a packet into a stream of bytes. The network code will then send the bytes of the network, and the code at the other end will reconstruct the bytes into a packet.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> ToByteArray<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0600FF;">return</span> Encoding.<span style="color: #0000FF;">ASCII</span>.<span style="color: #0000FF;">GetBytes</span><span style="color: #000000;">&#40;</span>ChatData<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>The above code uses the built-in ASCII encoding capabilities of dot net to convert our string of packet data into an array of bytes which can be transmitted.</p>
<p><strong>Step 5: Implement the Parse operation which reconstructs packets.</strong> The Parse function takes as input a series of bytes and should return the reconstructed packet object.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">        <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #008000;">new</span> ChatPacket Parse<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> packetData<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            ChatPacket p <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> ChatPacket<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">if</span><span style="color: #000000;">&#40;</span>packetData.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span>
                p.<span style="color: #0000FF;">ChatData</span> <span style="color: #008000;">=</span> Encoding.<span style="color: #0000FF;">ASCII</span>.<span style="color: #0000FF;">GetString</span><span style="color: #000000;">&#40;</span>packetData<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
            <span style="color: #0600FF;">return</span> p<span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, the above function is fairly simple. It just does the opposite of the ToByteArray function, in that it uses the built-in ASCII Encoding classes to construct a string from bytes rather than deconstruct a string into bytes.</p>
<p>It returns a new packet object.</p>
<h3>Completing the ChatPacket Class</h3>
<p>So what we have done here is to create a class which will our packet class. ChatPacket is a very simple packet class, because the only data that is sent in the packet is the text of the chat message. It is a good idea to keep your packets very simple, so they can be easily and quickly packaged and processed.</p>
<p>In the next portion of this tutorial series, we are going to move on to implement the Server class and program, which is where this all gets interesting!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveandrews.org/2009/09/27/chat-clientserver-part-2-packet-extension-and-server/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Chat Client/Server Part 1: Network Programming Primer and Packet Base Class</title>
		<link>http://www.daveandrews.org/2009/09/14/chat-clientserver-part-1-network-programming-primer-and-packet-base-class/</link>
		<comments>http://www.daveandrews.org/2009/09/14/chat-clientserver-part-1-network-programming-primer-and-packet-base-class/#comments</comments>
		<pubDate>Tue, 15 Sep 2009 03:46:17 +0000</pubDate>
		<dc:creator>Dave Andrews</dc:creator>
				<category><![CDATA[CSharp]]></category>
		<category><![CDATA[Dot Net]]></category>

		<guid isPermaLink="false">http://www.daveandrews.org/?p=55</guid>
		<description><![CDATA[This is the first part of a multiple-post tutorial which will explain how to use the dot net TcpClient and TcpListener classes to create a multi-threaded network application. This result will manifest itself in a chat server and chat client. Basics of Networking The full methodologies of network programming are a beyond the scope of [...]]]></description>
			<content:encoded><![CDATA[<p>This is the first part of a multiple-post tutorial which will explain how to use the dot net TcpClient and TcpListener classes to create a multi-threaded network application. This result will manifest itself in a chat server and chat client.<br />
<span id="more-55"></span></p>
<h2>Basics of Networking</h2>
<p>The full methodologies of network programming are a beyond the scope of this article, so I will just give a very brief and elementary overview of how network programming works. We will be writing our chat server and client using the TcpClient and TcpServer classes which are built into System.Net in the dot net framework. These classes themselves are built on top of System.Net.Sockets, which wraps the socket structure provided by the operating system.</p>
<p>A &#8220;socket&#8221; is the basic connector in network communication. A socket can send and receive data, and a socket can listen for incoming connections. Think of an electrical wall socket in your home. It is sitting there, waiting for a connection. We call waiting for a connection &#8220;listening.&#8221; When you plug in a vacuum cleaner, the socket comes alive and provides electricity (the metaphor for network data) to the device. Plugging in the device is called &#8220;connecting&#8221; to the socket. When you unplug the vacuum cleaner, the flow is interrupted and the vacuum cleaner no longer receives power. This unplugging is called &#8220;disconnecting.&#8221;</p>
<p>To put the above into technical terms, here is the flow of a socket&#8217;s use.</p>
<ol>
<li>The socket sits, listening for an incoming connection.</li>
<li>When a connection is made, the socket is capable of sending and receiving data to the connected device.</li>
<li>The client disconnects from the socket.</li>
<li>The socket again begins listening for incoming connections.</li>
</ol>
<p>The above is fine and dandy if you will ever only have a single vacuum cleaner. But what happens, as often does in the world of networking, when you have many devices that want to connect to the same server? The most basic answer is to create many sockets. That is the answer to the problem, but there is a catch to it which makes it a little more complicated. That catch is called &#8220;ports.&#8221; A port is a number in which your socket will be listening to. Only one socket can listen to any port on a server at any given time. If another socket attempts to listen on the same port, the operating system will throw an error. You may have heard of &#8220;port 80&#8243; for website traffic, or &#8220;port 21&#8243; for file transfer traffic. These are the numbers on which the server listens for incoming clients. It would not work to create many sockets listening on many ports, because your clients would never know what ports were already being used by other clients and what ports were not. So, how do you overcome this problem?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.daveandrews.org/2009/09/14/chat-clientserver-part-1-network-programming-primer-and-packet-base-class/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Convert Byte Array to Hexidecimal String in C#</title>
		<link>http://www.daveandrews.org/2009/09/10/convert-byte-array-to-hexidecimal-string-in-c/</link>
		<comments>http://www.daveandrews.org/2009/09/10/convert-byte-array-to-hexidecimal-string-in-c/#comments</comments>
		<pubDate>Thu, 10 Sep 2009 18:40:23 +0000</pubDate>
		<dc:creator>Dave Andrews</dc:creator>
				<category><![CDATA[CSharp]]></category>
		<category><![CDATA[Dot Net]]></category>

		<guid isPermaLink="false">http://www.daveandrews.org/?p=63</guid>
		<description><![CDATA[I had to write a function today which will take an arrayof bytes and convert those bytes into a hexidecimal string representation. This was part of an encryption algorithm. There are a couple on the net but they seem to be more complicated. Here is what I came up with (and this seems the simplest [...]]]></description>
			<content:encoded><![CDATA[<p>I had to write a function today which will take an arrayof bytes and convert those bytes into a hexidecimal string representation. This was part of an encryption algorithm. There are a couple on the net but they seem to be more complicated. Here is what I came up with (and this seems the simplest way to me, so there may be similar ones out there):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre class="csharp" style="font-family:monospace;">        <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">string</span> ToHexString<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> array<span style="color: #000000;">&#41;</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #FF0000;">string</span> vals <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;0123456789ABCDEF&quot;</span><span style="color: #008000;">;</span>
            StringBuilder result <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringBuilder<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> array.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                result.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span>vals<span style="color: #000000;">&#91;</span>array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #008000;">&gt;&gt;</span> <span style="color: #FF0000;">4</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
                result.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#40;</span>vals<span style="color: #000000;">&#91;</span>array<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #008000;">&amp;</span> <span style="color: #FF0000;">15</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #000000;">&#125;</span>
&nbsp;
            <span style="color: #0600FF;">return</span> result.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.daveandrews.org/2009/09/10/convert-byte-array-to-hexidecimal-string-in-c/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
