Welcome, today we will look at Berkeley Socket of Programming API. An API allows application programs to access certain resources through a predefined interface. Well, most popular APIs that provide access to network resources is Berkeley Socket Interface. Which is popular in many UNIX machines. By abstraction, the socket API hides the details of underlying communication technologies as much as possible. It just allows programmers to write application programs easily without worrying about underlying network details. Applications create sockets in a write or read to or from sockets. Note there are other popular socket interface. For example, Winsock in Microsoft Windows environment. The figure shows how two applications talk to each other across the communication networks through the socket interface. In a typical scenario, one application operates as a server, while the other as a client. As you tell from the figure, sockets are kernel space resources while their interface accessible by descriptor are in user space. Please recall that host computers run two transport protocols on top of IP to enable process-to-process communications. UDP enables best-effort connectionless transfer to individual block of information, while TCP enables reliable transfer of a stream of bytes. That's two modes of services available through the circuit interface, connection-oriented and connection-less. In connection-oriented mode, an application must first establish a connection. Then, data will be delivered through the connection to the destination in sequenced transfer of byte strings. Note, no boundaries preserve the transfer, so a programmer has to pay attention to it when developing client server applications. The connectionless mode doesn't have connection setup overhead. It provides best effort of service. The mode does immediate transfer of one block of information where message boundaries are preserved in transfer. But please note the message may be possibly out of order when received. Even worse, there is possible loss of a message. So a programmer needs to take care of retransmission and ordering if those features are really important. There are some key differences in programming server and this client. Server specifies a well-known and unique port # when creating a socket. It awaits passively for client requests. Client is assigned an ephemeral port number that is valid only during the connection. Client initiates communications with the server. It needs to know server IP address, which can consult DNS service for the translation between IP name and IP address. The figure shows a typical diagram of the sequence of sockets calls for connection-oriented mode. The server begins by carrying out a passive open as follows. The socket called creates a socket specifying TCP stream pipe. The type is sock online stream. It returns a socket descriptor and the integer number if the call is successful. The bind call binds the well-known port number, as well as the local IP address of the server to the socket descriptor. Note that it could wipe out IP address for multiple net interface. The listen call turns the socket into a listening socket that can accept incoming connections from clients. There is a parameter in the call that specifies the maximum number of requests that may be queued while waiting for server to accept them. The server calls accept to accept incoming requests. Accept blocks if queue is empty. The client side active open, it creates a circuit to connect to the server. Client calls connect and attempts to establish a connection to their local socket, which is specified to the specified remote address upon number. This indeed initiates a three-way handshaking process for connection build up according to the TCP protocol. The three-way handshaking process will be covered in a later course. When the TCP connection is established, acceptor function under the server wakes up and returns a new socket descriptor for the given connection. That is specified by the source app IP address, source port number, destination IP address, and destination port number. Note that a client on a server will use a new socket for track data transfer, while the original socket at the server side continues to listen for new connection requests from a client. For data transfer client or server pulls write function to transmit the data into the connected socket. Write function call if successful returns a number of bytes transferred, it is a blocking call until all data are transferred. Client on this server calls real function to receive data from the connected socket. The call returns number of bytes [INAUDIBLE]. It is a broken call if no data arrives. Client or server cause close function to close out the TCP connection. As a quick summary, socket API hides details of underlying protocols and mechanisms. Next class, we will learn connectionless mode and program examples.