questions about sockets

Hello! Here are some questions & answers. The goal isn't to get all the questions "right". Instead, the goal is to learn something! If you find a topic you're interested in learning more about, I'd encourage you to look it up and learn more.

do programs always use sockets to make HTTP connections?

pretty much!

if a program on your computer is connecting to the internet in any way, it's almost certainly using the socket API!

write some code: use sockets to send the string "hello!" to 1.2.3.4:4321 using TCP

in Python:

import socket
s = socket.socket(
    socket.AF_INET,
    socket.SOCK_STREAM)
s.connect(
    ("1.2.3.4", 4321))
s.send(b"hello!")
        

SOCK_STREAM is used to create TCP connections. AF_INET means "the internet".

write some code: use sockets to send a UDP packet to 127.0.0.1:1234

in Python:

import socket
s = socket.socket(
    socket.AF_INET,
    socket.SOCK_DGRAM)
s.sendto(b'hi!', 
    ('127.0.0.1', 1234))
        

SOCK_DGRAM is used to send UDP packets

write some code: use sockets to create a UDP server listening on port 1234

in Python:

import socket
s = socket.socket(
    socket.AF_INET,
    socket.SOCK_DGRAM)
s.bind(('0.0.0.0', 1234))
# read 1 packet
s.recvfrom(65000)
        

what does binding to "0.0.0.0" do?

listen on every network interface

0.0.0.0 isn't a real IP address. When passed to bind, it's code for "listen for connections on every network interface". Often this means the loopback interface (127.0.0.1 etc) and your ethernet/wireless card.

0.0.0.0 is also known as INADDR_ANY.

what kind of socket does ping use?

a raw socket (SOCK_RAW)

raw sockets let you send packets with protocols other than UDP or TCP. ping sends ICMP packets so it needs to use a raw socket.

where is the code that actually sends the network packets when you write to a socket?

in your operating system!

for example, on Linux, the code that implements TCP and UDP is in the Linux kernel.

do sockets only let you connect to the Internet?

nope! there are lots of kinds of sockets!

one important kind on Unix is "unix domain sockets", which let you listen on/connect to a file instead of an IP address

write some code: use the socket API to create a server that listens on a Unix domain socket instead of an IP address

in Python:

import socket
s = socket.socket(
    socket.AF_UNIX,
    socket.SOCK_DGRAM)
s.bind('/tmp/my.socket')
print(s.recvfrom(10000))
        

does your program need to decide what should go in each TCP packet a stream socket sends?

nope!

it can just write the bytes it wants to send to the socket (like an HTTP response) and the operating system will decide how to split it up into packets.

when you write the string "hello" to a UDP socket, does it send a packet right away?

yes!

it'll immediately send a UDP packet with the contents "hello". It won't buffer or anything.

when you write the string "hello" to a TCP socket, does it send a packet right away?

not necessarily!

your operating system might instead put "hello" in a buffer, wait for more writes, and combine them into a single TCP packet it sends later. It depends on how you've configured the socket.