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
#4
(Jan-17-2019, 06:25 PM)pkm Wrote: 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?

Yes, in Python, a "socket" can be on both ends of the connection. There's no special distinction between listener/connector or server/client, they're just two ends of a pipe. That said, any time you're accepting connections on a socket, you can pretty much guarantee it's a server of some sort (or a peer to peer network, like for gaming, or Tor lol).
Quote
#5
The Python sockets module is a tin wrapper around C Socktes. It's a little bit abstracted.
A server and a client use the same method to create a socket (Adress family, Protocol, Flags)
After the socket creation you decide to bind, if you want to be a server or connect if you're
a tcp-client. If you're using udp, then use sendto as client. UDP is connections less.

If you use C, all this methods are functions, which returns a file-descriptor or error code.
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  UDP Socket Basic question zeeshannnetwork 2 768 Sep-10-2018, 02:25 AM
Last Post: zeeshannnetwork
  Raw socket sendto function errors IronReign 5 2,553 Jul-09-2017, 04:46 PM
Last Post: Blue Dog

Forum Jump:


Users browsing this thread: 1 Guest(s)