190 likes | 198 Views
Learn about sockets, which act as endpoints in client-server communication over a network. Understand different protocols and socket address structures for IPv4 and IPv6. Discover socket system calls for connection-oriented and connectionless protocols.
E N D
INTRDUCTION TO SOCKETS A socket acts as an end point in connection between a client and a server present in a network. A process running on one computer can communicate with other processes running on distinct computer. To support IPC over a local area network , BSD (Berkeley Software Distribution) UNIX 4.2 Developed sockets which provide protocol-independent network interface services. The sockets are now available in BSDUNIX 4.3,4.4. Sockets can run on either : Connection –oriented protocol. (client server manner) Connection –less protocol(peer to peer manner)
Server Connection oriented Protocol.orClient-Server socket() “well-known” port bind() listen() Client accept() socket() (Block until connection) “Handshake” connect() Data (request) send() recv() Data (reply) send() recv() End-of-File close() recv() close()
Ports • Numbers (typical, since vary by OS): • 0-1023 “reserved”, must be root • 1024 - 5000 “ephemeral” • Above 5000 for general use • (50,000 is specified max) • Well-known, reserved services (see /etc/services in Unix): • ftp 21/tcp • telnet 23/tcp • finger 79/tcp • snmp 161/udp
Udp client-serveror Connection less protocol Server socket() “well-known” port bind() Client recvfrom() socket() Data (request) (Block until receive datagram) sendto() sendto() recvfrom() Data (reply) close() - No “handshake” - No simultaneous close
SOCKET ADDRESSES A socket address structure is a special structure that stores the connection details of a socket. It mainly consists of fields like IP address, port number and protocol family. The name of each socket address structure starts with “sockaddr_” followed by its unique name. Ipv4 socket address structure –“ struct sockaddr_in” Ipv6 socket address structure –“ struct sockaddr_in6” Generic socket address structure –“ struct sockaddr” New generic socket address structure –“ struct sockaddr”
ipv4 Socket Address Structure It is defined in header file <netinet/in.h> struct in_addr { in_addr_t s_addr; /* 32-bit IPv4 addresses */ }; struct sockaddr_in { unit8_t sin_len; /* unsigned 8 bit integer length of structure */ sa_family_t sin_family; /* AF_INET */ in_port_t sin_port; /* TCP/UDP Port num */ struct in_addr sin_addr; /* IPv4 address (above) */ char sin_zero[8]; /* unused */ }
ipv6 Socket Address Structure It is defined in header file <netinet/in.h> struct sock in6_addr { uint8_t sin6_len; /* length of this struct */ sa_family_t sin6_family; /* AF_INET6 */ in_port_t sin6_port; /* transport layer port # */ uint32_t sin6_flowinfo; /* IPv6 flow information */ struct in6_addr sin6_addr; /* IPv6 address */ uint32_t sin6_scope_id; /* set of interfaces for a scope */ };
Generic socket address structure It is defined in <sys/socket.h > header file. struct sockaddr { uint8_t sa_len; // sizeof this struct sa_family_t sa_family; // AF_INET|AF_INET6 char sa_data[14]; };
New Generic socket address structure struct sockaddr_storage { int8_t ss_len; /* length of this struct (implementation dependent) */ sa_family_t ss_family; /* address family: AF_xxx value */ };
Socket system callsfor connection oriented and connectionless protocol
socket() The system call socket creates one end of the socket #include<sys/types.h> #include<sys/socket.h int socket(int family or domain, int type, int protocol); • familyargument specifies the socket naming convention and the protocol address. • AF_INET (IPv4), AF_INET6 (IPv6), AF_UNIX (local Unix), • AF_ROUTE (access to routing tables), AF_KEY (new, for encryption) • Type argument specifies the socket type • SOCK_STREAM (TCP) • S OCK_DGRAM (UDP) • SOCK_SEQPACKET (Provides A Sequenced,realible,two Way, connection Based Transmission With Fixed Maximum Length) • Protocol argument specifies a particular protocol to be used with the socket. The values are depended on domain. Usually this is set to be ZERO.
bind() Bind associates an address to a socket descriptor created by socket.it binds a name to a socket. #include<sys/types.h> #include<sys/socket.h int bind(int sockfd, conststructsockaddr *myaddr, socklen_taddrlen); • Sockfd is socket descriptor from socket() • myaddrargument points to a structure that contains the name to be assigned to the socket.: • addrlen is length of structure • returns 0 if ok, -1 on error • EADDRINUSE (“Address already in use”)
listen() • This is called in a server process to establish a connection-based socket for communication (SOCK_STREAM (TCP) ,SOCK_SEQPACKET ) • #include<sys/types.h> • #include<sys/socket.h • int listen(int sockfd, int backlog); • sockfd is socket descriptor from socket() • backlog is maximum number of incomplete connections • historically 5 • rarely above 15 on a even moderate Web server! • Sockets default to active (for a client) • change to passive so OS will accept connection
connect() This is called in a client process in requestinga connection to a server socket. #include<sys/types.h> #include<sys/socket.h int connect(int sockfd, conststructsockaddr *servaddr, socklen_taddrlen); sockfd is socket descriptor from socket() servaddr is a pointer to the address of a sockaddr-type object that holds the name of the server socket to be connected: addrlen is length of structure returns socket descriptor if ok, -1 on error
accept() This is called in a server process to establish a connection-based socket connection with a client socket. #include<sys/types.h> #include<sys/socket.h int accept(int sockfd, structsockaddrcliaddr, socklen_t *addrlen); sockfd is socket descriptor from socket() cliaddr and addrlen return protocol address from client. returns socket descriptor if ok, -1 on error
close() #include<sys/types.h> #include<sys/socket.h int close(int sockfd); Close socket for use. • sockfd is socket descriptor from socket() • closes socket for reading/writing • returns -1 if error
Sending and Receiving #include<sys/types.h> #include<sys/socket.h int recv(int sockfd, void *buff, size_tmbytes, int flags); int send(int sockfd, void *buff, size_tmbytes, int flags); • Same as read() and write() but for flags • MSG_DONTWAIT (this send non-blocking) • MSG_OOB (out of band data, 1 byte sent ahead) • MSG_PEEK (look, but don’t remove) • MSG_WAITALL (don’t give me less than max) • MSG_DONTROUTE (bypass routing table)
Sending and Receiving #include<sys/types.h> #include<sys/socket.h int recvfrom(int sockfd, void *buff, size_tmbytes, int flags, structsockaddr *from, socklen_t *addrlen); int sendto(int sockfd, void *buff, size_tmbytes, int flags, conststructsockaddr *to, socklen_taddrlen); • Same as recv() and send() but for addr • recvfrom fills in address of where packet came from • sendto requires address of where sending packet to