Linux Sockets David Ferry Chris Gill CSE 522
- Slides: 8
Linux Sockets David Ferry, Chris Gill CSE 522 S - Advanced Operating Systems Washington University in St. Louis, MO 63130 1
Communication API Wish List 1. Protocol abstraction 2. Endpoint abstraction 3. Address abstraction Would give flexibility and substitutability Process A File I/O Socket ? Socket File I/O Process B Desired Socket Protocol CSE 522 S – Advanced Operating Systems 2
Communication API Wish List 1. Protocol abstraction 2. Endpoint abstraction 3. Address abstraction Would give flexibility and substitutability Process A File I/O Socket IPv 4 Socket File I/O Process B Socket Protocols: IPv 4, IPv 6, local, etc… CSE 522 S – Advanced Operating Systems 3
Communication API Wish List 1. Protocol abstraction 2. Endpoint abstraction 3. Address abstraction Would give flexibility and substitutability Process A File I/O Socket IPv 4 Socket File I/O Process C Socket Protocols: IPv 4, IPv 6, local, etc… CSE 522 S – Advanced Operating Systems 4
Communication API Wish List 1. Protocol abstraction 2. Endpoint abstraction 3. Address abstraction Would give flexibility and substitutability Process A File I/O Pipe Implementation Pipe File I/O CSE 522 S – Advanced Operating Systems Process C 5
Sockets (aka Berkeley Sockets or POSIX Sockets) • Are communications endpoints • Are connected by user-specified protocols (TCP/IPv 4, UDP/IPv 6, TCP/local, etc…) • Are read and written as though a file • Are bidirectional CSE 522 S – Advanced Operating Systems 6
Creating a Socket The socket is an abstract endpoint: • Endpoint of what? • A socket must be bound to an address Steps: 1. Create with socket() Process A If server: Process B 2. Bind with bind() 3. Wait for connection with listen() and accept() Socket Address: Port 80 Socket Address: /home/pi/mysock If client: 2. Connect with connect() (implicit binding) CSE 522 S – Advanced Operating Systems 7
Sockets vs. Pipes (and FIFOs) • Both are read as though they are files – Pipes connect processes within a single machine – Sockets may connect processes on different hosts • Pipes are unidirectional • Sockets are bidirectional • FIFOs allow for multiple readers and writers • Each socket should only have one threading from it at once (else there’s a potential concurrency hazard) – But you can open multiple sockets, and/or synchronize threads CSE 522 S – Advanced Operating Systems 8