Socket Basics
https://docs.python.org/2/library/socket.html?highlight=pton\#module-socket
- To create a socket:
import socket
s = socket.socket(addr_family, type)
- Address Families:
socket.AF_INET Internet protocol (IPv4)
socket.AF_INET6 Internet protocol (IPv6)
- Socket Types:
socket.SOCK_STREAM Connection based stream (TCP)
socket.SOCK_DGRAM Datagrams (UDP)
Creating a socket is only the first step
s = socket(AF_INET, SOCK_STREAM)
Further use depends on application
- Server - Listen for incoming connections
- Client - Make an outgoing connection
TCP Server code will deal with TWO (or more) sockets
- The first socket is created by socket() and is used to LISTEN
- The other sockets are returned by accept() after a client connects to the listening socket. This new socket is used for data transfer to that specific client
- Accept() returns two values, the socket, and the IP of the other side.
Keep in mind
- Calls to accept() will NOT alter the LISTENING socket
- By default, accept() will block until a connection is received