C#
  Home arrow C# arrow Page 2 - Creating a Windows Service with C#, introd...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Creating a Windows Service with C#, introduction
By: David Fells
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 74
    2006-02-28

    Table of Contents:
  • Creating a Windows Service with C#, introduction
  • Creating a Listener
  • Making the Listener Listen
  • Testing the Server

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Creating a Windows Service with C#, introduction - Creating a Listener


    (Page 2 of 4 )

    Now that we have our application set up, we want to create a listener that will monitor the TCP port for incoming requests. Let’s use TCP port 48888, a random, unassigned port (for a listing of TCP port assignments, go here: http://www.iana.org/assignments/port-numbers). We are going to add two readonly fields – int ip = 48888 and IPAddress ip = IPAddress.Parse(“127.0.0.1”). These two fields are used to initialize our listener, which we will define as TcpListener listener.

    In order to use these fields, we have to define a constructor, so add a public method called TimeServer() to your class file. In it, initialize the listener. Create an instance of TimeServer in the Main() method, then add a method called Start() and call it from Main Main (). Your code should look like this:

    using System;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
     
    namespace TimeApp
    {
       /// <summary>
       /// Summary description for Class1.
       /// </summary>
       class TimeServer
       {
          private readonly int port = 48888;
          private readonly IPAddress ip = IPAddress.Parse("127.0.0.1");
     
          private TcpListener listener;
     
          /// <summary>
          /// The main entry point for the application.
          /// </summary>
          [STAThread]
          static void Main(string[] args)
          {
             TimeServer ts = new TimeServer();
             ts.Start();
          }
     
          public TimeServer()
          {
             this.listener = new TcpListener(this.ip, this.port);
          }
     
          public void Start()
          {
             this.listener.Start();
          }
       }
    }

    At this point, our application will build, but running it will not do much of anything. Next we are going to see how to handle an incoming request.

    More C# Articles
    More By David Fells


       · Thanks for reading!
       · Hi ,i have a C# console application which sends and receives messages from...
     

    C# ARTICLES

    - Cyclic Redundancy Check
    - Handling Methods and Functions
    - Destroying Objects in C#
    - Creating Objects in C-Sharp
    - Classes and Objects
    - Programming Languages: Managed versus Native
    - LINQ-to-MySQL with DbLinq in C#
    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT