Rhyous's 127.0.0.1 or ::1

February 5, 2010

How to get the relative path (folder the executable was launched in) as a string in C#?

Filed under: C# (C-Sharp) — rhyous @ 11:06 am

Well, lets say you launch an application and you want to know the relative path.

In C# you can use the System.Reflection.Assembly.GetExecutingAssembly().Location value. I have searched through many online sites that tell different ways, and some of the more experienced C# developers say that some of the other ways are not always accurate or won’t always work, while this one should always work.

So if you make a class and add two String class variables, you can use this function to populate them:


        private void GetPaths()
        {
            mExecutablePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
            mExecutableRootDirectory = System.IO.Path.GetDirectoryName(mExecutablePath);
        }

Ok, so I like to make sure any newbie can pull this off, so the whole file with it working (using a New WPF project):

using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TestPath
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        DataSet mDataSet;
        String mExecutablePath;
        String mExecutableRootDirectory;

        public Window1()
        {
            GetPaths();
            InitializeComponent();
        }

        private void GetPaths()
        {
            mExecutablePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
            mExecutableRootDirectory = System.IO.Path.GetDirectoryName(mExecutablePath);
        }
    }
}

February 1, 2010

BSD Mag is now a free online publication: Download the first issue

Filed under: FreeBSD — rhyous @ 6:41 pm

Hello everyone,

Many of you may know this, but I didn’t. BSD Mag is now a free online publication.

There is a January and February issue, as well as a few issues from last year and other past years. You can see the issues available for download here:

http://bsdmag.org/pdf-articles

The February issue is titled: BSDs AS SERVERS

The January issue is titled: Infinity. Freedom. FreeBSD

BSD Mag goes right along with my FreeBSD Friday idea of becoming a little more of an export slowly over time. The magazine comes once a month and reading it for a few months probably won’t change your skills much, but over five or ten or more years, the knowledge you gain will help you become an expert.

ShrewSoft VPN Proxy Services Blocks DNS Requests for VMWare Workstation Guests

Filed under: Open Source, VMWare, Windows 7 — rhyous @ 6:00 am

Ok, so I am big support of ShrewSoft VPN Client and I am also a user of VMWare Workstation. However, I have found an incompatibility between the two.

I tell everyone here at work to use ShrewSoft VPN Client on their Windows 7 64 bit clients. However, they are all running VMWare as well. So I feel it is my responsibility to also inform them of this issue that was found by one of my coworkers.

PROBLEM
Well, a strange problem is seen on VMWare Guests running on a host with ShrewSoft VPN Client installed. No DNS requests are going out for the Guest. A DNS Request never leaves the host.

Troubleshooting
I seached the following term on the VMWare forums: DNS Bridged

The following VMWare Community discussion appeared:
DNS Name Resolution using a bridged network connection not working

Resolution
If you stop the ShrewSoft DNS Proxy Daemon, then the issue goes away.

Also, this is not really a reason to stop using ShrewSoft VPN. There is a post about Cisco’s VPN Client firewall causing the same issue.
Problems with networking using Workstation 7

January 30, 2010

How to install VMWare-tools on FreeBSD 8?

Filed under: FreeBSD, VMWare — rhyous @ 1:48 pm

Well, I have documentation on how to create a FreeBSD 8 Desktop environment here:
How to install and configure a FreeBSD 8 Desktop with Xorg and KDE?

However, if you install FreeBSD as a VMWare guest, you will want to install two additional pieces of software when you are finished:

  1. You need to install the VMWare X11 driver (no, it will not be installed when you install the VMWare tools)
  2. You need to install VMWare-tools

This guide is to walk you through resolving the second of these two issues. I assume you followed my article for building the FreeBSD desktop, and if you did, these steps will work for you. But if you didn’t, I can’t guarantee that there won’t be some steps that are slightly different for you.

In order for me to consider the VMWare-Tools installed and working, the following features must work:

  • Clipboard Synchonization
  • Grab/Release Mouse Input when mouse enters/leaves the guests screen
  • Autofit Guest (automatically making the guests screen resolution take all available space)
  • Time Synchronization (the time in the VMWare guest will match the hosts time).

In this document, I successfully get these features working.

I have VMWare Workstation 6.5.3 build 185404 and the host is Windows 7.

Part 1 – Installing and Configuring the VMWare X11 Driver

Already posted on this here:
How to install the vmware video card driver on a FreeBSD 8 guest virtual machine?

Part 2 – Installing and Configuring VMWare Tools

Step 1 – Mount the VMware Tools virtual cd

  1. On the VMWare hosts make sure that your FreeBSD guest is selected and that you are not full screen so you have the VMWare Workstation application surrounding the FreeBSD guest.
  2. From the VMWare Workstation application, choose VM | Install VMWare tools…

    You will see a pop-up inside FreeBSD on the bottom left.

  3. Click on the popup where it says VMWare Tools. This will open Dolphin file browser.
  4. In the Dolphin file browser on the bottom left, you should see the VMWare Tools media. Click on it.

Step 2 – Extract the vmware-freebsd-tools.tar.gz

  1. Right click on vmware-freebsd-tools.tar.gz and choose Extract Archive to… Now you can extract to where you want, as long as you have the folder permission to write to, but the next steps describe where I extracted it to.
  2. Click on Home at the left. There is no need to create a folder as the extacted files will all be in one folder called vmwware-tools-distrib when extracted.
  3. Click OK. The vmware-freebsd-tools.tar.gz is extracted.

Step 3 – Open a shell as root

  1. Cick K | Applications | System | Konsole to open the shell.
  2. Type in su

    $ su
    Password:
    #

Step 4 – Install the freebsd6 compatibility package.

  1. Determine your architecture or processor type. If you are 64 bit, it will be amd64 even it is intel 64 bit. If you are 32 bit, it is likely x86. Type uname -a to determine what your architecture is and look at the last piece of information provided.

    # uname -a
    FreeBSD FBSD8.hsd1.ut.comcast.net. 8.0-RELEASE-p2 FreeBSD 8.0-RELEASE-p2 #0: Tue Jan 5 21:11:58 UTC 2010 root@amd64-builder.daemonology.net:/usr/obj/usr/src/sys/GENERIC amd64
    #

  2. Install the compat6x-.tgz package, where you replace with your architecture.

    # pkg_add -r compat6x-amd64

Step 5 – Compile and Install the vmware-freebsd-tools

  1. Change to the directory where you extracted the vmware-freebsd-tools.tar.gz.

    # cd /home/jared/vmware-tools-distrib

  2. Run vmware-install.pl.

    # ./vmware-install.pl

  3. Just keep hitting enter and accepting the defaults until the vmware-tools are installed.

Step 6 – Start vmware tools

  1. As root run the following:

    # /usr/local/etc/rc.d/vmware-tools.sh

    Or you could just reboot.

    Note: Since vmware-tools doesn’t exactly integrate with rcNG, you don’t need to add anything to /etc/rc.conf. The just dump a script into /usr/local/etc/rc.d and it is just a shell script and isn’t formatted to require it to be enabled by /etc/rc.conf.

Step 7 – Enabling VMWare user features
The vmware-user process must be launched to enable vmware-user features.

  • Clipboard Synchonization
  • Grab/Release Mouse Input when mouse enters/leaves the guests screen
  • Autofit Guest (automatically making the guests screen resolution take all available space)
  1. Configure vmware-user to run at KDE login by copying the /usr/local/bin/vmware-user shell script to the ~/.kde4/Autostart directory.

    $ cp /usr/local/bin/vmware-user ~/.kde4/Autostart

  2. Logout of KDE and log back in.

Step 8 – Running the VMWare-Toolbox
There may be some vmware settings you want to change on the VMWare guest. Not many are configurable through the vmware-toolbox on FreeBSD but at least one is configurable here called Time Synchronization. This feature will synchronize your vmware guest’s time with the hosts time.

  1. Run vmware-toolbox

    $ vmware-toolbox

  2. Check the box to enable time synchronization
  3. Feel free to explore and become familiar with the other settings, there aren’t many and two of the settings can only run as root.

Tuning Recommendations

  1. It is recommended to have kern.hz set to 100 and it is by default so nothing to do here.
  2. It was recommended that on the host, in the vmx config file for the guest, that the following be configured:
    • ethernet0.virtualDev = “e1000″

    If you chose Other | FreeBSD when creating your vm, this is set by default.

  3. Some people will recommend this setting in the vmx config file as well:
    • tools.syncTime = “TRUE”

    However, this is the same as the time synchronization setting we configured with the vmware-toolbox. It is safer to make the configuration in the vmware-toolbox tool.

If you know of any further tuning options, please comment them to me.

January 29, 2010

PDFForge released a new version of PDFCreator 0.9.9

Filed under: Open Source — rhyous @ 3:04 pm

Hey all,

PDFCreator is a nice tool that lets you

I am not sure if you noticed, but PDFForge released a new version of PDFCreator.

I just want everyone to know that I use PDFCreator with Windows 7.

PDFCreator allows me to print from any application to PDF, so if you thought you had to buy Adobe Acrobat Professional to get this feature, you are mistaken. PDFCreator gives you this feature at zero cost to you or your organization.

Save paper and print to PDF.

How to install the vmware video card driver on a FreeBSD 8 guest virtual machine?

Filed under: FreeBSD, VMWare — rhyous @ 12:00 am

Well, I have documentation on how to create a FreeBSD 8 Desktop environment here:
How to install and configure a FreeBSD 8 Desktop with Xorg and KDE?

However, if you install FreeBSD as a VMWare guest, you will want to install two additional pieces of software when you are finished:

  1. You need to install the VMWare X11 driver (no, it will not be installed when you install the VMWare tools)
  2. You need to install VMWare-tools

This guide is to walk you through resolving the first of these two issues. I assume you followed my article for building the FreeBSD desktop, and if you did, these steps will work for you. But if you didn’t, I can’t guarantee that there won’t be some steps that are slightly different for you.

Part 1 – Installing and Configuring the VMWare X11 Driver

Step 1 – Install the VMWare X11 driver

  1. Change to the appropriate ports directory.

    # cd /usr/ports/x11-drivers/xf86-video-vmware

  2. Make (compile) and install the VMWare X11 driver

    # make install

The VMWare X11 driver is now installed.

Step 2 – Modify the /etc/X11/xorg.conf
Note: If you didn’t create an xorg.conf file, then you don’t need to do this step, so skip it.

  1. If you auto-created your xorg.conf, auto create it again.
    # Xorg -configure
  2. Move the auto created /root/xorg.conf.new to /etc/X11/xorg.conf.

    # mv /root/xorg.conf.new /etc/X11/xorg.conf

If you have a custom xorg.conf, then it may be easier to edit it manually.

  1. Edit the /etc/x11/xorg.conf as root.

    # ee /etc/x11/xorg.conf

  2. Change the “Device” section to look as follows.
    Section "Device"
            ### Available Driver options are:-
            ### Values: <i>: integer, <f>: float, <bool>: "True"/"False",
            ### <string>: "String", <freq>: "<f> Hz/kHz/MHz"
            ### [arg]: arg optional
            #Option     "HWcursor"                  # [<bool>]
            #Option     "Xinerama"                  # [<bool>]
            #Option     "StaticXinerama"            # <str>
            Identifier  "Card0"
            Driver      "vmware"
            VendorName  "VMware"
            BoardName   "SVGA II Adapter"
            BusID       "PCI:0:15:0"
    EndSection
    

Step 3 – Restart Xorg
If you are still in KDE, you must logout and log back in. You can probably do this without instructions, but if this is your first time using FreeBSD and KDE 4, a quick screen shot of how to do this won’t hurt. I do like my walk-thru’s to be newbie proof.

  1. At the bottom left of your window, click on the K icon, then Leave, then Logout.

  2. After clicking the Leave option, KDE will exit, and xorg will restart and you should be returned to the login screen. Now log back in.

Note: If you aren’t using KDM, then you will be returned to your logged in shell and you will have to launch Xorg and KDE yourself using the startx command.
Step 4 – Configure your screen resolution

  1. Click the K icon, then Applications | System.
  2. When the menu shifts, the system options appears. Select Screen Resize & Rotate.

  3. Now don’t be confused when this doesn’t open a window. It does launch the program, but for some reason it starts up minimized and is an icon at the bottom right of the panel. It looks like a little monitor (the red arrow is pointing to it).
  4. Click on the monitor icon to get the Configure Display window.
  5. Click the Size drop down menu and choose your new resolution.
  6. Click Apply.
  7. If the resolution fails, it should return to the previous resolution after about ten or fifteen seconds. If the resolution works, click the option to accept the resolution.

You should now have the VMWare Driver installed.

Part 2 – Installing and Configuring VMWare Tools

This is in a separate post here:
How to install VMWare-tools on FreeBSD 8?

January 26, 2010

How to compile and install K-3D 0.7.12 on FreeBSD 8?

Filed under: FreeBSD, K-3D — rhyous @ 8:55 pm

Ok, so I found a port that is need of updating. K-3D. FreeBSD has K-3D version 6.7 in ports but K-3D is on release 7.11 and approaching their version 8 release. So lets see what we need to do to get this port updated.

To start out, I have informed both FreeBSD and K-3D that I am going to help with this using their forums.
http://forums.freebsd.org/showthread.php?t=10647
http://www.k-3d.org/forums/topic/updated-port-for-freebsd-8

Step 1 – Build a FreeBSD 8 Desktop Environment
Build yourself a desktop as described here (or however you want if you know what you are doing):
How to install and configure a FreeBSD 8 Desktop with Xorg and KDE?

Step 2 – Install dependencies
The following are dependencies that are required to build K-3D on FreeBSD 8.

  1. Install mercurial
    K-3D uses Mercurial as its version managed system for its code (it used to use subversion but now uses mercurial). So to download the code you need to install Mercurial.

    As root do this:

    # cd /usr/ports/devel/mercurial
    # make install

  2. Install cmake 2.8 or later.
    K-3D now uses cmake as its build management system as opposed to make or gmake. Since cmake is not installed on FreeBSD by default, lets install it.

    # cd /usr/ports/devel/cmake
    # make install

  3. Install cairomm.

    # cd /usr/ports/graphics/cairomm
    # make install

    Note: This will also install glibmm and sigc++.

  4. Install gnome-vfs.

    # cd /usr/ports/devel/gnome-vfs
    # make install

  5. Install gtkglex.

    # cd /usr/ports/x11-toolkits/gtkglext
    # make install

  6. Install gtkmm24.

    # cd /usr/ports/x11-toolkits/gtkmm24
    # make install

  7. Install gtksourceview2.

    # cd /usr/ports/x11-toolkits/gtksourceview2
    # make install

  8. Install gts.

    # cd /usr/ports/graphics/gts
    # make install

  9. Install ImageMagick.

    # cd /usr/ports/graphics/ImageMagick
    # make install

  10. Install e2fsprogs-libuuid.

    # cd /usr/ports/misc/e2fsprogs-libuuid
    # make install

  11. Install Doxygen.

    # cd /usr/ports/devel/doxygen
    # make install

    Note: This took quite a long time to compile because it also had a lot of large depencies.

  12. Install libexecinfo.

    # cd /usr/ports/devel/libexecinfo
    # make install

Step 3 – Download the code and build it

  1. As a regular user, run these commands:

    $ cd ~
    $ hg clone http://k3d.hg.sf.net/hgweb/k3d/k3d

    The code will take a minute or two (or more if you have a slow connection) to download.

  2. – Make a build directory
    K-3D cannot be built in the same directory where the code resides. So lets create a build directory and change to that directory.

    $ mkdir ~/k3d-build
    $ cd k3d-build

  3. Run cmake against k3d

    $ cmake ~/k3d

    Note: If you forget to install the dependencies, you will get errors. Here is the output of my attempt to compile before I installed the dependencies.

    $ cd k3d-build
    $ cmake ~/k3d
    -- checking the width of std::vector<>::size_type for this platform
    --   std::vector<>::size_type is 64 bits
    -- checking for module 'cairomm-1.0'
    gnome-config: not found
    --   package 'cairomm-1.0' not found
    -- checking for module 'dbus-glib-1'
    --   found dbus-glib-1, version 0.82
    -- checking for module 'freetype2'
    --   found freetype2, version 9.20.3
    -- checking for module 'glibmm-2.4'
    gnome-config: not found
    --   package 'glibmm-2.4' not found
    -- checking for module 'gnome-vfs-2.0'
    gnome-config: not found
    --   package 'gnome-vfs-2.0' not found
    -- checking for module 'gtkglext-1.0'
    gnome-config: not found
    --   package 'gtkglext-1.0' not found
    -- checking for module 'gtkmm-2.4'
    gnome-config: not found
    --   package 'gtkmm-2.4' not found
    -- checking for module 'gtksourceview-2.0'
    gnome-config: not found
    --   package 'gtksourceview-2.0' not found
    -- checking for module 'gts'
    gnome-config: not found
    --   package 'gts' not found
    -- checking for module 'ImageMagick++'
    gnome-config: not found
    --   package 'ImageMagick++' not found
    -- checking for module 'OpenEXR'
    --   found OpenEXR, version 1.6.1
    -- checking for module 'libpng12'
    --   found libpng12, version 1.2.40
    -- checking for module 'sigc++-2.0'
    gnome-config: not found
    --   package 'sigc++-2.0' not found
    CMake Error at cmake/modules/K3DDependencies.cmake:18 (MESSAGE):
      Couldn't find glibmm, which is required to build K-3D.  You can obtain
      glibmm from http://gtkmm.org.
    Call Stack (most recent call first):
      CMakeLists.txt:248 (K3D_CHECK_REQUIRED_DEPENDENCY)
    
    CMake Error at cmake/modules/K3DDependencies.cmake:18 (MESSAGE):
      Couldn't find libsigc++, which is required to build K-3D.  You can obtain
      libsigc++ from http://libsigc.sourceforge.net.
    Call Stack (most recent call first):
      CMakeLists.txt:251 (K3D_CHECK_REQUIRED_DEPENDENCY)
    
    -- checking for module 'uuid'
    gnome-config: not found
    --   package 'uuid' not found
    CMake Error at cmake/modules/K3DDependencies.cmake:18 (MESSAGE):
      Couldn't find uuid, which is required to build K-3D.  You can obtain uuid
      from http://www.ossp.org/pkg/lib/uuid.
    Call Stack (most recent call first):
      CMakeLists.txt:257 (K3D_CHECK_REQUIRED_DEPENDENCY)
    
    CMake Error at cmake/modules/K3DDependencies.cmake:28 (MESSAGE):
      Couldn't find the gtkglext library, which is required by
      K3D_BUILD_NGUI_MODULE.
    Call Stack (most recent call first):
      CMakeLists.txt:272 (K3D_CHECK_OPTIONAL_DEPENDENCY)
    
    CMake Error at cmake/modules/K3DDependencies.cmake:28 (MESSAGE):
      Couldn't find the gtkmm library, which is required by
      K3D_BUILD_NGUI_MODULE.
    Call Stack (most recent call first):
      CMakeLists.txt:273 (K3D_CHECK_OPTIONAL_DEPENDENCY)
    
    CMake Error at cmake/modules/K3DDependencies.cmake:28 (MESSAGE):
      Couldn't find the cairomm library, which is required by
      K3D_BUILD_NGUI_PIPELINE_PANEL_MODULE.
    Call Stack (most recent call first):
      CMakeLists.txt:274 (K3D_CHECK_OPTIONAL_DEPENDENCY)
    
    -- checking for module 'gthread-2.0'
    --   found gthread-2.0, version 2.20.5
    -- generating i18n catalog
    --   done
    -- Could NOT find Doxygen  (missing:  DOXYGEN_EXECUTABLE)
    -- Configuring incomplete, errors occurred!
    $
    

    Hopefully you installed the dependencies and didn’t see any of the above errors.

Step 4 – Test Running the application
We can test running this from the build directory before installing, so lets do that.

  1. From the k3d-build directory, type make run:

    $ make run

  2. Ok, so now that it is compiling it is failing to run…hopefully we can get that fixed. Stay tuned.

UPDATE: 2/2/2010

Ok, so I am back. Here is the what I have done.

Step 5 – Fixing run errors

  1. Compiled with debugging. I ran ccmake ~/k3d again and this time I set the CMAKE_BUILD_TYPE to Debug before choosing to configure. Once configure was done, I ran make again.

  2. Grepped for the error in the code. Specifically, this term:

    $ grep -Rn k3d::iuser_interface ~/k3d/*

    I found the problem code was in this file and line number:
    File: /home/jared/k3d/application/k3d_main.cpp
    line: 460

  3. Went to the problematic code and investigated it. It seems to be a simple check, so I commented that block of code out so it would continue:
    //      if(!dynamic_cast<k3d::iuser_interface*>(g_user_interface))
    //     {
    //              delete g_user_interface;
    //              g_user_interface = 0;
    //              handle_error("UI plugin module [" + module_name + "] does not impleme
    //              return;
    //      }
    
  4. I recompiled (everything remained compiled except the one part with the file I changed so compiling was quick).
  5. I launched make run again:

    # make run

    This time, as I had hoped, it loaded the application completely.

    However, I noticed some errors.

    The first two error lines occurred early in the load process. I am not sure but they seem to be unrelated:

    INFO: Gtk: Failed to load module “atk-bridge”: Shared object “libatk-bridge.so” not found, required by “k3d”
    :1: error: unexpected character `\241′, expected keyword – e.g. `style’

    ERROR: /home/jared/k3d/modules/virtual_opengl_painters/sds_point_painter.cpp line 73: assertion `delegate’ failed
    ERROR: Error creating document plugin: VirtualOpenGLSDSPointPainter
    ERROR: /home/jared/k3d/modules/virtual_opengl_painters/sds_edge_painter.cpp line 73: assertion `delegate’ failed
    ERROR: Error creating document plugin: VirtualOpenGLSDSEdgePainter
    ERROR: /home/jared/k3d/modules/virtual_opengl_painters/sds_face_painter.cpp line 73: assertion `delegate’ failed
    ERROR: Error creating document plugin: VirtualOpenGLSDSFacePainter

  6. I searched the ports tree for the libatk-bridge.so file.

    # /usr/ports
    # grep -R libatk-bridge *

    It looks like there is a at-spi package that provides this library.

  7. Install the at-spi package

    # cd /usr/ports/accessibility/at-spi
    # make install

  8. Tried to make run again.

    # exit
    $ cd ~/k3d-build
    $ make run

    The application loaded and the libatk-bridge error was gone but it was replaced with a warning:

    (k3d:55092): atk-bridge-WARNING **: AT_SPI_REGISTRY was not started at session startup.

    (k3d:55092): atk-bridge-WARNING **: IOR not set.

    (k3d:55092): atk-bridge-WARNING **: Could not locate registry

  9. Ok, so now that it is running, I think it is time to submit bugs to K-3d for the remaining errors.

As of right now, I don’t think it is worth the effort to create a port of version 7.11 as it has bugs for FreeBSD. I feel comfortable that when the next revision of K-3D comes out, version 8.x, the code will work well and we will create a port for it.

UPDATE: February 8, 2010

The main developer has made some changes so that K-3D launches now. The error that prevent the launch is gone (the others that seem somewhat benign are still occurring).

There is now a problem with K-3D on FreeBSD in that the faces cannot be properly selected, so the user-interface is not working perfectly.

Again, I will work on this problem and update this post.

January 22, 2010

How to install the Code::Blocks IDE on FreeBSD 8?

Filed under: Code::Blocks, FreeBSD — rhyous @ 12:00 am

Ok, so some developers are completely happy and content coding without an Integrated Development Environment (IDE), but I really like IDEs and think they provide a lot of ways to improve development speed and efficiency. I never have been able to get into vim or emacs though if you are into it, that’s cool for you, just doesn’t work for me.

So lets install the Code::Blocks IDE on FreeBSD and compile an application or two.

Step 1 – Setup a FreeBSD Desktop
Instructions for doing this are located here
How to install and configure a FreeBSD 8 Desktop with Xorg and KDE?
After following the linked-to guide, you should have FreeBSD with Xorg and KDE installed.

Step 2 – Install CodeBlocks from ports

  1. Login as root.

  2. Change to the ports directory.

    # cd /usr/ports

  3. Find the CodeBlocks port. There are a couple of commands that can help you find the port.

    Run this from anywhere:

    # whereis codeblocks

    Or from /usr/ports run this:

    # make search name=codeblocks

    The port location is /usr/ports/devel/codeblocks.

  4. Change to the directory for the codeblocks port.

    # cd /usr/ports/devel/codeblocks

  5. Type make install to compile and install Code::Blocks.

    # make install

    Note: If you pay attention while CodeBlocks is compiled, you will see that wxWidgets is installed as a dependency.

Step 3 - Launch CodeBlocks for the first time

  1. Click the K icon at the bottom left (similar to the Start icon in windows) and move the cursor over the Applications tab.

  2. Click Development and you will see the list of items. Yes, the Code::Blocks install places an icon to the Development section of the KMenu for you.

  3. Click the Code::Blocks icon.
  4. You will be prompted for a compiler. Select GNU GCC Compiler.

You now are running the Code::Blocks IDE on FreeBSD.

Note: Code::Blocks opens and gives you a "Tip of the day" window. These tips can be valuable, so unless you are a Code::Blocks expert, I recommend leaving them on and taking the short seconds it takes to read them. This goes along with my overall theme of becoming a little more of an expert each day.

Step 4 - Create a new project and compile it

  1. Click File | New | Project.

  2. Select Empty Project.
  3. The next screen is just a welcome screen and you can click a check box to never see it again and then click Next.
  4. Enter a project name. I just typed in hw (short for, yes you guessed it, Hello World).
  5. Enter a directory or click the ... icon to browse to a directory.
    Note: The bottom two fields are filled out for you.

  6. Click Next.
  7. On the next screen you have options, such as switching to a different compiler, but everything is pretty much set to defaults how you probably want. So unless you have a good reason, leave it as is and click Finish.
  8. Click File | New | Empty File.
  9. When prompted to add to the project, choose Yes.
  10. Enter a file name. I used main.cpp and click next.
  11. You will be prompted for the targets this file should be added to, such as debug and release. Click Select All and hit Ok.
  12. On the left, under Projects, expand Workspace, the expand the project Name ("hw" in this example), expand Sources.
  13. Open main.cpp by double-clicking on it.
  14. Enter your code (in this case I am entering in some simple hello world code).
    [/sourcecode]
    #include 
    
    int main()
    {
    	std::cout << "Hello World" << std::endl;
    
    	return 0;
    }
    
  15. Select Build | Build or press Ctrl + F9 to build/compile your program.

Your program should now be compiled.

Step 5 - Run the program

  1. You can run the program in two ways:

    • Select Build | Run or press Ctrl + F10.

    • Open a shell and browse to the directory.

Step 6 - Debug the program
To debug a program you must first create a break point and then start debugging, in that order. So lets do it.

  1. Set a breakpoint by clicking just to the right of the line numbers just to the left of the line of code you want the program to break at. When you set the break point, a red circle is added. I am setting a breakpoint on line 3 of the Hello World code, which is the main() function.

  2. Click Debug | Start or press F8. The program will start and a yellow arrow will show up inside the red circle.
  3. Press F7 to cause the debugging to continue on to the next line. Notice the yellow arrow moves to the next line.

Congratulations you are now using the Code::Blocks IDE on a FreeBSD desktop.

January 21, 2010

Book Review: TCP/IP Sockets in C – Practical Guide for Progammers Second Edition (Chapter 2)

Filed under: C/C++, Sockets — rhyous @ 10:14 am

Hello everyone,

I have just made my way through chapter 2. It had a few hiccups but was overall well done.

This chapters introduces you to a simple Client and Server communication process. The client program can send text to the server. The server sends the text back.

Client Code

The client code seems to work fine. I am a big fan of showing the absolute minimum the first time and not including anything else that may get in the way. However, they didn’t do the minimal, (though don’t worry, they were very close to the minimal). They also added two files, so to compile the following files are needed:

  • TCPEchoClient4.c
  • Practical.h
  • DieWithMessage.c

Personally, I have a hard time using code that I don’t understand, so this made me deviate from my purpose of learning sockets to see what is in these other files. Of course, the book give brief explanations too. These files were small and easy to understand.

Practical.h just declares a bunch of functions. However the implementation of these functions are if different .c files.

DieWithMessage.c provides the implementation of two of these functions, DieWithUserMessage and DieWithSystemMessage. These functions are little more than wrappers to output error messages to standard error.

To compile the client code on FreeBSD 8, I did this:

  1. Put the following files in the same directory: /home/jared/Devel/Echo/echoclient

    • TCPEchoClient4.c
    • Practical.h
    • DieWithMessage.c
  2. Change to that directory.
    cd /home/jared/Devel/Echo/echoclient

  3. Run gcc as follows:
    gcc TCPEchoClient4.c DieWithMessage.c -o echoclient

The book includes information about a web site where the code from the book can be downloaded. This web site is here:
http://cs.ecs.baylor.edu/~donahoo/practical/CSockets2/textcode.html
When I downloaded the code from there, the compiling of the client code worked first time and the echoclient works.

When I typed the code in myself (which I like to do because I think it helps me learn better to actually have to type all the code), I had some typos that the compiler errored on and I fixed them.

Server Code (IPv4)
My experience with this first example of server code was…well, lets just say it was confidence building.

The server code also had other files. Not only did it have the same extra files the client code had, but it required even more files:

  • TCPEchoServer4.c
  • Practical.h
  • DieWithMessage.c
  • TCPServerUtility.c
  • AddressUtility.c

We already know what Practical.h and DieWithMessage.c do.

TCPServerUtility.c is similar to DieWithMessage.c in that it provides implementation of functions declared in Practical.h, however, it is not the same in that it is not as short of a file with over 100 lines. It is not as complex as it looks because TCPServerUtility.c provides implementation for only three functions: SetupTCPServerSocket, AcceptTCPConnection, and HandleTCPConnection.

AddressUtility.c provides the implementation to functions declared in Practical.h as well. The following two functions are implemented: PrintSocketAddress, SockAddrsEqual.

I put these files in the same directory and tried to compile just like I did with the client code. However, there was a huge problem when I tried to compile. I got a long lists of errors.

[jared@FBSD8 ~/Devel/Echo/echoserver1]$ gcc TCPEchoServer4.c TCPServerUtility.c DieWithMessage.c AddressUtility.c
TCPServerUtility.c: In function ‘SetupTCPServerSocket’:
TCPServerUtility.c:17: error: ‘IPPROTO_TCP’ undeclared (first use in this function)
TCPServerUtility.c:17: error: (Each undeclared identifier is reported only once
TCPServerUtility.c:17: error: for each function it appears in.)
TCPServerUtility.c:25: error: ‘for’ loop initial declaration used outside C99 mode
AddressUtility.c:6: warning: ’struct sockaddr’ declared inside parameter list
AddressUtility.c:6: warning: its scope is only this definition or declaration, which is probably not what you want
AddressUtility.c: In function ‘PrintSocketAddress’:
AddressUtility.c:16: error: dereferencing pointer to incomplete type
AddressUtility.c:17: error: ‘AF_INET’ undeclared (first use in this function)
AddressUtility.c:17: error: (Each undeclared identifier is reported only once
AddressUtility.c:17: error: for each function it appears in.)
AddressUtility.c:18: error: dereferencing pointer to incomplete type
AddressUtility.c:19: error: dereferencing pointer to incomplete type
AddressUtility.c:21: error: ‘AF_INET6′ undeclared (first use in this function)
AddressUtility.c:22: error: dereferencing pointer to incomplete type
AddressUtility.c:23: error: dereferencing pointer to incomplete type
AddressUtility.c:30: error: dereferencing pointer to incomplete type
AddressUtility.c: At top level:
AddressUtility.c:40: warning: ’struct sockaddr’ declared inside parameter list
AddressUtility.c: In function ‘SockAddrsEqual’:
AddressUtility.c:43: error: dereferencing pointer to incomplete type
AddressUtility.c:43: error: dereferencing pointer to incomplete type
AddressUtility.c:45: error: dereferencing pointer to incomplete type
AddressUtility.c:45: error: ‘AF_INET’ undeclared (first use in this function)
AddressUtility.c:48: error: dereferencing pointer to incomplete type
AddressUtility.c:48: error: dereferencing pointer to incomplete type
AddressUtility.c:49: error: dereferencing pointer to incomplete type
AddressUtility.c:49: error: dereferencing pointer to incomplete type
AddressUtility.c:50: error: dereferencing pointer to incomplete type
AddressUtility.c:50: error: ‘AF_INET6′ undeclared (first use in this function)
AddressUtility.c:53: error: dereferencing pointer to incomplete type
AddressUtility.c:53: error: dereferencing pointer to incomplete type
AddressUtility.c:54: error: invalid application of ’sizeof’ to incomplete type ’struct in6_addr’
AddressUtility.c:54: error: dereferencing pointer to incomplete type
AddressUtility.c:55: error: dereferencing pointer to incomplete type
[jared@FBSD8 ~/Devel/Echo/echoserver1]$

This was not comforting. I really don’t like to debug sample code but here it goes. In the past, I probably would have stared at these errors and maybe I wouldn’t have had the skills to solve them. However, I was undaunted and resolved these. Let me explain how I resolved them.

  1. TCPServerUtility.c:17: error: ‘IPPROTO_TCP’ undeclared (first use in this function)
    This first error is a problem in TCPServerUtility.c with IPPROTO_TCP variable being undeclared. So I had to figure out where IPPROTO_TCP was declared. It is declared in netinet/in.h, which for some reason is not included. Maybe it only needs to be included on FreeBSD or maybe this is a bug in the Author’s code. Later, I will send the author a link to this post and let him comment on it. For now, I will add this line to the top of the TCPServerUtility.c.

    #include <netinet/in.h>
    
  2. TCPServerUtility.c:25: error: ‘for’ loop initial declaration used outside C99 mode
    This second error can easily be resolved by compiling with C99 mode enabled. This can be added to gcc as a paramter -std=c99.

  3. AddressUtility.c:16: error: dereferencing pointer to incomplete type
    This is not super clear to me at first. However, the line isn’t doing much. So why is the type incomplete? Maybe the struct is not declared yet because again, a file that should be included is not. The struct being called is sockaddr_in and it is defined in netinet/in.h but sure enough, netinet/in.h is not included in AddressUtility.c, so lets add it.

    #include <netinet/in.h>
    
  4. AddressUtility.c:17: error: ‘AF_INET’ undeclared (first use in this function)
    This error in AddressUtility.c is similar to the first error in TCPServerUtility.c. There is probably a header file missing. Again, this may be only a FreeBSD issue or it may be a bug in the author’s code. It is hard to know since I don’t see anywhere where the author specified the platform this was tested on. So I researched and found that AF_INET is declared in sys/socket.h however, again it is not included. Se we add this line to the file.

    #include <sys/socket.h>
    

Ok, now try to compile with this command line:

gcc -std=c99 TCPEchoServer4.c DieWithMessage.c TCPServerUtility.c AddressUtility.c -o echoserver4

Everybody cheer…this time it worked.

It tested the client and the server application together and sure enough, communication occurred. Any string I sent from the client, server sent back.

I was able to use the exact same command with the IPv6 version of the server code, only replacing the TCPEchoServer4.c with TCPEchoServer6.c.

gcc -std=c99 TCPEchoServer6.c DieWithMessage.c TCPServerUtility.c AddressUtility.c -o echoserver6

The rest of the chapter
The rest of the chapter is about what you would expect. The author gives a snippet of code and explains it well. Make sure to take time to read it. This is foundational material for understanding the rest of this book, so it may not hurt to read it twice.

Other notes on the C code
You need to be familar with some common C language features such as typedef and struct as they are used heavily.

typedef
Most the types used are just types that have been renamed with typedef. I am not 100% sure why one would declare a function as follows but I know at least one reason that I will mention:

in_port_t port;

To me this is much more confusing than what it is really saying. in_port_t is just an unsigned short. So it would seem that one should declare the function as follows:

unsigned short port;

However, what if your code is very popular and five years or ten years from now the RFC changes the implementation of a port so that an unsigned int is now required. it is really easy to change in_port_t to point be a typedef of an unsigned int and no other work needs to be done. However, if you declared your variable using unsigned short instead of in_port_t, then every where in your code that you did this, you have to find and manually change it or your code will probably fail.

You can even make a typedef of a typedef and in fact that is what in_port_t is. If you have an IDE (I am using Code::Blocks), then you can highlight the type, right click it and choose something similar to Find Declaration of: in_port_t and it will take you to where this value is defined using a typedef. However, in_port_t is a typedef of uinst16_t, which is a typedef itself of __uinst16_t, which is a typedef of unsigned short. So it is multiple typedefs deep. While sometimes confusing, this can be useful as mentioned above.

struct
C does not have objects or classes. Instead it has structs and one might say that they are the predecessor to objects or classes. However, they are not objects or classes and you should understand how they function. If you understand them, this book will be easier to understand. If you don’t, you will probably struggle. If you aren’t clear on structs, then take time to read about them.

January 20, 2010

Book Review: TCP/IP Sockets in C – Practical Guide for Progammers Second Edition (Chapter 1)

Filed under: C/C++, Sockets — rhyous @ 10:32 am

Hello everyone.

I want to learn sockets on FreeBSD so naturally I bought the following book about it.
TCP/IP Sockets in C, Second Edition: Practical Guide for Programmers (The Morgan Kaufmann Practical Guides Series)

Ok, so chapter one says it is an introduction but would be bettered titles as “Basic Computer Networking Concepts” as it really covers in a very, very broad sense the important points of the OSI model (without mentioning the OSI model). If you are excellent at networking, TCP/IP, etc, this chapter will be a quick read. It is so short that even if you are already an expert, just read it. Who knows, you may find that one line that will teach you something. By the way, I spent four years doing tech support for Nortel Networks Routers, Layer 3 switches, Load Balancers, Wireless APs, etc. I lived in the network world and spent a lot of time in Sniffer Pro or Wireshark (then Ethereal). After which I came to LANDesk (my current employer) where I spent a lot of time working with PXE booting over the network, which uses bootp, dhcp, TFTP and furthered my understanding of client/server communication. So you might think that I should have skipped this chapter entirely. No, I read it.

I found section 1.2.2 – Dealing with Two Versions interesting. This section mentions straight up that socket programmers will have to deal with IPv6. The whole chapter doesn’t just talk of IPv4 only, it includes little snippets of both. I believe the big move to IPv6 will not come quite yet. In 1999, a trainer in my MSCE class said that everyone will be using IPv6 within five years (2004) and if you don’t know, it has really been adopted yet in 2010. It is moving slowing and I think it will continue. Windows 7 and Windows 2008 will cause a lot of adoption, but people are still using Windows 98 in many places even today in 2010. Windows 2000 and Windows XP will take quite a long time to fade away. I predict that some companies (though not many) will still be running Windows XP in 2020. I think that a bigger adoption of IPv6 will occur once the next windows OS (the version after Windows 7) is on the majority of computers. So windows 7 will do what Windows Vista couldn’t and replace XP in the corporate world. The corporate world still must move all their domain environments to Server 2008 (or they may stay on Windows Server 2003 until the next server release after Windows Server 2008 ). The home world will take about five years (2015) to have everyone a majority moved to Windows 7. Anybody on Vista will upgrade to get off that poor unstable operating system and anybody running XP will probably have old hardware that will start dying off and will have to purchase new computers that already come with Windows 7. Moving the operating system is not enough. That aone won’t make everyone move to IPv6. Wireless routers are just starting to support IPv6 such as the DIR-655 Xtreme N Gigabit Router. However, there is a big difference between supporting IPv6 and making IPv6 the default. Most Internet Service Providers (ISPs) still give IPv4 addresses. The infrastructure using IPv4 is so huge it could take a few more decades to completely remove it. You can probably guess that the adoption is going to reach a craze when your ISP starts sending you notices that they are moving to IPv6 and you have to comply and you don’t have a choice, kind of like what just happened with the analog TV service this year. Funny, thing, for a lot of people, me included, their Cable TV service is also their ISP.

Ok, enough rambling about IPv6. It is enough to know that you are going to start to have to deal with it now and at some future time IPv4 will be replaced and you will only have to deal with IPv6.

As for the other topics in the chapter, it is actually amazing that they were able to mention as many topics as they did in such short pages. They mention NAT and DNS and URLs and other important topic (each of which have dozens of large books covering just the one topic). The networking world is a whole other field that have engineers who spend their entire lives becoming experts at just using the equipment, let alone developing software for it. Usually a software developer won’t need to reach such an expert level understanding of networking (unless they are developing the OS for a router/switch or a software such as a firewall).

This chapter was necessary and they added it and I would say they did a good job keeping it short and simple. They did a good job of providing the detail needed at just enough level that a freshman/sophmore in college who knows little about networking and programming would be able to understand the concept of networking enough to have a general idea why they need to code with sockets they way the book will describe.

Stay tuned for a review on Chapter 2.

Update: My review on Chapter 2 is here:
Book Review: TCP/IP Sockets in C – Practical Guide for Progammers Second Edition (Chapter 2)

Next Page »

Blog at WordPress.com.