client-server socket script that crashes on Mac

B

Bad Mutha Hubbard

Hi. I know I have some bugs in this, but I'm trying to find out one
thing in particular: why the accept() method in the server never returns
in OSX. It announces that it is waiting, and the client announces that
it is attempting to connect, and both print the same port number, but it
just hangs. On Linux, it connects, sends the data, and terminates. I
run the command:
python ./client.py
There are two files below (I was unable to attach a zip).

Any ideas? Thanks.
-Chuckk


## client.py

#!/usr/bin/env python

import os
import threading
import sys
import subprocess
import socket
#import select
import Queue
import time

class client(object):
def __init__(self):
self.cbport = 5899
self.outport = 5880
self.playing = 0
self.allowed2play = 1
self.play()
time.sleep(1)
self.stop()

def waitforconnect(self, sock, q):
conn = sock.accept()
q.put(conn)

def delegatecallbacks(self, sock):
cbtext = ''
while self.playing == 1:
# if select.select((sock,),(),())[0]:
try:
cbtext += sock.recv(32)
while cbtext.count('CB'):
cb, cbtext = cbtext.split('CB', 1)
print self.__class__.__name__, "-", "Callback:", cb
if cb == 'END':
print self.__class__.__name__, "-", "END received"
self.stop()
except:
print self.__class__.__name__, "-", "Callback Socket Unavailable"

def play(self):
if self.allowed2play:
self.allowed2play = 0
if True:
cbwait = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
count = 0
while count < 30000:
try:
cbwait.bind(('127.0.0.1', self.cbport))
print self.__class__.__name__, "-", 'Callback Port: %s' % str(self.cbport)
count = 30000
except:
self.cbport += 1
count += 1
if count == 30000:
print self.__class__.__name__, "-", "NO PORTS AVAILABLE FOR CALLBACK"

cbwait.listen(2)
self.outsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

#thread to wait for callback to connect
q = Queue.Queue()
wait = threading.Thread(target=self.waitforconnect, args=(cbwait, q))
wait.start()

count = 0
while count < 10000:
tmpsoc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
tmpsoc.bind(('127.0.0.1', self.outport))
print self.__class__.__name__, "-", 'outport:', self.outport
count = 10000
del tmpsoc
except:
self.outport += 1
count += 1
print self.__class__.__name__, "-", "count:", count
del tmpsoc

self.server = subprocess.Popen((sys.executable, 'server.py', str(self.outport), str(self.cbport)))

# success = self.server.poll()
# if success:
# print self.__class__.__name__, "-", success
# break

connected = False
tries = 1
while not connected:
try:
self.outsock.connect(('127.0.0.1', self.outport))
print self.__class__.__name__, "-", 'outsock connected to:', self.outport
connected = True
except:
print self.__class__.__name__, "-", "outsock failed to connect to:", self.outport, tries, "times"
tries += 1
if tries >= 10:
raise connectionError
time.sleep(.1)

try: self.cbsock = q.get()[0]
except: pass
while wait.isAlive():
try: self.cbsock = q.get()[0]
except: pass

#cleanup dead thread
if not wait.isAlive():
del wait

self.playing = 1
threading.Thread(target=self.delegatecallbacks, args=(self.cbsock,)).start()

self.outsock.sendall('inputmsgENDMESSAGE')

def stop(self):
if self.playing == 1:
try:
self.playing = 0
# print self.__class__.__name__, "-", "Stop"
self.outsock.sendall('ENDMESSAGEstopENDMESSAGE')
# print self.__class__.__name__, "-", 'closing...'
# print self.__class__.__name__, "-", self.server.poll()
ended = self.server.communicate()
print self.__class__.__name__, "-", 'ended', ended
self.outsock.close()
self.cbsock.close()


# self.outsock.shutdown(socket.SHUT_RDWR)
# self.cbsock.shutdown(socket.SHUT_RD)
# del self.outsock
# del self.cbsock
# del sock
# print self.__class__.__name__, "-", self.server.communicate()
except: print self.__class__.__name__, "-", "Unable to Close Server"
self.internalstop()

def internalstop(self):
self.allowed2play = 1

class connectionError(Exception):
def __init__(self):
Exception.__init__(self)
print "Unable to connect"

if __name__ == "__main__":
cli = client()
######### end client.py




## server.py
import time
import os
import sys
import socket
import threading

class server:
def __init__(self, inport, cbport, stdout):
print self.__class__.__name__, "-", "Initializing..."
self.inport = inport
self.initsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print self.__class__.__name__, "-", "binding initsock..."
self.initsock.bind(('127.0.0.1', int(inport)))
print self.__class__.__name__, "-", "done"
self.initsock.listen(5)
self.flag = 0
self.inputloopthread = threading.Thread(target=self.waitforconnect)
self.inputloopthread.start()

self.cbport = int(cbport)
self.cbsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print self.__class__.__name__, "-", "Connecting to Callback Socket..."
self.cbsock.connect(('127.0.0.1', self.cbport))
print self.__class__.__name__, "-", "Connected to Callback Socket"

def waitforconnect(self):
print self.__class__.__name__, "-", "waiting for Input Socket to Connect...", self.inport
self.insock = self.initsock.accept()[0]
print self.__class__.__name__, "-", "Input Socket Connected"
self.inputflag = 1
self.inputloop()

def inputloop(self):
string = ''
while self.inputflag ==1:
string += self.insock.recv(64)
while string.count('ENDMESSAGE'):
cmd, string = string.split('ENDMESSAGE', 1)
self.cmddelegate(cmd)
print self.__class__.__name__, "-", "input loop ending"

def scoreloc(self):
self.flag = 1
while self.flag < 10:
print self.__class__.__name__, "-", "flag:", self.flag
self.flag += 1
time.sleep(.125)
self.flag = 0
try:
self.cbsock.sendall('ENDCB')
except:
print self.__class__.__name__, "-", "Unable to send ENDCB"
print self.__class__.__name__, "-", "scoreloc thread ending"

def cmddelegate(self, cmd):
print self.__class__.__name__, "-", 'Command Received:', cmd
if cmd.startswith('stop'):
self.flag = 0
self.inputflag = 0
print self.__class__.__name__, "-", "Stopped"


sv = server(sys.argv[1], sys.argv[2], sys.stdout)
############## end server.py
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top