1 of 24

Sockets

What are they? 🔌

Ye Lin Aung

2 of 24

What is the first thing that comes to mind?

Not like this …

This thing ???

3 of 24

This is about (Linux) sockets

  • It is a way to send messages across a network
  • Imagine Bob want to say something to Alice
  • Bob has a number “111-111” and Alice has “111-222”
  • Bob can call Alice only when Bob knows the Alice’s number.
  • Alice can accept or reject the incoming call.
  • Upon the acceptance from Alice, Bob can send some messages.
  • Likewise, Alice can return some messages.

4 of 24

What type of Sockets are there?

  • Unix Sockets for Inter Process Communication (IPC)
    • Sending messages back and forth between “processes” in the same OS
    • Look for “Docker socket” for example!
  • Network Sockets (IP Sockets)
    • TCP Sockets
    • UDP Sockets

5 of 24

What’s below and above them ?

6 of 24

How does the TCP/UDP sockets work?

  • Imagine Bob is the “Client”
  • Alice is the “Server”
  • This is a TCP Socket
    • Also known as Berkeley Socket

7 of 24

How are they sending the data over ?

  • Over the internet, the data is going through many devices
    • Routers
    • Switches
    • Hub

8 of 24

How are they sending the data over ?

  • Locally, (i.e within your own computer)
  • There is a special network device called “loopback” interface
  • Also known as 127.0.0.1
  • Which is bind to “localhost” in /etc/hosts

9 of 24

Demo time!

10 of 24

The (in)famous TCP 3-Way Handshake 🤝

11 of 24

SYN – SYN,ACK – ACK 🤝

12 of 24

SYN – SYN,ACK – ACK 🤝 but in Terminal

13 of 24

“pdb” ( 🐍 Python Debugger) cheatsheet

  • b: set a breakpoint
  • c: continue debugging until you hit a breakpoint
  • s: step through the code
  • n: to go to next line of code
  • l: list source code for the current file (default: 11 lines including the line being executed)
  • u: navigate up a stack frame
  • d: navigate down a stack frame
  • p: to print the value of an expression in the current context

14 of 24

This is cool but … 🤔

  • That “toy” server dies after doing some stuff with an incoming request ???
    • Maybe one request per process is the what they cool kids call “Lambda” ??
  • What if we want to accept multiple connections at the same time?
  • And process them “asynchronously”
  • Can we have the 🎂 and eat it too ???
  • Hmmm….🤔

15 of 24

Enter “concurrency” 🥁

  • There are many ways to do it - in Python
  • The popular "asyncio" — Asynchronous I/O
  • Good ol’ threading
    • But … GIL be watching you
  • Or .. we go back to one request, one process but in a cooler way with multiprocessing — Process-based parallelism 😉

16 of 24

But today .. we are going back to stone age

  • Here comes “selectors”
  • After all, network sockets are nothing more than “input/output” (IO) operations
  • The more efficient operations can be, the more requests we can proces
    • I didn’t say faster lol
  • There are many iteration of this throughout.
    • In Linux/Unix-like systems, there are select(), poll(), epoll()

17 of 24

OK enough with all these slides! Show me the code

  • Here are the scripts I used in the demo

https://gist.github.com/yelinaung/57e817eeac0c89487a5a3a621ee6eb69

18 of 24

I/O Multiplexing

  • A technique that allows a program to monitor multiple input/output (I/O) channels simultaneously (sockets)
  • Without the need for multiple threads or processes
  • Like a juggler who can keep an eye on several balls in the air at once, rather than focusing on just one ball at a time!

19 of 24

What are select(), poll(), epoll() ?

  • There are “system call” (an interface for User/Kernel space) in Linux
  • They syscalls allow a process to monitor multiple socket to read, write, or handle exceptions without blocking on each socket individually.
  • There are small differences and similarities between each of them but basically, in terms of performance and scalability, it is like

epoll() > poll() > select()

20 of 24

There’s a new kid in town “io_uring"

21 of 24

A bonus - fetch( .. )

  • Browser API that fetches things given a valid URL
    • Fetch API
  • Yes, it’s based on the same concept
  • But .. there are much more going-on …

22 of 24

What’s next?

  • Ok, TCP was cool. Where do I learn more about it ?
  • How about UDP ? Does it do the same “3 way handshake” ?
    • (Hint: no)
  • How about TLS ?
  • How about Async ? Eventloop ? Maybe, that’s the next talk!
  • Finally, what is the point of all these?

23 of 24

Thank you

Link to the talk https://bit.ly/yelin-socket

24 of 24

Feedback pls 🙏