By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,758 Members | 1,223 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,758 IT Pros & Developers. It's quick & easy.

win32 service and sockets

P: n/a

I created a win32 service for XPPro called (see below).
The service runs as a particular user with administrative rights. It starts a
thread that creates a simple socket server ( -- also
below) that just waits for 20 character string. When I run the socket server
by itself the test client can connect to the server and send a 20 character
string just fine. When I run the service, the server will bind to the port
but the client cannot connect. I verified the server was listening on the
given port using netstat -an. The client eventually times out. Why isn't the
server accepting connections when run in a service?

import win32serviceutil
import win32service
import win32event
import N4010ASocketServer
from thread import start_new_thread

class N4010ATestService(win32serviceutil.ServiceFramewor k):
_svc_name_ = "N4010ATestService"
_svc_display_name_ = "N4010A Test Service"
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)

def SvcDoRun(self):
start_new_thread(N4010ASocketServer.main, ())

def SvcStop(self):
# Before we do anything, tell the SCM we are starting the stop process.
self.ReportServiceStatus(win32service.SERVICE_STOP _PENDING)

if __name__ == '__main__':
win32serviceutil.HandleCommandLine(N4010ATestServi ce)
from socket import socket
from select import select
from time import sleep
import win32evtlogutil

applicationName = 'N4010ASocketServer'
messageDll = 'C:\Python24\Lib\site-packages\win32\pythonservice.exe'
running = True
stopped = False

def registerWithEventViewer():
win32evtlogutil.AddSourceToRegistry(applicationNam e, messageDll)

def stop():
import servicemanager
global running
running = False

def waitfor():
while not stopped:

def main():
import servicemanager
servicemanager.LogInfoMsg('creating socket')
sock = socket()
servicemanager.LogInfoMsg('binding to port 48777')
sock.bind(('', 48777))
while running:
servicemanager.LogInfoMsg('Waiting for connection.')
readyRead, readyWrite, inerror = select([sock], [], [], 0)
while (not readyRead) and running:
readyRead, readyWrite, inerror = select([sock], [], [], 0)
if running:
conn, address = sock.accept()
msg = conn.recv(20)
servicemanager.LogInfoMsg('Recvd msg: %s' % msg)
global stopped
stopped = True
Jul 18 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.