-
March 19th, 2007, 02:47 PM
#1
[RESOLVED] HELP! Listener works, but not as Service
Somebody please enlighten me!
I am trying to write a port listener Windows Service. I have the code working just fine as long as I run it as a normal Windows Form app. But when I try to incorporate the same code into a Windows Service program, it doesn't work, and I never get my Callback function to fire.
Any of you gurus out there got any words of wisdom for me?
Here is the code that works, in its entirety
Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Net;
using System.Net.Sockets;
namespace WindowsApplication1
{
public class Form1 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;
private Socket listenSocket;
private AsyncCallback ClientConnect;
private System.Windows.Forms.Label label;
private IPEndPoint ipEndPoint;
public Form1()
{
InitializeComponent();
ClientConnect = new AsyncCallback(OnClientConnect);
ipEndPoint = new IPEndPoint (IPAddress.Any, 2500);
listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
listenSocket.Bind(ipEndPoint);
listenSocket.Listen(10);
listenSocket.BeginAccept(ClientConnect, null);
}
private void OnClientConnect(IAsyncResult ar)
{
try
{
Socket s = listenSocket.EndAccept(ar);
IPAddress ip = ((IPEndPoint)s.RemoteEndPoint).Address;
LogMessage("Connected to "+ip.ToString());
listenSocket.BeginAccept(ClientConnect, null);
}
catch(ObjectDisposedException)
{
LogMessage("ClientConnection: Listening Socket has been Closed\n");
}
catch(SocketException se)
{
LogMessage("ClientConnection Error: "+se.Message);
}
}
private void InitializeComponent()
{
this.label = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label
//
this.label.BackColor = System.Drawing.Color.White;
this.label.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.label.Location = new System.Drawing.Point(6, 8);
this.label.Name = "label";
this.label.Size = new System.Drawing.Size(518, 257);
this.label.TabIndex = 0;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(531, 273);
this.Controls.Add(this.label);
this.Name = "Form1";
this.ResumeLayout(false);
}
void LogMessage(string text)
{
label.Text += text+"\n";
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if (disposing && components != null) components.Dispose();
base.Dispose( disposing );
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
}
}
Whenever I start the app, I get messages from any client who tries to connect to my port 2500. But I can't make it do the same thing when I translate it to a Windows service. I know that the service is running, but I can't get any connections. HELP PLEASE!
-
March 19th, 2007, 03:29 PM
#2
Re: HELP! Listener works, but not as Service
my first thougth would be to not have that code in a form object.
my second thought would be to check your socket permissions. when you run as a service, it runs under a system identity unless you specify otherwise. so, it may not allow incoming connections.
my third thought (which actually relates to the first) would be to catch all the possible exceptions BeginAccept would throw (there are 5) and log them.
-
March 19th, 2007, 04:13 PM
#3
Re: HELP! Listener works, but not as Service
Thanks for the reply.
First of all, in the Windows Service, all this stuff is not in a Form object. They are just private members of the Service class.
Secondly, I do not seem to be getting any exceptions thrown around the BeginAccept or the EndExcept methods.
Thirdly, maybe it is something to do with socket permissions. Looking at the MSDN information about SocketPermission and SecurityElement, one might as well be looking at it in a foreign language. It is as clear as mud. Does anyone have a quick example of how to give unlimited access to a particular port (say port 2500 on 192.168.2.235 for example)?
-
March 19th, 2007, 04:39 PM
#4
Re: HELP! Listener works, but not as Service
while you may not be getting any other exceptions when it runs as you, when it runs as a service (under the system context), it may throw a security exception when trying to accept a connection.
open the service in the service manager, and change the logon as to you, re-run it, and if you get connections, then thats the problem.
the documentation for socket permission on msdn shows an example of how to implement it, but here's a cliff notes version:
Code:
SocketPermission perms = new SocketPermission(PermissionState.None);
perms.AddPermission(NetworkAccess.Accept, TransportType.All, "localhost", portNumber);
perms.AddPermission(NetworkAccess.Accept, TransportType.All, "192.168.2.235", portNumber);
perms.AddPermission(NetworkAccess.Accept, TransportType.All, Dns.GetHostName(), portNumber);
if(!perms.IsUnrestricted()) {
try {
perms.Demand();
} catch(SecurityException) {
// cant gain permissions
}
}
// socket code here
Last edited by MadHatter; March 19th, 2007 at 04:43 PM.
-
March 19th, 2007, 05:37 PM
#5
Re: HELP! Listener works, but not as Service
ARRGGHH - Nothing WORKS!
I tried the user account, with the same results.
I tried adding in the code you sent me for Permissions, same results.
Why is this so darned HARD!
I don't suppose anyone has a working example that they could send me of a Windows service that listens on a specific port?
If so, I would be greatly relieved, and less depileated!
MadHatter, thanks for your help so far! It's gurus like you who keep programming fun (for the most part)!
-
March 20th, 2007, 08:32 AM
#6
Re: HELP! Listener works, but not as Service
I did some further testing, and found that even after I call the Demand() method, the IsUnrestricted() method returns false. So, I guess I still am not getting the access that I am wanting.
-
March 21st, 2007, 01:12 PM
#7
Re: HELP! Listener works, but not as Service
Well, I finally figured out what the problem was. Windows Firewall was blocking access to the port. I enabled the port thru the Firewall configuration and all is well. I don't know why I wasn't getting some kind of an exception. I am using .NET 1.1 and there is no SecurityException generated by the BeginAccept or EndAccept methods.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|