Click here to Skip to main content
15,896,415 members
Articles / Programming Languages / C#

Remote Access .NET CF Devices

Rate me:
Please Sign up or sign in to vote.
4.94/5 (16 votes)
8 Jan 2010CDDL18 min read 36.3K   3.2K   60  
Implementing remote access to .NET enabled devices.
//-----------------------------------------------------------------------
// <copyright file="Form1.cs" company="Matjazev.NET">
//     Copyright (c) www.matjazev.net. All rights reserved.
// </copyright>
// <author>Matjaz Prtenjak</author>
//-----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Net;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace TCPServer
{
  public partial class Form1 : Form
  {
    private Matjazev.Tcp.TcpServer tcpServer = null;
    private List<string> ipDescriptions = new List<string>();
    private int ipDescriptionPos = 0;

    private void showLabel(object sender, EventArgs e)
    {
      bool working = (this.tcpServer != null) && (this.tcpServer.IsWorking);
      lblInfo.Text = (working) ? "Server running" : "Server stopped";
      lblIP.Visible = working;
    }

    public Form1()
    {
      InitializeComponent();
      string programPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
      this.tcpServer = new Matjazev.Tcp.TcpServer(programPath);

      this.tcpServer.StatusChanged += this.showLabel;
      this.showLabel(this.tcpServer, null);
    }

    private void lblInfo_Click(object sender, EventArgs e)
    {
      this.ipDescriptionPos++;
      if (this.ipDescriptionPos >= this.ipDescriptions.Count) this.ipDescriptionPos = 0;
      lblIP.Text = this.ipDescriptions[this.ipDescriptionPos];
    }

    private void button1_Click(object sender, EventArgs e)
    {
      this.tcpServer.Start(15555);

      IPHostEntry host = Dns.GetHostEntry(Dns.GetHostName());
      int cntr = 0;
      foreach (IPAddress addr in host.AddressList)
        this.ipDescriptions.Add(string.Format("IP {2} ({0}/{1})", ++cntr, host.AddressList.Length, addr.ToString()));

      this.ipDescriptionPos = -1;
      this.lblInfo_Click(this, e);
    }

    private void button2_Click(object sender, EventArgs e)
    {
      this.tcpServer.Stop();
      Close();
    }

    private void Form1_Closing(object sender, CancelEventArgs e)
    {
      if (this.tcpServer.IsWorking) this.tcpServer.Stop();
    }
  }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)


Written By
Software Developer (Senior) MERKUR D.D.
Slovenia Slovenia
I am a software developer in one of the largest retailer in our country. My job is (beside else) also in setting some standards in SW development throughout our company.

I have a M.Sc. in computer science and 20 years of experience. I have written 2 books in Slovenian language and both are sold out (the first about C++ language and the second about VBA language)...

Comments and Discussions