Ppt-4 Socket Programming
Ppt-4 Socket Programming
Programming
Protocol Families - TCP/IP
◼ Several protocols for different problems
◼ Protocol suites or protocol families: TCP/IP
◼ TCP/IP provides end-to-end connectivity specifying how data
should be
◼ Formatted,
◼ Addressed,
◼ Transmitted,
◼ Routed, and
◼ Received at the destination
◼ Can be used in the internet and in stand-alone private networks
◼ It is organized into layers
2
TCP/IP
*
FTP, SMTP, …
Transport Layer
TCP or UDP
Network Layer
IP
Communication
Channels
3
TCP vs UDP
◼ Both useport numbers
❑ Application-specific construct serving as a communication endpoint
❑ 16-bit unsigned integer, thus ranging from 0 to 65535 to provide end-to-
end transport
◼ UDP: user datagram protocol
❑ No acknowledgements and No retransmissions
❑ Out of order, duplicates possible
❑ Connectionless, i.e., App indicates destination for each packet
◼ TCP: transmission control protocol
❑ Reliable byte-stream channel (in order, all arrive, no duplicates)
◼ Similar to file I/O
❑ Flow control
❑ Connection-oriented
❑ Bidirectional
4
TCP vs UDP
◼ TCP is used for services with a large data capacity, and a persistent
connection
◼ UDP is more commonly used for quick lookups, and single use query-
reply actions.
◼ Some common examples of TCP and UDP with their default ports:
5
Berkley Sockets
◼ Universally known as Sockets
◼ It is an abstraction through which an
application may send and receive data
◼ Provide generic access to interprocess
communication services
◼ e.g. IPX/SPX, Appletalk, TCP/IP
Socket Socket
TCP TCP
IP Channel IP Channel IP
6
Sockets
◼ Uniquely identified by
❑ An internet address
❑ An end-to-end protocol (e.G. TCP or UDP)
❑ A port number
7
Sockets
Descriptor
references
Applications
TCP UDP
IP
8
Socket Programming
9
Client-Server communication
◼ Server
❑ Passively waits for and responds to clients
❑ Passive socket
◼ Client
❑ Initiates the communication
❑ Active socket
10
Client - Server Communication - Unix
Stream Datagram
Server (e.g. TCP) Client Server (e.g. UDP) Client
socket() socket() socket() socket()
listen()
synchronization
point
accept() connect()
11
Sockets - Procedures
12
Client - Server Communication - Unix
Stream Datagram
Server (e.g. TCP) Client Server (e.g. UDP) Client
socket() socket() socket() socket()
listen()
synchronization
point
accept() connect()
13
Socket creation in C: socket()
◼ int sockid = socket(family, type, protocol);
❑ sockid: socket descriptor, an integer (like a file-handle)
❑ family: integer, communication domain, e.g.,
◼ PF_INET, IPv4 protocols, Internet addresses (typically used)
◼ PF_UNIX, Local communication, File addresses
❑ type: communication type
◼ SOCK_STREAM - reliable, 2-way, connection-based service
◼ SOCK_DGRAM - unreliable, connectionless, messages of maximum length
❑ protocol: specifies protocol
◼ IPPROTO_TCP IPPROTO_UDP
◼ usually set to 0 (i.e., use default protocol)
❑ upon failure returns -1
❑ NOTE: socket call does not specify where data will be coming from, nor
where it will be going to – it just creates the interface!
14
Client - Server Communication - Unix
Stream Datagram
Server (e.g. TCP) Client Server (e.g. UDP) Client
socket() socket() socket() socket()
listen()
synchronization
point
accept() connect()
15
Socket close in C: close()
◼ When finished using a socket, the socket should be closed
◼ status = close(sockid);
❑ sockid: the file descriptor (socket being closed)
❑ status: 0 if successful, -1 if error
◼ Closing a socket
❑ closes a connection (for stream socket)
❑ frees up the port used by the socket
16
Specifying Addresses
◼ Socket API defines a generic data type for addresses:
struct sockaddr {
unsigned short sa_family; /* Address family (e.g. AF_INET) */
char sa_data[14]; /* Family-specific address information */
}
17
Client - Server Communication - Unix
Stream Datagram
Server (e.g. TCP) Client Server (e.g. UDP) Client
socket() socket() socket() socket()
listen()
synchronization
point
accept() connect()
18
Assign address to socket: bind()
◼ associates and reserves aport for use by the socket
19
bind()- Example with TCP
int sockid;
struct sockaddr_in addrport;
sockid = socket(PF_INET, SOCK_STREAM, 0);
addrport.sin_family = AF_INET;
addrport.sin_port = htons(5100);
addrport.sin_addr.s_addr = htonl(INADDR_ANY);
if(bind(sockid, (struct sockaddr *) &addrport, sizeof(addrport))!= -1) {
…}
20
Skipping the bind()
◼ Bind can be skipped for both types of sockets
◼ Datagram socket:
❑ If only sending, no needto bind.
❑ The osfinds aport each time the socket sends a packet
❑ If receiving, need to bind
◼ Stream socket:
❑ Destination determined during connection setup
❑ Don’t need to know port sending from (during connection setup,receiving end is
informed of port)
21
Client - Server Communication - Unix
Stream Datagram
Server (e.g. TCP) Client Server (e.g. UDP) Client
socket() socket() socket() socket()
listen()
synchronization
point
accept() connect()
22
Assign address to socket: bind()
◼ Instructs TCP protocol implementation to listen for connections
23
Client - Server Communication - Unix
Stream Datagram
Server (e.g. TCP) Client Server (e.g. UDP) Client
socket() socket() socket() socket()
listen()
synchronization
point
accept() connect()
24
Establish Connection: connect()
◼ The client establishes aconnection with the server by calling connect()
❑ connect() is blocking
25
Incoming Connection: accept()
◼ The server gets asocket for an incoming client connection by calling
accept()
◼ int s = accept(sockid, &clientAddr, &addrLen);
❑ s:integer, the new socket (used for data-transfer)
❑ sockid: integer, the orig. socket (being listened on)
❑ clientAddr: struct sockaddr, address of the active participant
◼ filled in upon return
❑ addrLen: sizeof(clientAddr): value/result parameter
◼ must be set appropriately before call
◼ adjusted upon return
◼ accept()
❑ is blocking: waits for connection before returning
❑ dequeues the next connection on the queue for socket (sockid)
26
Client - Server Communication - Unix
Stream Datagram
Server (e.g. TCP) Client Server (e.g. UDP) Client
socket() socket() socket() socket()
listen()
synchronization
point
accept() connect()
27
Exchanging data with stream socket
◼ int count = send(sockid, msg, msgLen, flags);
❑ msg: const void[], message to betransmitted
❑ msgLen: integer, length of message (in bytes) to transmit
❑ flags: integer, special options, usually just 0
❑ count: # bytes transmitted (-1 if error)
30