• 22 Sep 2009 /  Code, Technology

    Here’s yet another old C# code snipped from my antiquated and dead blog. But throwin’ this out there for anyone who might find it helpful.

    [cc lang="csharp" tab_size="2"]
    try
    {
    string newTime;
    System.Net.Sockets.TcpClient t =
    new System.Net.Sockets.TcpClient(”time-a.nist.gov”, 13);
    System.IO.StreamReader rd = new System.IO.StreamReader(t.GetStream());
    newTime = rd.ReadToEnd();

    string[] times = newTime.Split(’ ‘); // Parses The String
    rd.Close();
    t.Close();
    Console.WriteLine(”Todays Date Is: ” + times[1]);
    }
    [/cc]

    I read somewhere that in order for this code to work, the TCP client has to be installed and enabled in Windows Vista. The TCP client is not, be default, enabled in Vista. To install it, Control Panel — Programs and Features — Turn Windows Features On or Off — TCP Client.

    However, I tried this after unchecking my TCP client and it still worked – so maybe it’s just an old wive’s tale! I’d suggest trying it without the TCP client installed – but don’t count on it working if you’re planning on distributing this without further research.

    The second line after the opening try { has ” time-a.nist.gov”, which is the time server. You can swap this out with some of the other time servers below.If you were feeling ambitious, you might try creating a class that will try a random time server on it’s first try and then randomly try other time servers if the first x fail.

    Name | IP | Location
    time-a.nist.gov | 129.6.15.28 | NIST, Gaithersburg, Maryland
    time-b.nist.gov | 129.6.15.29 | NIST, Gaithersburg, Maryland
    time-a.timefreq.bldrdoc.gov| 132.163.4.101 | NIST, Boulder, Colorado
    time-b.timefreq.bldrdoc.gov | 132.163.4.102 | NIST, Boulder, Colorado
    time-c.timefreq.bldrdoc.gov | 132.163.4.103 | NIST, Boulder, Colorado
    utcnist.colorado.edu | 128.138.140.44 | University of Colorado, Boulder
    time.nist.gov | 192.43.244.18 | NCAR, Boulder, Colorado
    time-nw.nist.gov | 131.107.1.10 | Microsoft, Redmond, Washington
    nist1.datum.com | 209.0.72.7 | Datum, San Jose, California
    nist1.dc.certifiedtime.com | 216.200.93.8 | Abovnet, Virginia
    nist1.nyc.certifiedtime.com | 208.184.49.9 | Abovnet, New York City
    nist1.sjc.certifiedtime.com | 208.185.146.41 | Abovnet, San Jose, California

    Tags: , , , ,

  • 21 Sep 2009 /  Code, Technology

    This is an older post, from another [now dead] blog of mine, but I thought I’d throw it up here as well.

    When working in C#, if you are trying to load a jpeg file and you use the System.Drawing.Image.FromFile method on a file that you do not have the proper permissions for, you will get an “Out of Memory” exception. Somewhat misleading – but that is the error resulting from improper permissions.

    I found a web site that claimed that you create a FileStream object from your file and then use the FromStream method on the Image object.

    My solution for loading a file was as follows
    [cc lang="csharp"]
    private void addPhotoButton_Click(object sender, EventArgs e)
    {
    DialogResult newPhoto;
    Image newPhotoImage;
    string newPhotoFileName;
    newPhoto = openFileDialogAddPhoto.ShowDialog();
    if(newPhoto == DialogResult.OK)
    {
    newPhotoFileName = openFileDialogAddPhoto.FileName;
    FileStream photoStream =
    new FileStream(newPhotoFileName,
    FileMode.Open, FileAccess.Read);
    newPhotoImage = Image.FromStream(photoStream);
    }
    }
    [/cc]

    Tags: , ,