Python Forum
Question concerning function of a a socket
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?
Reply
#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.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#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.
Reply
#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).
Reply
#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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  socket.connect question about parenthesis pace 1 2,317 Jan-30-2021, 08:17 AM
Last Post: Gribouillis
  Function that searches and shows all socket hosts shaanukstar123 1 2,133 Jan-30-2020, 05:52 PM
Last Post: Gribouillis
  UDP Socket Basic question zeeshannnetwork 2 3,078 Sep-10-2018, 02:25 AM
Last Post: zeeshannnetwork
  Raw socket sendto function errors IronReign 5 6,435 Jul-09-2017, 04:46 PM
Last Post: Blue Dog

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020