When we desire a communication between two applications possibly running on different machines, we need sockets. This presentation aims to provide knowledge of basic socket programming to undergraduate students. Basically, this presentation gives the importance of socket in the area of networking and Unix Programming. The presentation of Topic (Sockets) has designed according to the Network Programming Subject, B.Tech, 6th Semester syllabus of Punjab Technical University Kapurthala, Punjab.
2. Definition of Socket
•when we desire a communication between two
applications possibly running on different machines,
we need sockets.
•Socket needs to return the file descriptor of following
values like domain name, type of socket and name of
the protocol.
•Socket commonly executes the user processes at
Transport Layer.
2
3. Socket and Process Communication
The interface that the OS provides to its networking subsystem
application layer
transport layer (TCP/UDP)
network layer (IP)
link layer (e.g. ethernet)
application layer
transport layer (TCP/UDP)
network layer (IP)
link layer (e.g. ethernet)
OS network
stack
User Process User Process
Socket
OS network
stack
Socket
Internet
Internet
Internet
3
4. Socket (Cont..)
4
•when we desire a communication between two applications possibly running on different machines, we need
sockets.
•Socket needs to return the file descriptor of following values like domain name, type of socket and name of
the protocol.
•Socket commonly executes the user processes at Transport Layer.
6. Network Address
For Two processes to communicate, they need to know each others Network
address.
Network address consist of two parts:
<Internet (IP) address, Port number>
Port no. 1024-49151 are registered port
Port 4915-65536 can not be used. They are called ephemeral ports, which
are assigned automatically by TCP or UDP for Client
6
7. Socket, Port and Interface
For Two processes to communicate, they need to know each others Network
address.
Network address consist of two parts:
<Internet (IP) address, Port number>
Well-known vs. ephemeral ports
Between 0 and 1023 (requires root to use)
Port no. 1024-49151 are registered port
Port 4915-65536 can not be used. They are called ephemeral ports, which
are assigned automatically by TCP or UDP for Client
7
8. Using Ports to Identify Services
8
Web server
(port 80)
Client host
Server host 1.1.1.2
Echo server
(port 7)
Service request for
1.1.1.1:80
(i.e., the Web server)
Web server
(port 80)
Echo server
(port 7)
Service request for
1.1.1.2:7
(i.e., the echo server)
OS
OS
Client
Client
9. Two Types of Application Processes
Communication
• Datagram Socket (UDP)
– Collection of messages
– Best effort
– Connectionless
• Stream Socket (TCP)
– Stream of bytes
– Reliable
– Connection-oriented
9
10. User Datagram Protocol (UDP):
Datagram Socket
Postal Mail
• Single mailbox to receive
messages
• Unreliable
• Not necessarily in-order
delivery
• Each letter is independent
• Must address each reply
Example UDP applications
Multimedia, voice over IP (Skype)
UDP
• Single socket to receive messages
• No guarantee of delivery
• Not necessarily in-order delivery
• Datagram – independent packets
• Must address each packet
Postal Mail
• Single mailbox to receive letters
• Unreliable
• Not necessarily in-order delivery
• Letters sent independently
• Must address each mail
10
11. Transmission Control Protocol (TCP):
Stream Socket
Postal Mail
• Single mailbox to receive
messages
• Unreliable
• Not necessarily in-order
delivery
• Each letter is independent
• Must address each reply
Example TCP applications
Web, Email, Telnet
TCP
• Reliable – guarantee delivery
• Byte stream – in-order delivery
• Connection-oriented – single
socket per connection
• Setup connection followed by
data transfer
Telephone Call
• Guaranteed delivery
• In-order delivery
• Connection-oriented
• Setup connection followed by
conversation
11
12. Socket Identification
• Communication Protocol
– TCP (Stream Socket): streaming, reliable
– UDP (Datagram Socket): packets, best effort
• Receiving host
– Destination address that uniquely identifies the host
– An IP address is a 32-bit quantity
• Receiving socket
– Host may be running many different processes
– Destination port that uniquely identifies the socket
– A port number is a 16-bit quantity
12
14. Types of Socket
1. Stream sockets allow processes to communicate using TCP. A
stream socket provides bidirectional, reliable, sequenced, and
unduplicated flow of data with no record boundaries. Once the
connection has been established, data can be read from and
written to these sockets as a byte stream. The socket
type is SOCK_STREAM.
2. Datagram sockets allow processes to use UDP to
communicate. A datagram socket supports bidirectional flow of
messages. A process on a datagram socket may receive
messages in a different order from the sending sequence and
may receive duplicate messages. Record boundaries in the data
are preserved. The socket type is SOCK_DGRAM.
14
15. Types of Socket (1)
3. Raw sockets provide access to ICMP. These sockets are
normally datagram oriented, although their exact
characteristics are dependent on the interface provided by the
protocol. Raw sockets are not for most applications. They are
provided to support developing new communication protocols
or for access to more esoteric facilities of an existing protocol.
15
16. Client-Server Communication
Stream Sockets (TCP): Connection-oriented
Create a socket
Bind the socket
(what port am I on?)
Listen for client
(Wait for incoming connections)
Accept connection
Receive Request
Send response
Server
Client
Create a socket
Connect to server
Send the request
establish connection
data (request)
Receive response
data (reply)
16
17. Connection-oriented Example
(Stream Sockets -TCP)
socket()
bind()
listen()
accept()
recv()
send()
Server
Client
socket()
connect()
send()
establish connection
data (request)
recv()
data (reply)
17
18. Server: Server Preparing its Socket
• Server creates a socket and binds address/port
– Server creates a socket, just like the client does
– Server associates the socket with the port number
• Create a socket
– int socket(int domain,
int type, int protocol )
• Bind socket to the local address and port number
– int bind(int sockfd,
struct sockaddr *my_addr,
socklen_t addrlen )
18
19. Description of TCP Socket
• #include <sys/socket.h>
int socket( int domain, int type, int protocol);
The domain is an integer specifying the address family and protocol. These
families are defined in <sys/socket.h>. Some of the common domain
values are
Name Purpose
• PF_UNIX: Local communication
• PF_INETIPv4: Internet protocols
• PF_INET6IPv6: Internet protocols
19
20. Description of TCP Socket (cont..)
listen(), which is defined by
#include <sys/socket.h>
int listen(int sockfd, int queue_size);
sockfd is the usual socket file descriptor from the socket() system call and
the second is the maximum size of the queue of pending (incomplete)
connections.
The accept() call is defined as follows:
#include <sys/types.h>
#include <sys/socket.h>
int accept(int sockfd, struct sockaddr *addr, socklen_t *addrlen);
•addr will usually be a pointer to a local struct sockaddr_in. This is where the
information about the incoming connection will go (and with it you can
determine which host is calling you from which port).
20
21. Common Functions of TCP Socket
(cont..)
• addrlen is a local integer variable that should be set to sizeof(struct
sockaddr_in) before its address is passed to accept(). Accept will not put
more than that many bytes into addr. If it puts fewer in, it’ll change the
value of addrlen to reflect that.
• The shutdown(sockfd, SHUT_WR) system call turns off writing to the
socket.
#include <sys/socket.h>
int shutdown(int s, int how);
• convert() reads the connected socket until it receives the end-of-file
• The client uses ordinary read() and write() calls for its I/O operations.
• The return value of socket() is a file descriptor that can be used to read or
write the socket. As an example,
sockfd = socket(PF_INET, SOCK_STREAM, 0);
21
22. Stream Socket
• These two functions are for communicating over stream sockets or
connected datagram sockets. If you want to use regular unconnected
datagram sockets, you’ll need to see the section on sendto() and
recvfrom(), below.
• The send() call:
int send(int sockfd, const void *msg, int len, int flags);
• sockfd is the socket descriptor you want to send data to (whether it’s the
one returned by socket() or the one you got with accept().) msg is a
pointer to the data you want to send, and len is the length of that data in
bytes.
22
23. Stream Socket (Cont..)
• The recv() call is similar in many respects:
int recv(int sockfd, void *buf, int len, unsigned int flags);
• sockfd is the socket descriptor to read from, buf is the buffer to read the
information into, len is the maximum length of the buffer, and flags can
again be set to 0.
• recv() returns the number of bytes actually read into the buffer, or -1 on
error (with errno set, accordingly.)
• Wait! recv() can return 0. This can mean only one thing: the remote side
has closed the connection.
23
24. Generic Socket
The client does not need to remember the IP address of the server because
generic socket are used to convert hostname to address translation.
A generic socket address is defined by the sockaddr struct defined in
<sys/socket.h>:
struct sockaddr {
sa_family_t sa_family; /* address family */
char sa_data[]; /* socket address */
};
The address structure for PF_INET, defined in <netinet/in.h>, would be
struct sockaddr_in {
sa_family_t sin_family; /* internet address family */
in_port_t sin_port; /* port number */
struct in_addr sin_addr; /* IP address */
unsigned char sin_zero[8]; /* padding */ 24
25. Related Question
Q1: what are two types of Internet Sockets?
Q2: What is socket?
Q3: What uses stream sockets?
Q4: How do stream sockets achieve this high level of data transmission
quality?
Q5: What is Layered Model?
Q6: What are well-known port number?
25
26. Related Question
Q5: What is Layered Model?
A layered model more consistent with Unix might be:
• Application Layer (telnet, ftp, etc.)
• Host-to-Host Transport Layer (TCP, UDP)
• Internet Layer (IP and routing)
• Network Access Layer (Ethernet, wi-fi, or whatever)
26
27. REFERENCES
[1] W. R. Stevens, B. Fenner & A. M. Rudoff, Unix Network Programming,
Vol. I, 3rd Ed., Pearson Education.
[2] W. R. Stevens , Unix Network Programming, Vol. II, 2nd Ed., Pearson
Education.
[3] Comer and Stevens, Internetworking with TCP/IP, Vol. I, II and III, PHI.
[4] Christian Benvenuti, Understanding Linux Network Internals, O‘Reilly.
[5] W. R. Stevens , Advanced Programming in Unix Environment, Pearson
Education.
[6]Brian “Beej” Hall,"Beej’s Guide to Network Programming Using Internet
Sockets",2005.
[7] Xi Liu,"Socket Programming",Computer Networks, Spring 2008.
[8] Dave Hollinger,Socket Programming",Computer Network Systems.
27