Socket Programming
Internet Edinanness
- The Internet standardised on big-endian
What is a protocol and what are 2 things it might describe?
- A protocol describes exactly how the communication should happen. E.g. - What order are the bits sent in? - What do the individual bits mean? - Necessary so the computer at the other end can understand the communication - Must be precise
TCP connections
- Gives us a reliable, stream-oriented connection to another machine - Once machine needs to be listening for a connection on a specific port - Second machine needs to connect to that open port - Once open, the connection is bidirectional
htonl() / htons()
- Host to network long/short - Converts a 32-bit (long) or 16-bit (short) number from host byte-ordering to network byte-ordering
ntohl() / ntohs()
- Network to host long/short - Converts a 32-bit (long) or 16-bit (short) number from network byte-ordering to host byte-ordering
Endianness
- Refers to how the bytes which are part of collections of bytes (for big numbers like addresses) are laid out in memory
Client-Server Paradigm
- Server is software actively listening for a connection - Client is software that connects to the server - Need both to form an active connection
Berkeley Sockets
- Treats network I/O as if they were files (Influenced by UNIX) - API for TCP/IP - Aims to be network-agnostic
Berkeley sockets functions used on both client & server (in order you might use them)
- socket() => Create a socket, rather than open()ing a connection, this forms one end of the connection - read() - write() - close()
Little Endian
Mirror of Big Endian, least significant byte first (Looks backwards in memory but can make chip design easier)
Big Endian
Most significant bytes first