Unix Unit-V
Unix Unit-V
Unit - V
Socket Programming
OSI (open system interconnection) Model
Socket
• Definition
port
Server request
Client
3
Socket Interface
socket socket
Application 1 Application 2
interface interface
user user
kernel kernel
Socket Socket
Underlying Underlying
communication communication
Protocols Protocols
Communication
network
4
Socket Address Structures
➢ process to kernel
5
IPv4 Socket Address Structure
netinet/in.h, sys/socket.h
struct sockaddr_in {
uint8_t sin_len;
sa_family_t sin_family; /* AF_INET */
in_port_t sin_port; /* 16-bit TCP or UDP port number
network byte ordered */
struct in_addr sin_addr; /* 32-bit IPv4 address network byte
ordered */
char sin_zero[8]; /* unused */ };
6
struct in_addr {
in_addr_t s_addr; /* 32-bit IPv4 address (network byte ordered) */
};
♦ sin_family = AF_INET
♦ sin_port: port # (512-65535)
♦ sin_addr: IP-address
♦ sin_zero: unused
7
Special IP addresses
IP address Description
8
TCP Socket
bind() socket()
socket() accept()
write()
read()
process request
read() write()
close()
read() close()
9
TCP Server Algorithm
10
TCP Client Algorithm
• Allocate a socket.
11
Value result arguments
• bind, connect and sendto pass socket address structure
from process to kernel.
• accept and recvfrom pass socket address structure from
kernel to process.
• In both the cases the arguments are pointer to socket
address structure and length.
• length is passed as a normal variable from process to
kernel.
• It is passed as a pointer from kernel to process.
• Reason for size changing from an integer to be a pointer
to an integer is size is both value and result
12
Value-Result Arguments
13
TCP Socket System calls
• socket
• bind
• listen
• accept
• connect
14
socket system call
Example:
if (( sd = socket (AF_INET, SOCK_STREAM, 0)) < 0)
perror (“socket call error”);
15
Protocol family constants
Family Description
AF_INET IPv4 protocols
AF_INET6 IPv6 protocols
AF_LOCAL Unix domain protocols
AF_ROUTE Routing sockets
AF_KEY Key socket
Type of socket
Type Description
SOCK_STREAM Stream socket (TCP)
SOCK_DGRAM Datagram socket (UDP)
SOCK_SEQPACKET Sequenced packet socket
SOCK_RAW Raw socket
16
Type Description
SOCK_DGRAM • Fixed length, connectionless, unreliable messages
• No logical connection is required between peers for
them to communicate.
• message oriented
• Aware of message boundaries
SOCK_STREAM • Sequenced, reliable, bidirectional, connection-
oriented
• Logical connection establishment is required between
peers for them to communicate.
• Byte stream oriented
• Unaware of message boundaries
17
SOCK_SEQPACKET • Fixed length, sequenced, reliable, connection-
oriented
• Logical connection establishment is required
between peers for them to communicate.
• Message oriented
• Aware of message boundaries
• SCTP sockets is default in AF_INET family
SOCK_RAW Datagram interface directly to the underlying
network layer.
Applications are responsible for building their own
protocol headers when using this interface, because
the transport protocols(for example: TCP and UDP)
are bypassed
18
Socket Creation
Write a program for creating a socket
#include <sys/types.h>
#include <sys/socket.h>
….
int sockfd;
….
sockfd = socket(AF_INET, SOCK_DGRAM, 0);
….
19
Assigning an address to a socket-- bind
bind Function
int bind (int sockfd, const struct sockaddr *myaddr,
socklen_t addrlen);
20
• The bind() system call is used to assign an IP address to
an existing socket.
21
Example :
#include<sys/socket.h>#include<sys/types.h> #include<netinet/in.h>
#include<arpa/inet.h>
struct sockaddr_in sin; int s;
s = socket(AF_INET, SOCK_STREAM, 0);
sin.sin_family = AF_INET;
sin.sin_port = htons(9999);
sin.sin_addr.s_addr = inet_addr(“0.0.0.0”);
22
listen()
int listen(int sockfd, int backlog)
♦ second argument is maximum number of connections that the kernel
should queue for this socket.
♦ applies only to sockets that have selected reliable stream delivery service.
24
Write a program to change mode of socket to passive
mode
if(listen(sockfd,5)<0)
exit(1);
printf(“listen successful”);
25
accept() -- TCP Sockets
int accept(int sockfd , struct sockaddr *cliaddr ,
socklen_t * addrlen)
returns 3 values
♦ an integer return code which is either a new socket
descriptor or an error indication.
♦ protocol address of the client process
♦ size of the client address
address.
27
• kernel creates a new connected socket for each client
28
int fd; /* socket descriptor */
struct sockaddr_in srv; /* used by bind() */
struct sockaddr_in cli; /* used by accept() */
int newfd; /* returned by accept() */
int cli_len = sizeof(cli); /* used by accept() */
29
connect()
Used by TCP client to establish connection
30
RTT – round trip time approximately 187ms
Complete queue must have more entries than incomplete
31
32
Kernel maintains 2 queues
33
Errors
34
RST is sent by server when
35
int fd; /* socket descriptor */
struct sockaddr_in srv; /* used by connect() */
connection.
new client.
37
pid_t pid; sfd=socket(..);
bind(…); listen(..);
for(;;)
{
nsfd=accept(..);
pid=fork();
if(pid==0)
{
close(sfd);
}
close(nsfd);
38
server client
Connection Before call to
sfd request connect()
accept
client
server
Connection
sfd connect() After return from
nsfd
accept
39
server(parent)
client
Connection
sfd connect()
nsfd
fork
sfd
nsfd
server(child)
After fork
40
server(parent)
client
sfd connect()
nsfd
server(child)
42
Basic UDP Socket I/O
• sendto()
# include <sys/socket.h>
int sendto(int s, void *buf, int len, unsigned int flags,
struct sockaddr *to, int tolen);
Returns: number of bytes sent, -1 on error
• recvfrom()
# include <sys/socket.h>
int recvfrom(int s, void *buf, int len, unsigned int flags,
struct sockaddr *from, int *fromlen);
Returns: number of bytes received, -1 on error
43
flags
Specifies the type of message reception.
44
UDP Sockets
socket()
process request
sendto()
recvfrom()
close()
45
UDP Server Algorithm
• Create a socket
46
UDP Client Algorithm
47
Simple UDP Server Example
#include <stdio.h> #include <sys/types.h> #include <sys/socket.h>
#include <netinet/in.h> #include <arpa/inet.h> #include <string.h>
#include <stdlib.h>
int main( ) {
int sfd, cliLen,n;
struct sockaddr_in srvAdr, cliAdr;
char mesg[100];
if((sfd = socket(AF_INET,SOCK_DGRAM,0)) < 0 ) {
perror("Can't open datagram socket \n"); }
srvAdr.sin_family = AF_INET;
srvAdr.sin_addr.s_addr = htonl (INADDR_ANY);
srvAdr.sin_port = htons(8000);
if(bind(sfd,(struct sockaddr*)&srvAdr,sizeof(srvAdr))<0) {
perror ("can't bind local address \n"); }
printf("server waiting for messages \n");
48
while(1)
{
cliLen = sizeof(cliAdr);
n = recvfrom(sfd,mesg,100,0 ,(struct sockaddr*)&cliAdr,&cliLen);
if(n < 0){ perror("recvfrom error \n"); }
if(sendto(sfd,mesg,n ,0,(struct sockaddr *)&cliAdr,cliLen) !=n) {
perror("sendto error \n"); }
printf("Received following message from client %s\n",mesg);
}
}
49
Simple UDP Client Example
#include <stdio.h> #include <sys/types.h> #include <sys/socket.h>
#include <netinet/in.h> #include <arpa/inet.h> #include <stdlib.h>
#include <string.h>
int main( ) {
int sfd,n;
struct sockaddr_in srvAdr;
char txmsg[100], rxmsg[100];
if((sfd = socket (AF_INET,SOCK_DGRAM,0))< 0) {
perror("can't open datagram socket \n"); }
srvAdr.sin_family = AF_INET;
srvAdr.sin_addr.s_addr = inet_addr("127.0.0.1");
srvAdr.sin_port = htons (8000);
printf("Enter message to send :\n");
fgets(txmsg,100,0);
50
n = strlen(txmsg);
if(sendto(sfd,txmsg,n,0,(struct sockaddr *)&srvAdr,sizeof(srvAdr ))!=n)
{
perror("sendto error \n");
}
n = recvfrom(sfd,rxmsg,100,0,NULL,NULL);
printf("n=%d\n",n);
printf("Received following message :%s\n",rxmsg);
}
51
End of Unit -V
52