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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Moblin 
JMSL Numerical Library 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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: 4 stars4 stars4 stars4 stars4 stars / 50
    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

    - Working with Regular Expressions in C#
    - Sending Simple E-Mail in C#
    - Building C# Comparable Objects: IComparable ...
    - Color Transformation Applications in C# GDI+...
    - Performing Color Transformation Operations i...
    - Color Transformation in C# GDI+ Programming
    - Exceptions in C#
    - Overriding versus Overloading
    - Value Types and Reference Types
    - Defining Member and Type Visibility
    - Managing Files in C#
    - Working with Windows Registry in C#
    - Lossless Image Resizing in C#
    - Lossless Image Converting in C#
    - Creating an RSS Feed with ASP.Net Written in...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway