C#
  Home arrow C# arrow Page 4 - 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 - Testing the Server


    (Page 4 of 4 )

    Now that we have the simple server whipped up, we need to go ahead and build it, then add a new Console Application to our TimeApp solution. Call it “TimeAppTest”. Rename “Class1” to “TimeServerTest”.  All this class is going to do is connect on the local port, send a request string, and output the returned time to our console. Since the code in this class is much like the code in the TimeServer class itself, I’m not going to break it up and explain the parts. Here it is:

    using System;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
     
    namespace TimeAppTest
    {
       /// <summary>
       /// Summary description for Class1.
       /// </summary>
       class TimeServerTest
       {
           /// <summary>
           /// The main entry point for the application.
           /// </summary>
           [STAThread]
           static void Main(string[] args)
           {
             int port = 48888;
             string ip = "127.0.0.1";
     
             TcpClient client = new TcpClient(ip, port);
             Byte[] request = Encoding.ASCII.GetBytes("request");
            
             Console.WriteLine("Sending request...");
            
             client.GetStream().Write(request, 0, request.Length);
                 
             Byte[] response = new Byte[client.ReceiveBufferSize];
             int bytesRead = client.GetStream().Read(response, 0,                                   client.ReceiveBufferSize);
     
             Console.WriteLine("Received response: " +                                              Encoding.ASCII.GetString(response));
             Console.ReadLine();
     
             client.Close();
          }
       }
    }



    Make sure that TimeApp is set as your startup project and press F5 to run. Browse to the bin directory for the TimeAppTest project and run TimeAppTest.exe. If all goes well you will see a message that a request was received in the TimeApp server window and the current date and time string displayed in the TimeAppTest window.

    Conclusion

    In this article we made a simple TCP based server and a simple client application to test it. We have not looked at anything related to Windows Services-–yet! But we will in the next article, where we will convert our TimeApp project to a Windows Service, install it, and use our TimeAppTest project to test it.


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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 1 hosted by Hostway
    Stay green...Green IT