P
Paul Tulou
Hello,
I have created a Windows service that uses the Socket class to listen
for incoming connections. I have tested my socket code in a normal
windows application for easy debugging and everything worked fine.
When I transfered this code to the service and started it up, the
service runs fine and stops fine, but any messages sent via telnet to
the IP are simply not received. No connection is ever made. Keep in
mind that the only difference (that I have found) between the working
and non-working versions is the type of application (app vs. service).
I am currently running the service in LocalSystem mode, although I have
also tried running it under my user login that has administrative
privileges. Both worked the same. Should there be any restrictions on
Sockets use based on which Account is used for the service?
The only thing I can think is that maybe there are extra setup steps
that I need to go through in order to get sockets to work with my
service. I have included my code below. The BeginAccept method
executes fine but the AcceptCallback method never gets executed until
the OnStop method closes the socket.
Any help with this would be greatly appreciated, I haven't had much
luck finding documentation on this.
namespace VerizonServer
{
public partial class VerizonServer : ServiceBase
{
private string outputPath;
private int port;
private Socket socket;
private TextLog log;
public VerizonServer()
{
InitializeComponent();
if
(!System.Diagnostics.EventLog.SourceExists("VerizonServerService"))
System.Diagnostics.EventLog.CreateEventSource("VerizonServerService",
"VerizonServerLog");
eventLog.Source = "VerizonServerService";
eventLog.Log = "VerizonServerLog";
log = new TextLog(Application.StartupPath + @"\Logs",
"VerizonLog");
}
protected override void OnStart(string[] args)
{
try
{
base.OnStart(args);
outputPath =
ConfigurationSettings.AppSettings.Get("OutputPath");
port =
int.Parse(ConfigurationSettings.AppSettings.Get("Port"));
IPEndPoint localEndPoint = new
IPEndPoint(IPAddress.Any, port);
socket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
socket.Bind(localEndPoint);
socket.Listen(100);
socket.BeginAccept(new AsyncCallback(AcceptCallback),
socket);
string message = string.Format("Service started
successfully.\r\n\tOutput Path:\t{0}\r\n" +
"\tPort:\t{1}", outputPath, port);
eventLog.WriteEntry(message);
log.LogEvent(message);
}
catch (Exception ex)
{
eventLog.WriteEntry(ex.ToString());
log.LogEvent(ex.ToString());
}
}
protected override void OnStop()
{
socket.Close();
base.OnStop();
eventLog.WriteEntry("Service stopped successfully.");
log.LogEvent("Service stopped successfully.");
}
private void AcceptCallback(IAsyncResult ar)
{
// Code omitted: I process the the data received upon
connection here
}
}
}
I have created a Windows service that uses the Socket class to listen
for incoming connections. I have tested my socket code in a normal
windows application for easy debugging and everything worked fine.
When I transfered this code to the service and started it up, the
service runs fine and stops fine, but any messages sent via telnet to
the IP are simply not received. No connection is ever made. Keep in
mind that the only difference (that I have found) between the working
and non-working versions is the type of application (app vs. service).
I am currently running the service in LocalSystem mode, although I have
also tried running it under my user login that has administrative
privileges. Both worked the same. Should there be any restrictions on
Sockets use based on which Account is used for the service?
The only thing I can think is that maybe there are extra setup steps
that I need to go through in order to get sockets to work with my
service. I have included my code below. The BeginAccept method
executes fine but the AcceptCallback method never gets executed until
the OnStop method closes the socket.
Any help with this would be greatly appreciated, I haven't had much
luck finding documentation on this.
namespace VerizonServer
{
public partial class VerizonServer : ServiceBase
{
private string outputPath;
private int port;
private Socket socket;
private TextLog log;
public VerizonServer()
{
InitializeComponent();
if
(!System.Diagnostics.EventLog.SourceExists("VerizonServerService"))
System.Diagnostics.EventLog.CreateEventSource("VerizonServerService",
"VerizonServerLog");
eventLog.Source = "VerizonServerService";
eventLog.Log = "VerizonServerLog";
log = new TextLog(Application.StartupPath + @"\Logs",
"VerizonLog");
}
protected override void OnStart(string[] args)
{
try
{
base.OnStart(args);
outputPath =
ConfigurationSettings.AppSettings.Get("OutputPath");
port =
int.Parse(ConfigurationSettings.AppSettings.Get("Port"));
IPEndPoint localEndPoint = new
IPEndPoint(IPAddress.Any, port);
socket = new Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
socket.Bind(localEndPoint);
socket.Listen(100);
socket.BeginAccept(new AsyncCallback(AcceptCallback),
socket);
string message = string.Format("Service started
successfully.\r\n\tOutput Path:\t{0}\r\n" +
"\tPort:\t{1}", outputPath, port);
eventLog.WriteEntry(message);
log.LogEvent(message);
}
catch (Exception ex)
{
eventLog.WriteEntry(ex.ToString());
log.LogEvent(ex.ToString());
}
}
protected override void OnStop()
{
socket.Close();
base.OnStop();
eventLog.WriteEntry("Service stopped successfully.");
log.LogEvent("Service stopped successfully.");
}
private void AcceptCallback(IAsyncResult ar)
{
// Code omitted: I process the the data received upon
connection here
}
}
}