Application Layer (3/3)
Socket Programming
- Now that we've looked at a number of important network applications, let's explore how network application programs are actually created.
- When a client program and server program are executed, a client process and a server process are created, and these processes communicate with each other by reading from, and writing to, sockets.
- There are two types of network applications.
- One type is an implementation whose operation is specified in a protocol standard, such as an RFC or some other standards document.
- One example would be client program implemented on client side of the HTTP protocol and server program implemented also by HTTP server protocol.
- Both developers carefully foolow rules of RFC, then the two programs will be able to interoperate.
- The other type of network application is a proprietary network application, a single developer creating both the client and server program.
- Developer could have complete control over what goes in the code, but other developers will not be able to develop code that interoperates with the application.
Socket Programming with UDP
- Application developer has control of everything on the application-layer side of the socket; however, it has little control of the transport-layer side.
- The sending process attaches to the packet a destination address, which consists of the destination host's IP address and the destination socket's port number.
- Also, the sender's source address (IP address and port number) are also attached to the packet, but this process is done by underlying operating system.
- In UDP, there is no connection between client & server; meaning there is no handshaking before sending data.
- Therefore in UDP, transmitted data may be lost or received out-of-order.
- In a application viewpoint, UDP provides unreliable transfer of groups of bytes(datagrams) between client and server.
- AF_INET indicates that the underlying network is using IPv4.
- SOCK_DGRAM indicates that the socket is a UDP socket rather than a TCP socket.
- When creating a socket, we do not specify the port number of client socket, instead let the operating system do this for us.
- As mentioned above, the source address is also attached to the packet, although this is done automatically rather than explicitly by the code.
- After sending the packet, the client waits to receive data from the server.
- The input 2048 in recvfrom indicates the buffer size.
- With decode(), we bytes aer converted back to string.
- In the server, the code written by the application developer is explicitly assigning a port number to the socket.
- In this manner, when anyone sends a packet to port 120000 at the IP address of the server, that packet will be directed to this socekt.
- bind() means assigning the port number to the server's socket.
- The varialbe client Address contains both the client's IP address and the client's port number.
Socket Programming with TCP
- Unlike UDP, TCP is a connection-oriented protocol.
- This means that before the client and server can start to send data to each other, they first need to handshake and establish a TCP connection.
- The difference with UDP is that when one side wants to send data to the other side, it drops the data into the TCP connection via its socket.
- The client has the job of initiating contact with the server.
- With the server process running, the client process can initiate a TCP connection to the server.
- The client initiates a three-way handshake and establishes a TCP connection with the server.
- Do not confuse the welcoming socket and each newly created server-side connection socket tha si subsequently created for communicating with each client.
- The second parameter in socket(), which is SOCK_STREAM indicates that the socket is a TCP socket rather than an UDP socket.
- The parameter of connect() method is the address of the server side of the connection.
- When using send(), the client no longer has to specify the server's address since in TCP, the two sockets are connected with each other.
- With TCP, serverPort will be our welcoming socket.
- When a client knocks on this door, the program invokes the accept() method for server Socket, which creates a new socket in the server called connectionSocket.
- After sending the modified sentence we close connection socket.