Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Question concerning function of a a socket
#1
My reverse engineering of torchat has led me to a question which I am unable to answer. When I look at the torchat source code, there is a file named tc_client.py, which contains the class “Listener“.

class Listener(threading.Thread):
    def __init__(self, buddy_list, socket=None):
        threading.Thread.__init__(self)
        self.buddy_list = buddy_list
        self.conns = []
        self.socket = socket
        self.start()
        self.startTimer()

    def run(self):
        self.running = True
        if not self.socket:
            interface = config.get("client", "listen_interface")
            port = config.getint("client", "listen_port")
            self.socket = tryBindPort(interface, port)
        self.socket.listen(5)
        while self.running:
            try:
                conn, address = self.socket.accept()
                self.conns.append(InConnection(conn, self.buddy_list))
                print "(2) new incoming connection"
                print "(2) have now %i incoming connections" % len(self.conns)
            except:
                print "socket listener error!"
                tb()
                self.running = False


    . . .
On line 19 of the code above, there is a socket on which the accept-method is called. As far as I know, the accept method can only be called on a server socket (I come from the JAVA world, where there exists a difference between a Socket and a ServerSocket object). Is the mentioned socket a server socket?
Quote
#2
I only recently started to delve into sockets, but here is the socket module documentation. As you can see there is only Socket object and no distinct ServerSocket object. Socket object is used in server and client. This Listener class looks like threaded server listening for client connections.

here are two more useful links for reference
Socket Programming HOWTO
Socket Programming in Python (Guide)

EDIT: There is separate socketserver module which simplifies the task of writing network servers.
Quote
#3
I don't know much about sockets either, but from what I have learned this code looks more like code for a server than a client. Perhaps why the script is named tc_client is because this file contains the part of the server that handles functions for the client like connections. But yes, this looks more like server code.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  socket programming ConnectionRefusedError error srm 3 92 May-16-2019, 08:07 PM
Last Post: LavaCreeperKing
  Socket reading script Will86 4 184 Apr-17-2019, 06:57 AM
Last Post: Will86
  Server and Network (socket) [WinError 10053] SheeppOSU 2 153 Apr-13-2019, 09:23 PM
Last Post: SheeppOSU
  Async socket server and ports Pengwyn 1 275 Feb-28-2019, 12:13 AM
Last Post: DeaD_EyE
  Multi connection socket server help! MuntyScruntfundle 0 187 Feb-19-2019, 12:03 PM
Last Post: MuntyScruntfundle
  socket programming (browser) kunz 8 623 Dec-18-2018, 10:13 AM
Last Post: siripriya
  Udp socket rocks88 3 448 Dec-02-2018, 01:26 AM
Last Post: Larz60+
  Multiple network socket servers? MuntyScruntfundle 1 372 Nov-13-2018, 03:46 PM
Last Post: wavic
  For Xilinx EthernetLite LWIP:Need help in Python SOCKET Coding Saras 1 502 Oct-01-2018, 05:16 AM
Last Post: Saras
  Python Socket programming with packets sourabhjaiswal92 1 930 Sep-18-2018, 06:24 AM
Last Post: martingever

Forum Jump:


Users browsing this thread: 1 Guest(s)