Advertisement

Multi-Player socket question. Please help.

Started by March 03, 2001 03:03 PM
1 comment, last by posterboy 23 years, 9 months ago
I''m a novice game programer and I added some multiplayer components to my program. I have no real experience doing multiplayer programming so I just took a shot at it and I''m wondering if there are any problems in my implementation. OK, here it goes. First, I''m coding in python, but I''m using standard sockets. I''m not using directly. This needs to work on multiple platforms. Obviously I have a host. The host has on thread with a socket listening for incoming connections. After it gets a new connection, I initialize two separate threads: one for writing to a socket and one for reading from a socket. Both these thread use a thread safe queue to get/put data respectively. Some code is below (python): My main concern is that sometimes I get "broken pipe errors" and "connection reset by peer". Are theses normal socket errors? The broken pipe error got me thinking. Do I need to have some sort of thread syncornization between the two threads that send/recv data on the same socket? Can you send and recv on the same socket from two different threads? Or could this a be race condition? Any advice will be helpful. Thanks! def recv_func(self,arg): self.log_msg("rec thread running...") #f = self.sock.makefile() buffer = "" bytes = 0 while self.status == MPLAY_CONNECTED: try: data = self.sock.recv(MPLAY_PACKET_SIZE) bytes = len(data) self.log_msg("Got: " + " Bytes:" + str(bytes)) if bytes == 0: self.log_msg("Error mplay recv thread! O bytes") self.set_status(MPLAY_OUTSYNC) index = data.find(''\0'') if index == -1: buffer = buffer + data else: while index != -1: buffer = buffer + data[:index] self.inbox.put(buffer) buffer = "" data = data[index+1:] index = data.find(''\0'') buffer = data except Exception, e: self.set_status(MPLAY_OUTSYNC) self.log_msg(e) break self.log_msg("rec thread exiting...") self.recv_event.set() def send_func(self,arg): self.log_msg("send thread running...") while self.status == MPLAY_CONNECTED: data = self.outbox.get() if len(data) > 0: try: b = self.sock.send(data+''\0'') self.log_msg("sent: " + str(b)) except Exception, e: self.set_status(MPLAY_OUTSYNC) self.log_msg(e) break self.log_msg("send thread exiting") self.send_event.set()
Damn, It didn''t peserve my code above. I''m gonna try and put it in a preformat tag. Sorry!
def recv_func(self,arg):        self.log_msg("rec thread running...")        #f = self.sock.makefile()        buffer = ""               bytes = 0        while self.status == MPLAY_CONNECTED:            try:                data = self.sock.recv(MPLAY_PACKET_SIZE)                                bytes = len(data)                self.log_msg("Got: " + " Bytes:" + str(bytes))                if bytes == 0:                                        self.log_msg("Error mplay recv thread! O bytes")                      self.set_status(MPLAY_OUTSYNC)                index = data.find(''\0'')                if index == -1:                    buffer = buffer + data                else:                    while index != -1:                        buffer = buffer + data[:index]                        self.inbox.put(buffer)                        buffer = ""                        data = data[index+1:]                                        index = data.find(''\0'')                    buffer = data            except Exception, e:                self.set_status(MPLAY_OUTSYNC)                self.log_msg(e)                break        self.log_msg("rec thread exiting...")        self.recv_event.set()                def send_func(self,arg):        self.log_msg("send thread running...")        while self.status == MPLAY_CONNECTED:            data = self.outbox.get()                        if len(data) > 0:                try:                    b = self.sock.send(data+''\0'')                    self.log_msg("sent: " + str(b))                except Exception, e:                    self.set_status(MPLAY_OUTSYNC)                    self.log_msg(e)                    break        self.log_msg("send thread exiting")        self.send_event.set()  
Advertisement
Damn, It didn''t peserve my code above. I''m gonna try and put it in a preformat tag. Sorry!
def recv_func(self,arg):        self.log_msg("rec thread running...")        #f = self.sock.makefile()        buffer = ""               bytes = 0        while self.status == MPLAY_CONNECTED:            try:                data = self.sock.recv(MPLAY_PACKET_SIZE)                                bytes = len(data)                self.log_msg("Got: " + " Bytes:" + str(bytes))                if bytes == 0:                                        self.log_msg("Error mplay recv thread! O bytes")                      self.set_status(MPLAY_OUTSYNC)                index = data.find(''\0'')                if index == -1:                    buffer = buffer + data                else:                    while index != -1:                        buffer = buffer + data[:index]                        self.inbox.put(buffer)                        buffer = ""                        data = data[index+1:]                                        index = data.find(''\0'')                    buffer = data            except Exception, e:                self.set_status(MPLAY_OUTSYNC)                self.log_msg(e)                break        self.log_msg("rec thread exiting...")        self.recv_event.set()                def send_func(self,arg):        self.log_msg("send thread running...")        while self.status == MPLAY_CONNECTED:            data = self.outbox.get()                        if len(data) > 0:                try:                    b = self.sock.send(data+''\0'')                    self.log_msg("sent: " + str(b))                except Exception, e:                    self.set_status(MPLAY_OUTSYNC)                    self.log_msg(e)                    break        self.log_msg("send thread exiting")        self.send_event.set()  

This topic is closed to new replies.

Advertisement