Solved

Create dummy service for port status check

Posted on 2015-02-05
5
614 Views
Last Modified: 2015-02-13
Hello EE,

I want to create a service that runs any old exe for the purpose of queuing a status for another peice of hardware. In essence , this service will run on port 5555 if disabled , it will invoke an event on another piece of hardware .

I will use the sc create " Some Service" binpath= " C:\path\some.exe" . I tried to create a dummy.exe and save n notepad as "dummy.exe" but the service will fail on start up .. Is there way to do a quick compile for any old exe that will start the service

This is windows 2012 server 64bit
0
Comment
Question by:davesnb
  • 3
  • 2
5 Comments
 
LVL 23

Expert Comment

by:Michael74
ID: 40592953
0
 

Author Comment

by:davesnb
ID: 40599011
ok great , I have this installed as a service , however I need it to listen on port 5555
0
 
LVL 23

Accepted Solution

by:
Michael74 earned 500 total points
ID: 40599464
Here is a tutorial that has the code you can integrate into your service to listen on a selected port

http://tech.pro/tutorial/704/csharp-tutorial-simple-threaded-tcp-server

For example

using System;
using System.ServiceProcess;
using System.Diagnostics;
using System.Text;
using System.Net.Sockets;
using System.Threading;
using System.Net;

namespace MyService
{
    public class MyService : ServiceBase
    {
        
		private TcpListener tcpListener;
		private Thread listenThread;
		
		public MyService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            this.EventLog.WriteEntry("MyService Service Has Started");
			this.tcpListener = new TcpListener(IPAddress.Any, 5555);
			this.listenThread = new Thread(new ThreadStart(ListenForClients));
			this.listenThread.Start();
        }

        protected override void OnStop()
        {
            this.EventLog.WriteEntry("MyService Service Has Stopped");
        }

        private void InitializeComponent()
        {
            this.ServiceName = "MyService";
            this.CanStop = true;
            this.AutoLog = false;
            this.EventLog.Log = "Application";
            this.EventLog.Source = "MyService";
        }
		
		private void ListenForClients()
		{
		  this.tcpListener.Start();

		  while (true)
		  {
			//blocks until a client has connected to the server
			TcpClient client = this.tcpListener.AcceptTcpClient();

			//create a thread to handle communication 
			//with connected client
			Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
			clientThread.Start(client);
		  }
		}
		
		private void HandleClientComm(object client)
		{
		  TcpClient tcpClient = (TcpClient)client;
		  NetworkStream clientStream = tcpClient.GetStream();

		  byte[] message = new byte[4096];
		  int bytesRead;

		  while (true)
		  {
			bytesRead = 0;

			try
			{
			  //blocks until a client sends a message
			  bytesRead = clientStream.Read(message, 0, 4096);
			}
			catch
			{
			  //a socket error has occured
			  break;
			}

			if (bytesRead == 0)
			{
			  //the client has disconnected from the server
			  break;
			}

			//message has successfully been received
			ASCIIEncoding encoder = new ASCIIEncoding();
			System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));
		  }

		  tcpClient.Close();
		}
    }
}

Open in new window

0
 

Author Comment

by:davesnb
ID: 40600685
I am not having much luck building this, I think this is more to do with me being a newbie to programming and visual studio . is it possible to build this for me ?
0
 

Author Closing Comment

by:davesnb
ID: 40607717
Good walk through
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

What to do when Windows Update is not working correctly? What tools can I use to detect the cause of the malfunction problem? What does this numeric error code mean? These and other questions that you have been asking in the past are answered here (…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this Micro Tutorial viewers will learn how to restore their server from Bare Metal Backup image created with Windows Server Backup feature. As an example Windows 2012R2 is used.
This tutorial will walk an individual through the process of installing the necessary services and then configuring a Windows Server 2012 system as an iSCSI target. To install the necessary roles, go to Server Manager, and select Add Roles and Featu…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question