Monday 29 July 2013

on Leave a Comment

4) The Socket API

The Socket API is the most widely used interface to write applications on the internet.

Network-Application Interface:
• Defines how apps use the network
– Lets apps talk to each other via hosts; hides the details of the network

2

Allows apps use this interface (Dark pink continuous line) to connect or talk to other apps over the internet without revealing the network components.
1

Motivating Application:Simple client-server setup
3
• Simple client-server setup
– Client app sends a request to server app
– Server app returns a (longer) reply
• This is the basis for many apps! (Client sends, server replies back)
– File transfer: send name, get file
– Web browsing: send URL, get page
– Echo: send message, get it back
• Let’s see how to write this app …


Socket API:
• Simple abstraction to use the network
– The network service API used to write all Internet applications
– Part of all major OSes and languages; originally Berkeley (Unix) ~1983
• Supports two kinds of network services
– Streams: reliably send a stream of bytes »
– Datagrams: unreliably send separate messages. (Ignore for now.)
• Sockets let apps attach to the local network at different ports
4
5
BIND LISTEN ACCEPT are used for incoming side to get ready and get incoming calls.

Using Sockets:
Time sequence diagram:
6

Numbering them in the occurrence of their sequence.

7

* => indicates the blocking calls where the program makes the calls and the program is in halted by the operating system until something happens on the network side.
We accept before we connect because accept is a blocking call as it says wait or you are allowed to connect with me.
Receive is another blocking call , which is on receiving side.

Client Program (outline):

socket()                          // make socket
getaddrinfo()                 // server and port name
                                       // www.example.com:80
(network calls/socket calls don’t take high                                                   level addresses , they take IP address )(translating between high                                                 level names and network addresses)
connect()                      // connect to server [block]

send()                           // send request
recv()                           // await reply [block]
…                                  // do something with data!
close()                         // done, disconnect


Server Program (outline):
socket()                      // make socket
getaddrinfo()             // for port on this host (same reason)
bind()                         // associate port with socket
listen()                       // prepare to accept connections
accept()                     // wait for a connection [block]

recv()                        // wait for request

send()                       // send the reply
(this program portion will be in a loop because client program might exit to make single connection and get the information back. Server will be a long running operation , it will be running , it will wait for client to connect and it will send the request to wait for the next client to send his request. )
close()                      // eventually disconnect



There are many online guides for learning sockets, the most popular of which is perhaps “Beej’s Guide to Network Programming”

Precious: 1-3 Network Components

0 comments:

Post a Comment

AKB. Powered by Blogger.