Socket programming with a service

Thread Solved

Join Date: Jan 2008
Posts: 18
Reputation: mg0959 is an unknown quantity at this point 
Solved Threads: 0
mg0959 mg0959 is offline Offline
Newbie Poster

Socket programming with a service

 
0
  #1
May 26th, 2009
I have written a program that is supposed to take a string via a socket connection and echo it back. This program is also run as a service. I can get a client program to connect to the service if the client program is on the same machine as the service, but if I try to connect from another machine, the client program will not connect. Does anyone have any idea about what might be wrong and how to fix it?

Thanks in advance
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 122
Reputation: slate is an unknown quantity at this point 
Solved Threads: 30
slate slate is offline Offline
Junior Poster

Re: Socket programming with a service

 
0
  #2
May 26th, 2009
Have you checked the firewall?
Try open the server port higher (>5000).

Check out if minimal implementations are working.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 1,606
Reputation: scru has a spectacular aura about scru has a spectacular aura about 
Solved Threads: 130
Featured Poster
scru's Avatar
scru scru is offline Offline
Posting Virtuoso

Re: Socket programming with a service

 
0
  #3
May 26th, 2009
Most likely issue is a firewall.

After that there's routers (port forwarding stuff) and proxies. Are you trying to connect over the internet, or on the same subnet?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 1,046
Reputation: jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough jlm699 is a jewel in the rough 
Solved Threads: 264
Sponsor
jlm699's Avatar
jlm699 jlm699 is offline Offline
Knows where his Towel is

Re: Socket programming with a service

 
0
  #4
May 26th, 2009
You can debug this yourself. Try pinging that ip address and port.

Helps to find root cause first
1. Use Code Tags.
2. Homework? Show Effort.
3. Keep discussions on the forum: no PMs
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 18
Reputation: mg0959 is an unknown quantity at this point 
Solved Threads: 0
mg0959 mg0959 is offline Offline
Newbie Poster

Re: Socket programming with a service

 
0
  #5
May 27th, 2009
Sorry to be so vague... I will supply the code to clarify.

I don't think there is a problem with firewalls. The program will work if I do not run it as a service, and the error I get on the client side is connection refused.

Here is my code for the service function... called service_module.py

  1. import time
  2. import thread
  3. import os
  4. import sys
  5. from socket import *
  6. from relocator import relocate # this function changes the cwd to the program's physical location
  7.  
  8. def handleClient(connection): # in spawned thread: reply
  9. print connection, time.ctime() # show this client's address
  10. data = connection.recv(1024) # read client socket
  11. try:
  12. response = 'Server got: ' + data
  13. except:
  14. sys.stderr.write('\n\nerror in function! ')
  15. sys.stderr.write("%s, %s\n," % tuple(sys.exc_info()[:2]))
  16. response = 'There was an error with the server.'
  17. connection.send(response) # send a response
  18. connection.close()
  19.  
  20.  
  21. class service_test:
  22. def __init__(self):
  23. thread.start_new(self.do_something, tuple()) # this is run in a thread so that the service can respond to stop requests
  24. while True:
  25. if getattr(sys,'stopservice', False):
  26. sys.exit()
  27. time.sleep(0.3)
  28.  
  29. def do_something(self):
  30. '''
  31. Do something
  32. '''
  33. myHost = '' # server machine, '' means local host
  34. myPort = 50007 # listen on a non-reserved port number
  35.  
  36. sockobj = socket(AF_INET, SOCK_STREAM) # make a TCP socket object
  37. sockobj.bind((myHost, myPort)) # bind it to server port number
  38. sockobj.listen(5) # allow up to 5 pending connects
  39. st = '\nIm starting... ' + time.ctime() + '\n'
  40. print st
  41. print str(os.getcwd())
  42. print str(os.path.dirname(sys.argv[0]))
  43. relocate() # this just changes the cwd to the program's physical location
  44. print '\ncwd: '
  45. print str(os.getcwd())
  46. print '\n'
  47.  
  48.  
  49. while True:
  50. connection, address = sockobj.accept() # pass to thread for service
  51. print 'Server connected by', address,
  52. print 'at', time.ctime(), '\n'
  53. thread.start_new(handleClient, (connection,))
  54.  
  55.  
  56.  
  57. if __name__ == "__main__":
  58. tst = service_test()

service_module.py is imported in the following code. This code is the serviceLauncher.py... This function is sent to py2exe for compiling.

  1. import win32serviceutil
  2. import win32service
  3. import win32event
  4. import os
  5. import sys
  6. import time, redirect
  7.  
  8. sys.stopdriver = "false"
  9.  
  10. def main():
  11. '''
  12. Modulo principal para windows
  13. '''
  14. sys.path.insert(0,os.getcwd())
  15. import service_module
  16. a = service_module.service_test() # this is the funciton from the service_module.py
  17.  
  18. class ServiceLauncher(win32serviceutil.ServiceFramework):
  19. _svc_name_ = 'ServiceTest'
  20. _scv_display_name_ ='ServiceTest'
  21.  
  22. def __init__(self, args):
  23. win32serviceutil.ServiceFramework.__init__(self, args)
  24. self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
  25.  
  26. def SvcStop(self):
  27. self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
  28. sys.stopservice = "true"
  29. win32event.SetEvent(self.hWaitStop)
  30.  
  31. def SvcDoRun(self):
  32. redirect.redirect(main, '') # this is a stream redirection function...
  33. # it is run like this for debugging purposes
  34. # this should not affect anything... I can supply this code too if I need to


Here is the client code...


  1. import sys, os, time
  2. from socket import * # portable socket interface plus constants
  3. serverHost = 'localhost' # replaced with the IP address of server prog
  4. serverPort = 50007 # non-reserved port used by the server
  5.  
  6.  
  7.  
  8. message = raw_input('Enter message: ') # default text to send to server
  9.  
  10. sockobj = socket(AF_INET, SOCK_STREAM) # make a TCP/IP socket object
  11. sockobj.connect((serverHost, serverPort)) # connect to server machine and port
  12.  
  13. sockobj.send(message)# send message to server over socket
  14. data = sockobj.recv(1024) # receive line from server: up to 1k
  15. print 'Client received:', repr(data) # make sure it is quoted, was `x`
  16. print 'Client recieved data at', time.ctime(time.time())
  17.  
  18. sockobj.close() # close socket to send eof to server
  19.  
  20. raw_input('Press enter when done')


I hope this helps... thanks for the help
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 18
Reputation: mg0959 is an unknown quantity at this point 
Solved Threads: 0
mg0959 mg0959 is offline Offline
Newbie Poster

Re: Socket programming with a service

 
0
  #6
May 29th, 2009
It was a firewall issue afterall
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC