Tips and Tricks for Becoming a Contractor – Part 2

Your Personal Life Will Change

It may seem like it goes without saying, but you may not realize what, and to what extent, things will change in your life by becoming a contractor.

Freedom to make your own hours, be your own boss, set your own pay rate, all of these things are true to some extent, and at the same time very wrong. You have control and freedom, but at the same time you are a slave to your customer(s), at least if you want to be successful.

Note: Some of the sections below may relate to certain types of contracting or situations, when that is the case they will have the situation explained in the title.

Demeanor

It may be obvious to some of you that when you are your own salesman you have to put your best foot forward every second of every minute that you are interacting with your customers, and for you out there that know this, you are either a salesperson for a living or have been a contractor for years already.

As a contractor, you can’t have a bad day. Sure, some days if you aren’t feeling up to the task you can call in like any other job, but some days you just have to be present when you don’t want to be. You may be saying to yourself, “I have to go into work on days I don’t want to already!”, and that’s true, but while your co-workers at your company, that you are a salaried employee of, may be more tolerant of your poor moods, a Customer will not be as forgiving.

Keep in mind that a W2 employee costs money to fire or lay off, but a contract for and individual contractor can usually be cancelled easily with few, if any, repercussions.

You may be wondering how this is going to affect your personal life. It won’t at first. You’ll be elated that you are “your own boss” and high on life for a few months. Then, over time all those bad days that you can’t be a snarky know-it-all to your boss or co-workers will add up, and pretty soon you’ll be taking it out on your friends and family.

So, make sure to set aside time for yourself to blow off steam. Make sure you stick to your workout, or paintball on the weekends, or hiking that pretty mountain trail. You will need time for yourself to blow off the steam that you can’t with your co-workers anymore.

Hygiene

Depending on the type of contracting, and the situation you are in, your Hygiene will likely improve or decline, but it will almost certainly do one of the two. Be prepared.

Say for example that you are a remote contractor, lot more of these now than years before. This could be a steady gig or even if you are drumming up your own short term business. Being remote and working from home tends to make you forget that you haven’t showered in a week, especially when you are working 12-16 hour days trying to keep your customers happy and the money flowing in.

On the other hand, say you are a contractor who works in the customer’s office. Well I’ll tell you what, you are going to want to look your absolute sharpest every single day because, why? That’s right, you are your own sales team. Get used to it.

No matter what situation you are put into as a contractor, you should be prepared for changes in your daily hygiene. As a contractor, you will likely never be in that sweet spot of comfort and professionalism that is encapsulated by a typical office, so expect changes.

For those that aren’t going to be forced by their customers to shower and look sharp, this can be hard to deal with.

The best way to cope with poor hygiene situations is to develop a daily working schedule that includes your hygiene needs, and stick to it, come hell or high water.

Health

Let’s face it, computer people are not typically the healthiest or fit people. We all know it, but for a remote contractor it could be even worse.

As burdensome as it feels, few people realize how getting out of the house and going to the office each day has a positive impact on us.

In addition to a little exercise, face to face interaction with other human beings is good for us. Sitting in your home office from sun up to midnight cranking away on your projects can be great for your pocket book, but terrible for your waistline and your mental health.

If you happen to be working in your customer’s office you don’t have to worry as much about this, but if you are a contractor long enough you will inevitably be faced with a work from home situation.

Some of the best ways to combat the negative effects from work from home situations is to set a work schedule like you would have in an office, and try to stick to it. Also, working from a coffee shop or a shared workspace is a great way to get a little social interaction. Set yourself a timer to get up and move around every hour or two, and even better, get a standing desk for your home office.

I am no doctor, but we have all heard that exercise improves your mental health, as well as social interaction, so if you aren’t careful this could be a negative spiral into depression and poor physical health if you don’t make yourself get up from your desk from time to time.

If you don’t plan to take care of yourself, this can have negative effects on your friends and family as well, not just you.

You Will Need to Know a lot More About Businesses Than you do now!

In Part 3 we’ll discuss some of the business related issues that are going to come up.

Tips and Tricks for Becoming a Contractor – Part 1

“Hi” to all you developer’s out there that are thinking of shedding the chains of a W2 and forging your own path to fame and fortune!

I’d like to help, well, at least with shedding the W2! You’ll have to find your own path to fame and fortune.

I am writing this article to help developer’s, or possibly anyone looking to become a contractor. With some of the steps, business and personal, necessary to transition. I certainly don’t have all the answers but I have learned a few things in the years I’ve been a contractor.

Finding Work

The first thing you have to do is figure out how you are going to get work. Find your own contracts? Get contracts from a contracting hub? Know a friend that can hook you up? All viable choices.

There are many companies that offer full time contracting positions, and typically these offer more stability but without the chance for “the big pay off”.

The alternative is temporary contracting gigs. The hard part about these is finding them. These tend to offer higher pay rates for expertise in a specific domain. You can find these types of jobs on job boards sometimes, but for the biggest pay offs you typically need to know someone that can set you up with a client. Best people to know for these kinds of connections are development managers and Architects within larger organizations.

My personal recommendation on this one is to find stable long term contracting work first in order to grow your connections, but don’t stay put, move from contract to contract as you find them to have maximum exposure. For the long term contracting work, you’ll likely want to stay for at least a year, just like with W2 employees, organizations hiring for long term contractors don’t like to have high turn over rates. Eventually you’ll start seeing better opportunities through your network.

Keeping the Work

As a contractor you have to realize something, you are your own sales and engineering teams. You have to have a “customer first” attitude. I have seen many developers become contractors and not have the appropriate attitude for it.

The Basement Dweller

If you are the kind of developer that wants to sit in the back corner working odd hours, not talking to anyone, and hissing at the light when someone opens the window, well then you are going to have a problem.

Contracting is more about working with people than it is writing code. You have to be present when the customer needs you, you have to have a cheery demeanor when you are speaking with your customers, and most importunately you have to show interest and enthusiasm for your(their) work.

The Critic

I can’t remember how many times I have been on a project and seen other contractors with the same organization whining and complaining about how terrible the customer’s code base was, and how terrible of a process they had, and how annoying it all was. They tend to last between one and four weeks before their contracts are terminated, depending on how nice the customer is.

By the same token, if you are missing every other meeting, and delivering code late (or not at all), you will have your contract terminated.

It amazes me how many developers think that contracting is a free pass to have all the worst habits that any developer can have, and feel that the customer should still be groveling at their feet.

Customer First

The customer is #1! You have to make them feel warm and fuzzy any time they see your face. That’s not to say you can’t point out where things are going wrong, you absolutely should, but you have to have tact while doing so. They don’t just want to hear what’s broken, they also want to hear how it can be fixed, and how things are going to be peaches and rainbows in the end.

Most importantly, you have to be able to give the customer the information that they need, and then let them decide what to do with it. Don’t keep pointing out their flaws to them over and over, they got it, they don’t need their faces rubbed in it.

Your Personal Life will Change

In Part 2 we will discuss some of the changes you can expect in your personal life, and some strategies for dealing with those changes.

Fingernail and Toenail Maintenance

Keeping your phalanges well groomed is a very important thing if you study a martial art.  You don’t want to be the guy who just scraped the crap out of your training partner because you didn’t feel like spending a few minutes to trim.  But even more than martial arts, finger nail grooming especially,  can have a benefit in you career and how people perceive you in life.

 

Nobody likes a slob!

Imagine you’re reaching out to shake the hand of your company’s CEO and you stab his pointer finger with one of those claws you forgot to cut! Pretty embarrassing right?  Imagine somebody mistakes you, a hard core dedicated code monkey fueled on adrenaline, dubstep, and Mt Dew for the receptionist because of the constant tap, tap, tap of those unkempt nails on the keyboard.

Whats more, longer finger and toe nails can be a health concern. According to the CDC, dirty unkempt nails “… can contribute to the spread of some infections, such as pinworms.”.  Additionally, poor nail hygiene can lead to fungal infections. The cracks and crevices of your nails is an ideal place for anaerobic fungi to thrive.

So, not only is it inconvenient, dirty and viewed poorly by others, it can also be a health concern for you and others.

 

Good Habits

Cutting and trimming alone is not sufficient “good nail care” you also need to scrub under your nails regularly, removing dirt and debris that may accumulate there. Before trimming wash all your nail care tools to sterilize them in case something is still living on them from the last grooming session.

Aside from looks and diseases, good nail grooming habits can help to protect the health of your nails themselves. The Mayo Clinic reference link below outlines several conditions that can occur do to poop nail hygiene, including color changes, thinning or thickening, curling and deformations among other things.

So next time your thinking of skipping that nail trim and scrub do us all(yourself included) a favor and don’t!

 

References:

http://www.cdc.gov/healthywater/hygiene/hand/nail_hygiene.html

http://www.hygieneexpert.co.uk/nailcare.html

http://www.mayoclinic.org/healthy-lifestyle/adult-health/in-depth/nails/art-20044954

ADFS Environment Buildup with WCF Utilization – Part 3

Welcome back! This is part 3, and we are going to jump right in by configuring the security certificates on our identity server for the ADFS and IIS roles. If you’ve been following along as I’ve been publishing this multi-part article, I apologize for the delay between part 2 and 3, I had a hardware failure that delayed my writing.

 

Create a Certificate

If you are following along using a local network and VM machines then creating your own self-signed security certificates is likely going to be sufficient. This is what I will be doing for this tutorial.

However, for a production system you will likely want to use professionally created and signed certificates for your domain and server. Now is the time to get them and install them.

So we start by creating a certificate. Open up Powershell ISE once again and run the following script with your domain information.

New-SelfSignedCertificate -DnsName fs.martialdeveloper.local -CertStoreLocation Cert:\LocalMachine\My

The Thumbprint and the Subject will be displayed for you on the console after creation. Make a note of these two fields for future reference. Shown below.

create new cert

 

Bind the Certificate

Now we are going to hope over to IIS Manager by opening the server manger window, right clicking on the Tools menu and selecting “Internet Information Services (IIS) Manager” as shown below.

iis manager selection

Once the IIS Manager window comes up, we will navigate to the Default Wed Site, right click, and select the “Edit Bindings” option, as shown below.

IIS edit binding default website

On the window that pops up select “Add”, and configure the new binding for HTTPS and the security certificate you just created, like shown.

https binding

 

Create the ADFS Service

Now we are going to set up the ADFS service. Go to the Server Manager window, right click Tools, and select “AD FS Management”.

ADFS Manager selection

When the ADFS Management window comes up, click the configuration wizard link.

ADFS config wizard

Click “Next” on the first screen that appears, then select the “Stand-alone federation server” as shown below.

ADFS stand alone

For large production environments you may want to create a federation server farm, but for this tutorial we only need one.

Click next then verify the certificate that is auto populated is the one you created earlier, and then click Next three more times.

 

Create the Application Server Security Certificate

Now we have to jump back over to our application server and configure it for use.

Lets start by creating a security certificate for it. You have two options, either use the same PowerShell ISE command string as you did on the identity server(with application server specific substitutes), or navigate to the IIS Manager and create the certificate through the IIS GUI.  I’m going to show the IIS Manager approach since the PowerShell command has already been demonstrated.

Open the IIS Manager through the Tools menu of the Server Manager on the Application server.

IIS Manager Open

Now navigate the the local server and click the “Server Certificates” icon at the bottom of the list of options in the center of the GUI.

Server Certificates

Now we are going to create a self signed security certificate by clicking the link on the right side of the Server Certificates panel in the IIS Manager.

Create Self-Signed Cert

Now type in a friendly name for the security certificate. I use a similar name to the one created for the identity server.

Certificate Name

Click next and the certificate will appear in the security certificates list on the previous screen. Next right click on the newly created certificate and select the view option.

View Certificate

On the screen that pops up navigate to the Details tab and click the Copy to File button towards the bottom right.

Export Certificate

Now, on the screen that pops up click Next three times, then give the exported certificate file an easy to use and remember name and export location. Then click next.

Certificate Export Name

Click Next, Finish and Okay to complete the certificate export. You can now close the IIS Manager. Now we are going to copy the newly exported certificate to the identity server. Once this is done, we will use it to configure a relying part with the ADFS server.

In Part 4 we will continue by configuring the Identity server’s DNS roll with resource records to allow IPv4 lookups for our two servers, and then the we will configure the Identity server’s ADFS roll with a relying part pointing to our application server.

If you have any questions or comments about the above article please comment below!

 

References:

See references from Part 2.

 

ADFS Environment Buildup with WCF Utilization – Part 2

In Part 2 we will continue configuring the Identity server starting with installing the Active Directory Domain Services role.

 

Active Directory Domain Services

Begin by opening the Server Manager panel if it is not already. Click on Manage -> Add Roles and Features.

Continue through the wizard until you get to the role selection window and select the Active Directory Domain Services role shown below.

ADDS role selection

Add the additional features that automatically pop up and click “Next”. Continue clicking next until you reach the “Install” page, select the “Restart the Destination server automatically if required” check box, and click “Install”.

 

Static IP

Before continuing configure your Identity Server with a static IPv4 address, and disable IPv6. Since I am on a local network, my static IP is an internal IP only, but this is sufficient for this tutorial.

 

DNS Server and Domain Controller

 

The next things we are going to do are to, promote the identity server to a domain controller, install the DNS Server role onto the identity server, and to create a forest on the DNS Server with forward and reverse lookup zones for our local domain that we will be using for this tutorial. This next step is one good reason to use Virtual Machines if you are performing these configurations strictly as a learning exercise since you would have to have a dedicated domain if the machines were not on a private network.

Start by clicking the notification icon that has appeared since ADDS was added, shown below.

Promote domian controller notification

Next click the “Promote this server to a domain controller” link in the notifications drop down.

promote to DC link

In the wizard that comes up select the “Add a new forest” radio button shown below. Then type in the domain that you will be using.

add new forest domain name

Click next, then select the forest functional levels you want. I leave these at Server 2012 since I don’t plan on interacting with older DNS servers. Then enter a password for the DSRM, I use the admin password for this so it’s easy to remember for the demo but you will likely want a unique password for a production system.

Add new forest

Click “Next”. You will be warned about DNS Delegation options. For this demo on a local network this is not important, for a production environment, creating this delegation should be your next step.

Delegation to parent DNS server

Click OK and Next. Then on the next page click Install. Wait till the installation has finished to proceed.

Once the Identity server has completed it will then be a Domain Controller for martialdeveloper.local, and have the DNS Server role installed and integrated with Active Directory for the domain we created.

Application server IIS

While you are waiting for the Identity server to finish this installation you can start installing IIS on the Application server. Refer back to Part 1 of this series of articles if you forgot how to do this.

 

Configure Active Directory

We will continue by creating Organizational Units in Active Directory. We will be doing this in PowerShell. Please see Stefan Severin’s blog in the references section for the source of these commands. I am very happy to have found them on his blog!

Start by running Powershell ISE in administrative mode, and setting the execution policy to RemoteSigned.

Set-ExecutionPolicy RemoteSigned

Now run the following script to set the Organizational Units. Making sure to replace the first line with one appropriate for the domain you are using.

$domain = "dc=martialdeveloper,dc=local"; 
$ouAdmin = "ou=Adfs Administration"; 
$ouServiceAccounts = "ou=Service Accounts"; 
$ldapConnection = [ADSI] "LDAP://$domain"; 
$newAdminOu = $ldapConnection.Create("OrganizationalUnit", $ouAdmin); 
$newAdminOu.SetInfo(); 
Set-ADOrganizationalUnit "$ouAdmin,$domain" -ProtectedFromAccidentalDeletion $True; 
$ouAdminPath = [ADSI]($newAdminOu.path) 
$newAccountOu = $ouAdminPath.Create("OrganizationalUnit", $ouServiceAccounts); 
$newAccountOu.SetInfo(); 
Set-ADOrganizationalUnit "$ouServiceAccounts,$ouAdmin,$domain" -ProtectedFromAccidentalDeletion $True;

Now create the service account for the ADFS farm. Make sure to enter your domain specifics on line 4. Also, since this is a domain controller the strong password requirements are enabled, so get creative unless you want to have to recreate the account a few times when it fails on the password strength.

$dnsRoot = (Get-ADDomain).dnsroot 
$pwd = read-host "Enter strong password" -AsSecureString 
$upn = "SVC-ADFS" + "@" + $dnsRoot 
New-ADUser –Name "SVC-ADFS" –SamAccountName SVC-ADFS –DisplayName SVC-ADFS -Description "Service account for ADFS farm" -userprincipalname $upn -Path "OU=Service Accounts,OU=ADFS Administration,DC=martialdeveloper,DC=local" –Enabled $true –ChangePasswordAtLogon $False -PasswordNeverExpires $true -AccountPassword $pwd

Configure the SPN (Service Principle Name) for the ADFS service account. Making sure to remember to replace the hostname, and domain information for your environment.

setspn -S host/WIN-924FNOTSB0B.martialdeveloper.local martialdeveloper.local\svc-adfs

Now we install the ADFS Feature onto our identity server machine.

Install-WindowsFeature AD-Federation-Services

We now have ADFS installed, our domain is configured, and we have Active Directory setup. When we continue in Part 3, we will begin by configuring our security certificates for ADFS and IIS.

 

References:

As always, please have a look at my references for further information on the topics discussed above.

Adding Additional Domain Controller (Windows Server 2012)

http://blogs.msdn.com/b/stseverin/archive/2012/12/29/deploying-adfs-2-1-on-windows-server-2012-using-powershell.aspx

ADFS Environment Buildup with WCF Utilization – Part 1

Before I get into it, I wanted to give a little back ground about what this series of articles is, and what caused them to be written.

A while back I had a project come up that needed to use some WCF services for data sharing across multiple device types, and multiple domains.  Given that there were going to be potentially dozens of different corporate domains needing to access these services I thought ADFS or Active Directory Federation Services would be an ideal fit.

Ultimately we used far fewer of the ADFS features than originally thought but regardless, I struggled so much to understand the whole end to end process of building the servers and hosting up from scratch to deploy my services to, that I decided that I needed to document that learning experience in case any developer in the future who, like me, has to build a custom environment from the ground up needs some guidance.

For this tutorial you will need to have at least two Server 2012 machines to work with. You can accomplish this a number of ways, Hyper-V VMs, VMware VMs, two separate physical boxes, Amazon EC2 Instances, etc.. For this tutorial I will be using a single Server 2012 machine running VMs on Hyper-V, but the steps in this guide can be applied to a number of different computing environments. However, I would suggest using VMs on a private network if you are doing this strictly as a learning exercise. In later steps we will have to promote the Identity server to a Domain Controller.

In this tutorial I will be providing a concrete example of creating and configuring a custom application domain and implementing an ADFS server to handle token distribution.

I’m not entirely sure how many parts this series of articles will contain as there is a substantial amount of work and understanding involved here.

 

Before We Begin

Before we can get started, we need to have our first two machines up and running. Creating the VMs that you will be using or building the physical boxes is an exercise for the reader since there are so many ways of going about this.

Okay, so go ahead, get your machines set up, I’ll wait… done yet? No… Now? Okay good! Let’s move on!

 

First Steps

Our first several tasks are going to involve configuring the Identity server for all the various tasks it will be performing in the near future.  The Application server has much less configuration required but we will get to that later.

We are going to start by installing the IIS Role on the identity server. Log into your identity machine, the Server Manager panel should launch automatically. On the Server Manager panel click Manage->Add Roles and Features.

Add Roles

When the Roles and Features wizard comes up, you can click “Next” through the first three screens until you come to the roles selection screen shown below.

IIS role selection

Check the “Web Server (IIS)” box. There will be an automatic pop up asking to install the IIS management tools with the IIS role. Click the “Add Features” button shown below.

Add Feature confirmation

Then click “Next”. You will now be presented with the “Features Selection” screen. Strictly speaking, nothing should be absolutely necessary on this page but I like to add a few things as a personal preference, mostly for backwards compatibility. The three additional options I select are shown below.

Features selection

The HTTP Activation feature will require additional dependencies. Once your features are selected, click “Next” twice.

Now you’re presented with the Role Services selection window. A lot of the choices here are going to be based on your needs, but I have a set of capabilities that I like or need in most IIS installations that I will show below in the next three images.

IIS services 1 IIS Services 2 IIS Services 3

Most of the selections I make here are not strictly necessary but the two categories you need to think about the most are the Security and Logging options.  Additional Security services will allow you the freedom of choosing to authenticate your WCF services/users in more ways, and the additional logging options are extremely handy when you’re getting a very generic authentication failure error message and can’t figure out why.

Now click “Next” and on the following page make sure to select the automatic restart check box as shown below.

Restart and install

Then click “Install”. A progress bar screen will be displayed, and the server will automatically restart during the installation process if necessary.

In Part 2 we will continue configuring the Identity server with the additional roles and services that it needs to act as an identity server, starting with ADDS(Active Directory Domain Services).

Now that you have been through the process of adding a Role, the next section will not have as much detail about how to do this, unless an important configuration needs to be pointed out during the role installation.

UDP Discovery Tutorial – Part 2

In Part 1 of this article I showed how one might use UDP datagrams to discover a client or server on a connected system over an IPv4 network. In Part 2 I am going to add a subsequent TCP connection and briefly explain how it is done.

 

Now we add a TCP connection.

There is more than one way to approach adding a TCP connection to this example.  One of the simpler to understand approaches would be to use UDP to broadcast the listener’s IP back to the broadcaster after the listener received the broadcaster’s IP, and then create a TCP connection between the two specific IPs.

I am not using the above described method however. It seems to me to be a more usable approach to have the broadcaster asynchronously wait for an incoming TCP connection from any source, and once the connection is made authentication can be performed, but as with Part 1, I leave authentication and error handling up to the reader to implement.

Since we are using an asynchronous model for the TCP connection and communications, the pattern that all operations will follow is:

  1. Operation
  2. Callback
  3. Wait on Callback

We will also be using custom events to implement our waiting operations. These are created on lines 28 of the UDPBroacaster project and lines 27-29 of the UDPListener project.

 

Resolve the TCP address to an Endpoint

The first step we perform here is to resolve the TCP address that we are using to an endpoint object for the TCP client object to initialize itself with.

For the listener this is done using the IP received in the UDP datagram:

IPEndPoint remoteEP = new IPEndPoint(tcpAddress, tcpPort);

For the broadcaster this is done using it’s own host IP since we are accepting connections from any other address.

IPEndPoint localEndPoint = new IPEndPoint(localIP, tcpPort);

 

Connect to the Broadcaster

Once we have the endpoints we now attempt a connection to the UDPBroadcast application from the UDPListener application:

            // Connect to the remote endpoint
            client.BeginConnect(remoteEP, new AsyncCallback(ConnectCallback), client);
            connectDone.WaitOne();  // Without these waits other processing could continue
            listView.Items.Add("TCP Connection Established with UDPBroadcaster");

 

Asynchronously wait for connection

Now we listen for an incoming connection request from anyone within the UDPBroadcast application:

            // Bind the socket to the local endpoint and listen for incoming connections
            //  This logic will parrot back any message received
            listener.Bind(localEndPoint);
            listener.Listen(100);

            // Start an asynchronous socket to listen for connections
            listener.BeginAccept(
                new AsyncCallback(AcceptCallback),
                listener);

            // Wait until a connection is made before continuing
            allDone.WaitOne();
            listView.Items.Add("TCP Connection has been Established, and message received and returned");

The above listening logic for the UDPBroadcast application will asynchronously listen for a TCP connection, and once established, will wait for a message, read the message and return the same message back to the sender.  You can see all the logic for the above within the AcceptCallback() method which you can view by downloading the example projects for this tutorial.

 

Send some test data

Now that we have a connection established, we can send some data to make sure everything is connected up correctly.  We send a test message from within the UDPListener application:

            // Send test data to the remote device
            Send(client, "Hello, this is UDPListener!<EOF>");
            sendDone.WaitOne();
            listView.Items.Add("Message sent to UDPBroadcast");

In a real application this initial message would likely be where you authenticate the connection between the two applications.

 

Wait for responce

Since the UDPBroadcast application is designed to parrot back any message it receives we can immediately wait for a response within the UDPListener application:

            // Receive the response from the remote device
            Receive(client);
            receiveDone.WaitOne();
            listView.Items.Add("Received the following responce from UDPBroadcast: "+response);

 

Close the Connection

Now that we have connected, sent, and received back data from the UDPBroadcast application we can close the TCP connection and release the socket:

            // Release the socket
            client.Shutdown(SocketShutdown.Both);
            client.Close();
            listView.Items.Add("Connection to UDPBroadcast application has been closed.");

 

Final Comments

The above is a very basic example, and as mentioned before error handling and proper authentication is necessary for using this in any real application.  Much of the described logic is encapsulated within the callback methods that can be viewed by downloading the completed example projects linked at the bottom of this article.

I hope this has been helpful to those that have come here to read it. And as always if you have suggestions for improvements to this article, or comments about anything that I’ve done wrong, please leave a comment below.

UDPListenerPart2.zip

UDPBroadcasterPart2.zip

 

References:

As always please refer to my references for further detail.  Much of the code has been compiled from the linked references. For the UDP references see Part 1 of this series.

https://msdn.microsoft.com/en-us/library/bew39x2a(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/fx6588te(v=vs.110).aspx

UDP Discovery Tutorial – Part 1

I recently had a reason to look into this so I thought I’d post a short basic example of how to use UDP to implement client/server discovery over a local network in C#.  As this is a basic example, I am not going to cover security or negotiation minutia. At least not in these short tutorials. As a note to the reader, all error handling has been omitted for the sake of clarity.

Find the Broadcast IP of your Network

A special “Broadcast Address” must be used when using UDP for the purpose of sending a datagram to all machines connected to a given network.  For example, the typical home network host/gateway of 192.168.0.1 has a broadcast address of 192.168.0.255.  If your network differs from this you can use an IPv4 broadcast address calculator like the one found here http://jodies.de/ipcalc.  We don’t want to have to figure out the broadcast IP by hand every time so we’d like to calculate it programatically.

The below example code shows one possible way of how to calculate the broadcast address for your local network adapter.  Depending on your needs, there is also a special definition for the 255.255.255.255 broadcast address which is the broadcast IP for the 0.0.0.0 network.  This special broadcast address is more limited by definition in that packets sent to it are not forwarded to the routers connecting the local network to other networks.

The below code is a fully encapsulated method to return the broadcast address of the machine that it is running on. The return is a object of the IPAddress type.

        public IPAddress calculateUdpBroadcastAddress()
        {
            //Get the local IP address of the network adapter
            IPHostEntry host;
            host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily.ToString() == "InterNetwork")
                {
                    localIP = ip;
                }
            }

            //Get the SubnetMask of the Local Network Adapter
            foreach (NetworkInterface adapter in NetworkInterface.GetAllNetworkInterfaces())
            {
                foreach (UnicastIPAddressInformation unicastIPAddressInformation in adapter.GetIPProperties().UnicastAddresses)
                {
                    if (unicastIPAddressInformation.Address.AddressFamily == AddressFamily.InterNetwork)
                    {
                        if (localIP.Equals(unicastIPAddressInformation.Address))
                        {
                            localSubnetMask = unicastIPAddressInformation.IPv4Mask;
                        }
                    }
                }
            }

            //Get the SubnetMask bytes before finding the complement
            IPAddress complimentIP = new IPAddress(localSubnetMask.Address);
            byte[] addressBytes = complimentIP.GetAddressBytes();
            byte[] complimentBytes = new byte[4];

            //Get the compliment of the SubnetMask
            for (int i = 0; i < addressBytes.Length; i++)
            {
                complimentBytes[i] = (byte)~addressBytes[i];
            }

            //bitwise OR the compliment of the Subnet Mask and the host IP
            byte[] localBytes = localIP.GetAddressBytes();
            byte[] broadcastBytes = new byte[4];
            for (int i = 0; i < localBytes.Length; i++)
            {
                broadcastBytes[i] = (byte)(complimentBytes[i] | localBytes[i]);
            }

            //Return the calculated broadcast address
            return new IPAddress(broadcastBytes);
        }

With the above code we can find the broadcast IP that we need to broadcast on to find our clients or server.

Select a Listening/Broadcasting Port

Any free port on both your client and server is acceptable for this purpose. MSDN uses port 11000 for their udp example. Personally I like utilizing ports in the 10k to 11k range for me projects as there is a fair amount of free ports in that range.

The port you’ve selected will be used in the following code example, for this example I am using port 11000.

Code to Implement the Listener

            //Update the UI and listen for a datagram until we get the right one
            bool done = false;
            while (!done)
            {
                byte[] bytes = udpListen();  //Blocking method call until a datagram is received

                //Validate the message
                string udpMessage = Encoding.ASCII.GetString(bytes, 0, bytes.Length);
                string[] udpDatagramParts = udpMessage.Split(' ');
                if (!udpDatagramParts[0].Equals("UDPBroadcasterIP:"))
                {
                    //The UDP message we received is not the right one
                    listView.Items.Add("The received datagram was incorrect ... continue listening");
                }
                else
                {
                    //The udp message is the right one
                    listView.Items.Add("Received a correct datagram ... attempting a TCP connection ... ");
                    //Save the IP for the TCP connection
                    connectionIP = udpDatagramParts[1];
                    done = true;
                }
            }

Note: The third parameter to Console.WriteLine, “Encoding.ASCII…” represents the string value sent over UDP in the datagram packet. This contains the desired negotiation information for a discovery situation, such as the IP address of the client or server you wish to connect to.

The above code is performing a blocking listen operation until a UDP datagram has been received. The UdpListen() method can be seen in its entirety in the available visual studio projects listed at the bottom of this article.

Code to Implement the Broadcaster

            Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);

            //The sendBuf contains the message we are sending to the UDPListener, so here is where the application specific validation
            // information and the TCP connection information will be provided to the listener
            byte[] sendbuf = Encoding.ASCII.GetBytes("UDPBroadcasterIP: " + localIP.ToString());
            IPEndPoint ep = new IPEndPoint(broadcastAddress, udpPort);

            for (int i = 0; i < 100; i++)
            {
                s.SendTo(sendbuf, ep);
                Thread.Sleep(10);
            }

This is a very simple example. The broadcaster is rebroadcasting for a period of time to make sure the Listener receives it. In this example I send the datagram every 10 milliseconds for 1 second. Even after the UDP datagram is sent/received there will need to be some negotiation to ensure a more permanent connection is made between the client and server.

In Part 2 of this tutorial I will implement a TCP connection between the Broadcaster and the Listener.  Once Part 2 is published I will also update the below code projects to show the TCP connection as well.

A full implementation of this tutorial is available for download at the link below. This is still a simplified version of what you will likely need for your project but should be more complete than the above code snippets.

UDPListener.zip

UDPBroadcaster.zip

 

Blog Awakenings

Over the Christmas and New Years holiday 2015 my wife, daughter, and I were spending some time in the mountains enjoying the trees and snow.  My wife and I tend to not spend too much on Christmas gifts for each other but this year she got me something that I’ll never forget, she got me a book.

This book was special though, and I’ll never forget that Christmas.  Its safe to say this book changed my life for ever.

The book was Soft Skills, and given the snowy freezing climate I had a lot of time in doors by the fire place to get some good quality reading time in.  So I got to it and in a couple days I had managed to get through the book, as well as another I had gotten The Beer Bible, I’m not a saint!

In the remaining days of our vacation I spent a fair amount of time reading articles at simpleprogrammer.com and watching John’s videos, and then I found something, “Sign up for my free blogging course via email”.

I was iffy about it at first. I wasn’t super enthusiastic about giving out my email address to yet another web site, but everything I had read and heard about the benefits of blogging from John’s educational material changed my mind. So I signed up.

After that I started following the guide’s advice and performing the homework, and even though I have no expectation that John is actually reading my responses or actually looking at my new blog, I knew this would be a good exercise for me. This was going to help get me into a successful routine.

The first lesson or two seemed a bit slow, but they got harder, not in the amount of work that they entailed but the amount of thinking that they made me do about my goals and expectations for my own life and career.

After having made all the decisions that I needed to, at least tentatively, I finally had my blog, this blog.  I have greatly enjoyed the journey to this point and all that I have learned along the way.  The resources that John has over at simpleprogrammer.com have taught me a huge amount in my quest to become more entrepreneurial minded and I look forward to being able to put it all in to practice over time.

So if you to would like to take John’s email blogging course, you can go here to sign up.  I highly recommend that you go through the course, even if you have been blogging for a while and want to improve your blog or blogging skills.  I believe anyone can learn form the lessons in this course.