[go: up one dir, main page]

0% found this document useful (0 votes)
31 views26 pages

IT692 Lectures

The document discusses socket programming and the client-server model. It explains concepts like TCP, UDP, ports, sockets and related system calls. Example applications of TCP and UDP are also covered.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views26 pages

IT692 Lectures

The document discusses socket programming and the client-server model. It explains concepts like TCP, UDP, ports, sockets and related system calls. Example applications of TCP and UDP are also covered.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

IT 692: Computer Networking

Lecture 2: Socket Programming

Dr. Amitava Nag


Associate Professor and Head
Dept. of IT
Academy of Technology
amitava.nag@aot.edu.in
End System: Computer on the
Internet

Internet

Also known as a “host”…


Client/sever model
• Client asks (request) – server provides (response)
• Typically: single server - multiple clients

• The server does not need to know anything about the client
– even that it exists
• The client should always know something about the server
– at least where(address) it is located

Note: clients and servers are processes running on hosts


(can be the same or different hosts).
Sockets as means for inter-process
communication (IPC)

application layer application layer

Client Process Internet Server Process


Socket Socket
transport layer (TCP/UDP) transport layer (TCP/UDP)
OS network Internet OS network
network layer (IP) network layer (IP)
stack stack
link layer (e.g. ethernet) link layer (e.g. ethernet)
physical layer
Internet physical layer

The interface that the OS provides to its networking subsystem


Server and Client
Server and Client exchange messages over the network through a common
Socket API

Clients
user
Server
ports space

TCP/UDP Socket API TCP/UDP kernel


space

IP IP

Ethernet Adapter Ethernet Adapter hardware


5
User Datagram Protocol(UDP):
An Analogy
UDP Postal Mail
Postal Mail
• Single socket to receive messages • Single
• •
• Singlemailbox
mailboxto receive letters
to receive
No guarantee of delivery Unreliable 
• Not necessarily in-order delivery • messages
Not necessarily in-order delivery
• Datagram – independent packets •• Unreliable
Letters 
sent independently
• Must address each packet • Must address each reply
• Not necessarily in-order
delivery
• Each letter is independent
• Must address each reply

Example UDP applications


Multimedia, voice over IP

6
Transmission Control Protocol
(TCP): An Analogy
TCP Telephone Call
• Guaranteed delivery
• Reliable – guarantee
• In-order delivery
delivery • Connection-oriented
• Byte stream – in-order • Setup connection followed by
delivery conversation
• Connection-oriented –
single socket per
connection
• Setup connection followed
by data transfer
Example TCP applications
Web, Email, Telnet

7
Network Addressing Analogy
Telephone Call Network Programming

Professors at AOT Applications/Servers


033-2708-8000 033-2708-8000
ext.123 ext.654 Web Mail
Port 80 Port 25

Extension Port No.

Telephone No IP Address
Central Number Network No.
Exchange Host Number
Area Code

Students Clients
Internet Connections (TCP/IP)
• Address the machine on the network
– By IP address
• Address the process
– By the “port”-number
• The pair of IP-address + port – makes up a “socket-address”

Client socket address Server socket address


128.2.194.242:3479 208.216.181.15:80

Server
Client
Connection socket pair (port 80)
(128.2.194.242:3479, 208.216.181.15:80)

Client host address Server host address


128.2.194.242 208.216.181.15

Note: 3479 is an Note: 80 is a well-known port


ephemeral port allocated associated with Web servers
by the kernel
Concept of Port Numbers
–Port numbers are used to identify
“entities” on a host NTP Web
–Port numbers can be daemon server
• Well-known (port 0-1023) port 123 port 80
• Dynamic or private (port 1024-65535)
–Servers/daemons usually use well- TCP/UDP
known ports
• Any client can identify the server/service
• HTTP = 80, FTP = 21, Telnet = 23, ... IP
• /etc/service defines well-known ports
–Clients usually use dynamic ports Ethernet Adapter
• Assigned by the kernel at run time

10
Using Ports to Identify Services
Server host 128.2.194.242

Client host Service request for Web server


128.2.194.242:80 (port 80)
(i.e., the Web server)
Client Kernel
Echo server
(port 7)

Service request for Web server


128.2.194.242:7 (port 80)
(i.e., the echo server)
Client Kernel
Echo server
(port 7)
Socket Programming - An analogy
Socket: telephone
Bind: assign telephone number to a telephone
Listen: turn on the ringer so that you can hear the
phone call
Connect: dial a phone number
Accept: answer the phone
Send/Recv: talking
Close: ???
Sockets
• What is a socket?
– To the kernel, a socket is an endpoint of communication.
– To an application, a socket is a file descriptor that lets the application
read/write from/to the network.

• Clients and servers communicate with each by reading from


and writing to socket descriptors.

• The main distinction between regular file I/O and socket I/O is
how the application “opens” the socket descriptors.
Types of socket
• Two most common types:
– SOCK_STREAM: Stream sockets, which provide reliable, two-way,
connection-oriented communication streams. <Uses TCP>
– SOCK_DGRAM: Datagram sockets, which provide connectionless,
unreliable service, used for packet-by-packet
transfer of information. <Uses UDP>
• Other types like SOCK_RAW also exist.
– Beyond the scope of the present discussion.
Systems calls for using sockets

• socket()
• bind()
• connect()
• listen()
• accept()
• send() & recv()
• sendto() & recvfrom()
• close() & shutdown()
• getpeername()
• gethostname()
• gethostbyname()
connection-oriented : TCP Client-Server
Interaction
TCP Server
socket()

bind()
TCP Client
listen()
socket()

connection establishment accept()


connect()

data request recv()


send()

data reply send()


recv()

end-of-file notification read()


close()
close()
from UNIX Network Programming Volume 1, figure 4.1
Socket functional calls
• socket (): Create a socket
• bind(): bind a socket to a local IP address and port #
• listen(): passively waiting for connections
• connect(): initiating connection to another socket
• accept(): accept a new connection
• write()send(): write data to a socket
• read()/recv(): read data from a socket
• sendto(): send a datagram to another UDP socket
• recvfrom(): read a datagram from a UDP socket
• close(): close a socket (tear down the connection)
Socket-programming using TCP

socket( )
bind( ) server
socket( ) listen( )
client bind( )
connect( ) TCP conn. request
accept( )
send( ) TCP ACK
recv( )

recv( )
send( )
close( ) close( )

controlled by
application process
process
developer socket
socket
controlled by TCP with
TCP with
operating buffers, internet
system buffers,
variables
variables
connectionless: UDP Client-Server
Interaction
UDP Server
socket()

bind()
UDP Client
recvfrom()
socket()
blocks until datagram
sendto() received from a client
data request

data reply sendto()


recvfrom()

close()

from UNIX Network Programming Volume 1, figure 8.1


Byte Ordering Functions
• Converts between host byte order and network byte order
– ‘h’ = host byte order
– ‘n’ = network byte order
– ‘l’ = long (4 bytes), converts IP addresses
– ‘s’ = short (2 bytes), converts port numbers

#include <netinet/in.h>

Network Byte Ordering


– Network is big-endian, host may be big- or little-endian
– Functions work on 16-bit (short) and 32-bit (long) values
– htons() / htonl() : convert host byte order to network byte order
– ntohs() / ntohl(): convert network byte order to host byte order
– Use these to convert network addresses, ports,

20
The struct sockaddr
#include <netinet/in.h>
• The generic: • The Internet-specific:
struct sockaddr_in {
struct sockaddr {
u_short sa_family; short
char sa_data[14]; sin_family;
}; u_short sin_port;
struct in_addr
sin_addr;
char
sin_zero[8];
};
/* Internet address structure */
struct in_addr {
u_long s_addr;
};
– sin_family = AF_INET
21
– sin_port: port # (0-65535)
Structure Casts
– You will see a lot of ‘structure casts’

struct sockaddr_in serv_addr, cli_addr;


/* fill in serveraddr with an address */

/*Server Side */
bind(sockfd,(struct sockaddr *)&serv_addr,sizeof(serv_addr));
accept(sockfd, (struct sockaddr *) &cli_addr, &clilen) ;


struct sockaddr_in serv_addr
/*Client Side Connect takes (struct sockaddr *) as its second argument */
connect(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
Socket primitives
• SOCKET: int socket(int domain, int type,
int protocol);
– domain := AF_INET (IPv4 protocol)
– type := (SOCK_DGRAM or SOCK_STREAM )
– protocol := 0 (IPPROTO_UDP or IPPROTO_TCP)
– returned: socket descriptor (sockfd), -1 is an error

• BIND: int bind(int sockfd, struct sockaddr *my_addr, int


addrlen);
– sockfd - socket descriptor (returned from socket())
– my_addr: socket address, struct sockaddr_in is used
– addrlen := sizeof(struct sockaddr)
struct sockaddr_in {
unsigned short sin_family; /* address family (always AF_INET) */
unsigned short sin_port; /* port num in network byte order */
struct in_addr sin_addr; /* IP addr in network byte order */
unsigned char sin_zero[8]; /* pad to sizeof(struct sockaddr) */
};
• LISTEN: int listen(int sockfd, int backlog);
– backlog: how many connections we want to queue
• ACCEPT: int accept(int sockfd, void *addr, int *addrlen);
– addr: here the socket-address of the caller will be written
– returned: a new socket descriptor (for the temporal socket)
• CONNECT: int connect(int sockfd, struct sockaddr *serv_addr, int
addrlen); //used by TCP client
– parameters are same as for bind()
• SEND: int send(int sockfd, const void *msg, int len, int flags);
– msg: message you want to send
– len: length of the message
– flags := 0
– returned: the number of bytes actually sent
• RECEIVE: int recv(int sockfd, void *buf, int len, unsigned int flags);
– buf: buffer to receive the message
– len: length of the buffer (“don’t give me more!”)
– flags := 0
– returned: the number of bytes received
• SEND (DGRAM-style): int sendto(int sockfd, const void *msg, int len, int
flags, const struct sockaddr *to, int tolen);
– msg: message you want to send
– len: length of the message
– flags := 0
– to: socket address of the remote process
– tolen: = sizeof(struct sockaddr)
– returned: the number of bytes actually sent

• RECEIVE (DGRAM-style): int recvfrom(int sockfd, void *buf, int len,


unsigned int flags, struct sockaddr *from, int *fromlen);
– buf: buffer to receive the message
– len: length of the buffer (“don’t give me more!”)
– from: socket address of the process that sent the data
– fromlen:= sizeof(struct sockaddr)
– flags := 0
– returned: the number of bytes received

• CLOSE: close (socketfd);


References
1. W. Richard Stevens, UNIX Network Programming
2. http://www.facweb.iitkgp.ernet.in/~isg/NWLAB/SLIDES/sockets.pdf
3. http://beej.us/guide/bgnet/output/print/bgnet_A4_2.pdf

You might also like