C#
  Home arrow C# arrow Page 3 - 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 
Mobile Linux 
App Generation ROI 
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 / 56
    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 - Making the Listener Listen


    (Page 3 of 4 )

    Calling listener.Start() binds the listener to the IP address and port, but that is all. We have to tell it what to do next. In order to do that, we will need to create a loop. For now it will just say “while (true)”, and to cancel the program we’ll press “ctrl + c”. This will be handled a bit differently later on when we make our service.

          public void Start()
          {
             this.listener.Start();
     
             while (true)
             {
                 
             }
          }

    Press F5 to run the application and test it. Pressing “ctrl + c” should terminate the program, as with any other console application. If you have a firewall installed on your PC you may get a message about this application trying to access the Internet or something similar; be sure you tell it to allow access.

    Now that our listener is running and we can turn it off when we’re ready, we need to actually capture requests and respond to them. Capturing the request takes several steps. First, we need to create a TcpClient. Then we will use the TcpClient’s NetworkStream object to read into a Byte[] buffer named incomingBuffer. Once we have received the request-–which does not get processed-–we simply write back the time, in bytes, to the TcpClient’s NetworkStream. The code looks like this:

          public void Start()
          {
             this.listener.Start();
     
             Socket s;
             Byte[] incomingBuffer;
             Byte[] time;
             int bytesRead;
     
             while (true)
             {
                s = this.listener.AcceptSocket();
               
                incomingBuffer = new Byte[100];
                bytesRead = s.Receive(incomingBuffer);
     
                time = Encoding.ASCII.GetBytes(
                   System.DateTime.Now.ToString().ToCharArray());
                s.Send(time);
             }
          }



    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

    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#
    - 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...





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
    Stay green...Green IT