0% found this document useful (0 votes)
32 views

SocketProgramming 6

The document discusses the client-server paradigm and socket programming. It provides definitions for key concepts like clients initiating contact with servers, sockets providing an abstraction for network communication, and the socket API defining functions for creating, closing, reading from and writing to sockets. The summary covers: 1) Clients initiate contact with servers by requesting a service, while servers provide the requested service. 2) Sockets provide an abstraction for network communication and are used by processes to communicate by sending and receiving data. 3) The socket API defines functions for creating sockets, binding sockets to addresses, listening for connections, accepting connections, and connecting sockets between clients and servers.

Uploaded by

Post Techinsert
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

SocketProgramming 6

The document discusses the client-server paradigm and socket programming. It provides definitions for key concepts like clients initiating contact with servers, sockets providing an abstraction for network communication, and the socket API defining functions for creating, closing, reading from and writing to sockets. The summary covers: 1) Clients initiate contact with servers by requesting a service, while servers provide the requested service. 2) Sockets provide an abstraction for network communication and are used by processes to communicate by sending and receiving data. 3) The socket API defines functions for creating sockets, binding sockets to addresses, listening for connections, accepting connections, and connecting sockets between clients and servers.

Uploaded by

Post Techinsert
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Client-

Client-server paradigm
Client:
application
 initiates contact with server transport
network
data link
(“speaks first”) physical

 typically requests service request


Socket Programming from server,
 for Web, client is
implemented in browser; for
e-mail, in mail reader
reply
Server:
Srinidhi Varadarajan  provides requested service application
transport
to client network
data link
physical
 e.g., Web server sends
requested Web page, mail
server delivers e-mail

Application Layer Programming


Socket Interface. What is it?
API: application programming interface
 Gives a file system like abstraction to the
 defines interface between application and capabilities of the network.
transport layer
 Each transport protocol offers a set of
 sockets: Internet API services. The socket API provides the
– two processes communicate by sending data abstraction to access these services
into socket, reading data out of socket
 The API defines function calls to create,
close, read and write to/from a socket.

Socket Abstraction What do you need for socket communication ?


 The socket is the basic abstraction for network
 Basically 4 parameters
communication in the socket API
– Defines an endpoint of communication for a process – Source Identifier (IP address)
– Operating system maintains information about the – Source Port
socket and its connection – Destination Identifier
– Application references the socket for sends, receives,
etc. – Destination Port

Network  In the socket API, this information is


Process Process
A B communicated by binding the socket.

Ports (Sockets)

Application Layer 1
Creating a socket Binding a socket
int socket(int domain, int type, int protocol) int bind (int socket, struct sockaddr *address, int addr_len)

Protocol Family: Usually  This call is executed by:


PF_INET or UNSPEC
PF_UNIX – Server in TCP and UDP

Communication
semantics:  It binds the socket to the specified address. The
SOCK_STREAM or address parameter specifies the local component
SOCK_DGRAM
of the address, e.g. IP address and UDP/TCP port

The call returns a integer identifier called a


handle

Socket Descriptors
Socket Descriptors Socket Socket Data
Descriptor Structure
 Operating system maintains a set of Table proto family:
socket descriptors for each process 0: PF_INET
Address Data
– Note that socket descriptors are shared 1: service: Structure
by threads 2: SOCK_STREAM
address family:
.. local address:
 Three data structures . AF_INET

– Socket descriptor table remote address: host IP:


128.173.88.85
– Socket data structure ..
. port:
– Address data structure 80

TCP Server Side: Listen TCP Server Side: Passive Open


int listen (int socket, int backlog) int accept (int socket, struct sockaddr *address, int *addr_len)

 This call is executed by the server.


 This server side call specifies the number
of pending connections on the given
socket.  The call does not return until a remote
client has established a connection.
 When the server is processing a
connection, “backlog” number of  When it completes, it returns a new socket
connections may be pending in a queue. handle corresponding to the just-
established connection

Application Layer 2
TCP Client Side: Active Open Sockets: Summary
int connect (int socket, struct sockaddr *address, int *addr_len)  Client:
int socket(int domain, int type, int protocol)
 This call is executed by the client. *address
int connect (int socket, struct sockaddr *address, int addr_len)
contains the remote address.

 The call attempts to connect the socket to a  Server:


server. It does not return until a connection has int socket(int domain, int type, int protocol)
been established. int bind (int socket, struct sockaddr *address, int addr_len)
int listen (int socket, int backlog)
 When the call completes, the socket “socket” is int accept (int socket, struct sockaddr *address, int *addr_len)
connected and ready for communication.

Summary of Basic Socket Calls


Message Passing CLIENT Connect SERVER
(3-way handshake)
 int send (int socket, char *message, int msg_len, int connect() accept()
flags) (TCP)

new connection
 int sendto (int socket, void *msg, int len, int
flags, struct sockaddr * to, Data
int tolen ); (UDP) write() read()
 int write(int socket, void *msg, int len); /* TCP */

read() Data write()


 int recv (int socket, char *buffer, int buf_len, int
flags) (TCP)
 int recvfrom(int socket, void *msg, int len, int
flags, struct sockaddr *from, int close() close()
*fromlen); (UDP)
 int read(int socket, void *msg, int len); (TCP)

Network Byte Order Some Other “Utility” Functions


 gethostname() -- get name of local host
 Network byte order is most-significant
 getpeername() -- get address of remote
byte first
host
 Byte ordering at a host may differ
 getsockname() -- get local address of
 Utility functions socket
– htons(): Host-to-network byte order for a short
word (2 bytes)  getXbyY() -- get protocol, host, or service
– htonl(): Host-to-network byte order for a long number using known number, address, or
word (4 bytes) port, respectively
– ntohs(): Network-to-host byte order for a short  getsockopt() -- get current socket options
word  setsockopt() -- set socket options
– ntohl(): Network-to-host byte order for a long
word  ioctl() -- retrieve or set socket information

Application Layer 3
Address Data Structures
Some Other “Utility” Functions
struct sockaddr {
u_short sa_family; // type of address
 inet_addr() -- convert “dotted” char sa_data[14]; // value of address
}
character string form of IP address to
internal binary form  sockaddr is a generic address structure

 inet_ntoa() -- convert internal binary struct sockaddr_in {


form of IP address to “dotted” u_short sa_family; // type of address (AF_INET)
u_short sa_port; // protocol port number
character string form struct in_addr sin_addr; // IP address
char sin_zero[8]; // unused (set to zero)
}
 sockaddr_in is specific instance for the Internet address
family

Application Layer 4

You might also like