Introduction to Sockets
Objectives
We would like to :
- Gain a basic understanding of the socket IPC mechanism
Notes
- This is chapter 56
- There are about 6 chapters in this section.
- And you might consider it a mini course in practical datacomm.
- Sockets allow two way communications between processes
- On the same machine or across the network.
- Unix domain sockets are on a single computer
- Internet domain sockets are across a network.
- Both have the same interface.
- And guess what, both will give us a file descriptor.
- We will soon have a set of machines on which we can build internet socket software.
- Unlike pipes, there is no need for socket applications to come from the same ancestor.
- Sockets employ communications domains
- AF_UNIX
- Communications are provided by the kernel.
- Address is in the form of a path name.
- sockaddr_un is the address structure
- AF_INET, AF_INET6
- via IP4 or IP6
- Address is in the form of an ip (or ipv6) address
- sockaddr_in or sockaddr_in6
- Sockets can have three properties:
- Reliable: we are guaranteed that the data will arrive intact, exactly as transmitted or we will receive an error. (all data correct in order)
- Boundaries preserved: there is a way to distinguish between message, as opposed to just a string of bits.
- Connection oriented: Operates in pairs (sender - receiver) and expects the pair to communicate until the session/connection ends.
- Think phone call not chat group.
- There are two types of sockets
- Stream : Connection oriented, reliable, but no message boundaries, TCP
- Datagram: Connectionless, unreliable, but message boundaries., UDP
- In general, it takes the following calls to make a socket program work
-
socket
- allocate the basic socket infrastructure: client and server
-
bind
- associate the socket with an address: server
-
listen
- allow for incoming connections: server
-
accept
- select one connection and begin "servicing" it: server
-
connect
- connect to a server : client
-
read/write
- both
-
send, recv, sendto, recvfrom
- both
- Look at the pictures on page 1156 and 1157