UNIT8: SOCKETS Topics Introduction to sockets Socket Addresses

Slides:



Advertisements
Similar presentations
Sockets: Network IPC Internet Socket UNIX Domain Socket.
Advertisements

Sockets CS 3516 – Computer Networks. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Distributed Computing Systems Sockets. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
Quick Overview. 2 ISO/OSI Reference Model Application Application Presentation Presentation Session Session Transport Transport Network Network Data Link.
Socket Programming.
Multimedia Networking Sockets. Outline Socket basics Socket details (TCP and UDP) Socket options Final notes.
Tutorial 8 Socket Programming
Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.
CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS Operating Systems.
CSE/EE 461 Getting Started with Networking. Basic Concepts  A PROCESS is an executing program somewhere.  Eg, “./a.out”  A MESSAGE contains information.
Lecture 10 Overview. Network API Application Programming Interface – Services that provide the interface between application and protocol software often.
UNIX Sockets COS 461 Precept 1. Clients and Servers Client program – Running on end host – Requests service – E.g., Web browser Server program – Running.
1 Tutorial on Socket Programming Computer Networks - CSC 458 Department of Computer Science Yukun Zhu (Slides are mainly from Monia Ghobadi, and Amin Tootoonchian,
UNIX Sockets COS 461 Precept 1.
Sockets CIS 370 Fall 2009, UMassD. Introduction  Sockets provide a simple programming interface which is consistent for processes on the same machine.
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
CPSC 441 TUTORIAL – JANUARY 18, 2012 TA: MARYAM ELAHI INTRODUCTION TO SOCKET PROGRAMMING WITH C.
TCP Socket Programming. r An abstract interface provided to the application programmer  File descriptor, allows apps to read/write to the network r Allows.
ECE 4110 – Internetwork Programming Client-Server Model.
Sockets and intro to IO multiplexing. Goals We are going to study sockets programming as means to introduce IO multiplexing problem. We will revisit socket.
1 Networking (Stack and Sockets API). 2 Topic Overview Introduction –Protocol Models –Linux Kernel Support TCP/IP Sockets –Usage –Attributes –Example.
Operating Systems Chapter 9 Distributed Communication.
Socket Programming. Introduction Sockets are a protocol independent method of creating a connection between processes. Sockets can be either – Connection.
Chapter 17 Networking Dave Bremer Otago Polytechnic, N.Z. ©2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E William Stallings.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
Sockets CIS 370 Lab 10 UMass Dartmouth. Introduction 4 Sockets provide a simple programming interface which is consistent for processes on the same machine.
Server Sockets: A server socket listens on a given port Many different clients may be connecting to that port Ideally, you would like a separate file descriptor.
 Wind River Systems, Inc Chapter - 13 Network Programming.
Chapter 2 Applications and Layered Architectures Sockets.
1 Socket Programming r What is a socket? r Using sockets m Types (Protocols) m Associated functions m Styles m We will look at using sockets in C.
Remote Shell CS230 Project #4 Assigned : Due date :
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
CPSC 441 TUTORIAL – FEB 13, 2012 TA: RUITNG ZHOU UDP REVIEW.
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
University of Calgary – CPSC 441.  A socket is an interface between the application and the network (the lower levels of the protocol stack)  The application.
UNIX Sockets COS 461 Precept 1. Socket and Process Communication The interface that the OS provides to its networking subsystem application layer transport.
Introduction to Socket
CSE/EE 461 Getting Started with Networking. 2 Basic Concepts A PROCESS is an executing program somewhere. –Eg, “./a.out” A MESSAGE contains information.
Socket Programming Lab 1 1CS Computer Networks.
1 Sockets Programming Socket to me!. 2 Network Application Programming Interface (API) The services provided by the operating system that provide the.
Socket Programming Introduction. Socket Definition A network socket is one endpoint in a two-way communication flow between two programs running over.
Socket Programming.
CSCI 330 UNIX and Network Programming Unit XV: Transmission Control Protocol.
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
Sockets Introduction Socket address structures Value-result arguments
Introduction to Sockets
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Review: – Why layer architecture? – peer entities – Protocol and service interface – Connection-oriented/connectionless service – Reliable/unreliable service.
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
Socket Programming. Computer Science, FSU2 Interprocess Communication Within a single system – Pipes, FIFOs – Message Queues – Semaphores, Shared Memory.
UNIX Sockets Outline UNIX sockets CS 640.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
Lecture 15 Socket Programming CPE 401 / 601 Computer Network Systems slides are modified from Dave Hollinger.
1 Network Communications A Brief Introduction. 2 Network Communications.
1 Socket Interface. 2 Client-Server Architecture The client is the one who speaks first Typical client-server situations  Client and server on the same.
Sockets Intro to Network Programming. Before the internet... Early computers were entirely isolated No Internet No network No model No external communications.
1 Socket Interface. 2 Basic Sockets API Review Socket Library TCPUDP IP EthernetPPP ARP DHCP, Mail, WWW, TELNET, FTP... Network cardCom Layer 4 / Transport.
SOCKET PROGRAMMING Presented By : Divya Sharma.
UNIX Sockets COS 461 Precept 1.
Socket Programming in C
Tutorial on Socket Programming
Transport layer API: Socket Programming
Socket Programming in C
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Outline Communications in Distributed Systems Socket Programming
Sockets.
Presentation transcript:

UNIT8: SOCKETS Topics Introduction to sockets Socket Addresses Socket system calls for connection oriented protocol and connection less protocol Example client/server programs

Introduction Pipes,FIFOs, UNIX system V IPC methods (message queues,shared memory, semaphores) are useful for process communicating on the same machine, but they do not support processes running on different machines to communicate. Socket supports IPC with in the same machine and support processes running on the different machines to communicate. To support IPC over a local area network BSD UNIX 4.2 developed sockets which provide protocol independent network interface services.

What is socket A socket acts as an end point in connection between client and a server present in a network. Socket is an interface between application and network. -The application creates a socket -The socket type dictates the style of communication End point determined by two things: Host address: IP address is Network Layer Port number: is Transport Layer Two end-points determine a connection: socket pair ex: 206.62.226.35,p21 + 198.69.10.2,p1500 ex: 206.62.226.35,p21 + 198.69.10.2,p1499

Socket and Process Communication application layer application layer User Process Internet User Process Socket Socket OS network stack OS network stack transport layer (TCP/UDP) network layer (IP) link layer (e.g. ethernet) transport layer (TCP/UDP) network layer (IP) link layer (e.g. ethernet) Internet Internet The interface that the OS provides to its networking subsystem

Two Types Sockets Datagram Socket (UDP) Stream Socket (TCP) Collection of messages Best effort Connectionless: sender or receiver address must be passed along with each message sent from one process to another Stream Socket (TCP) Stream of bytes Reliable Connection-oriented: sender and receiver socket addresses are pre established before messages are passed between them.

User Datagram Protocol (UDP): Datagram Socket 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 messages Unreliable  Not necessarily in-order delivery Each letter is independent Must address each reply Postal Mail Single mailbox to receive letters Unreliable Not necessarily in-order delivery Letters sent independently Must address each mail Example UDP applications Multimedia, voice over IP (Skype)

Transmission Control Protocol (TCP): Stream Socket Reliable – guarantee delivery Byte stream – in-order delivery Connection-oriented – single socket per connection Setup connection followed by data transfer Postal Mail Single mailbox to receive messages Unreliable  Not necessarily in-order delivery Each letter is independent Must address each reply Telephone Call Guaranteed delivery In-order delivery Connection-oriented Setup connection followed by conversation Example TCP applications Web, Email, Telnet

Clients and Servers Client program Server program Running on end host Requests service E.g., Web browser Server program Running on end host Provides service E.g., Web server GET /index.html “Site under construction”

Client-Server Communication Client “sometimes on” Initiates a request to the server when interested E.g., Web browser on your laptop or cell phone Doesn’t communicate directly with other clients Needs to know server’s address Server is “always on” Handles services requests from many client hosts E.g., Web server for the www.cnn.com Web site Doesn’t initiate contact with the clients Needs fixed, known address

Client and Server Processes Client process process that initiates communication Server Process process that waits to be contacted

Client-Server Communication Stream Sockets (TCP): Connection-oriented Create a socket Bind the socket (what port am I on?) Client Listen for client (Wait for incoming connections) Create a socket establish connection Connect to server Accept connection data (request) Send the request Receive Request data (reply) Send response Receive response

Connection-oriented Example (Stream Sockets -TCP) Server socket() bind() Client listen() socket() establish connection connect() accept() data (request) send() recv() data (reply) send() recv()

Client-Server Communication Datagram Sockets (UDP): Connectionless Create a socket Create a socket Bind the socket Bind the socket data (request) Send the request Receive Request data (reply) Send response Receive response

Connectionless Example (Datagram Sockets - UDP) Server Client socket() socket() bind() bind() data (request) sendto() recvfrom() data (reply) sendto() recvfrom()

Socket Addresses 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

Socket Identification (Cont.) Process A Process B port X port Y Port Number TCP/UDP Protocol IP Host Address Ethernet Adapter

Ports Each host has 65,536 ports Some ports are reserved for specific apps 20,21: FTP 23: Telnet 80: HTTP Port 0 Port 1 Port 65535 A socket provides an interface to send data to/from the network through a port

Knowing What Port Number To Use Popular applications have well-known ports E.g., port 80 for Web and port 25 for e-mail See http://www.iana.org/assignments/port-numbers Well-known vs. ephemeral ports Server has a well-known port (e.g., port 80) Between 0 and 1023 (requires root to use) Client picks an unused ephemeral (i.e., temporary) port Between 1024 and 65535 Uniquely identifying traffic between the hosts Two IP addresses and two port numbers Underlying transport protocol (e.g., TCP or UDP)

Socket Address Structure 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. Different protocol suites use different socket address structures. The different socket address structures are IPv4 socket address structure: struct sockaddr_in IPv46 socket address structure: struct sockaddr_in6 Generic socket address structure: struct sockaddr

Generic socket addresses Used by kernel struct sockaddr { uint8_t sa_len; sa_family_t sa_family; char sa_data[14]; }; sa_family specifies the address type. sa_data specifies the address value. Socket Programming

struct sockaddr_in (IPv4) Length of structure (16) struct sockaddr_in { uint8_t sin_len; sa_family_t sin_family; in_port_t sin_port; struct in_addr sin_addr; char sin_zero[8]; }; struct in_addr { in_addr_t s_addr; AF_INET 16 bit Port number Make structure 16 bytes 32 bit IPv4 address Socket Programming

struct sockaddr_in (IPv6) uint8_t sin6_len; sa_family_t sin6_family; in_port_t sin6_port; uint32_t sin6_flowinfo; struct in6_addr sin6_addr; uint32_t sin6_scope_id; }; struct in6_addr { uint8_t s6_addr[16]; Length of structure (28) AF_INET6 Port number Flow label Scope of address 128 bit IPv6 address Socket Programming

Address and port byte-ordering Address and port are stored as integers u_short sin_port; (16 bit) in_addr sin_addr; (32 bit) struct in_addr { u_long s_addr; }; Problem: different machines / OS’s use different word orderings little-endian: lower bytes first big-endian: higher bytes first these machines may communicate with one another over the network Big-Endian machine Little-Endian machine 12.40.119.128 128.119.40.12 WRONG!!! 128 119 40 12 128 119 40 12

Solution: Network Byte-Ordering Defs: Host Byte-Ordering: the byte ordering used by a host (big or little) Network Byte-Ordering: the byte ordering used by the network – always big-endian Any words sent through the network should be converted to Network Byte-Order prior to transmission (and back to Host Byte-Order once received)

Network Byte Order Functions ‘h’ : host byte order ‘n’ : network byte order ‘s’ : short (16bit) ‘l’ : long (32bit) uint16_t htons(uint16_t); uint16_t ntohs(uint_16_t); uint32_t htonl(uint32_t); uint32_t ntohl(uint32_t); Socket Programming

UNIX’s byte-ordering funcs Same code would have Big-Endian machine Little-Endian machine 128 119 40 12 128.119.40.12 128 119 40 12 128.119.40.12 htonl ntohl 128 119 40 12 128 119 40 12

Socket system calls for connection oriented and connection less protocols bind() listen() accept() connect() send(),sendto() recv(),recvfrom() Close()

socket(): creating a socket socket - create an endpoint for communication The socket() function creates an unbound socket in a communications domain, and returns a file descriptor that can be used in later function calls that operate on sockets. Syntax: #include <sys/socket.h> int socket(int domain, int type, int protocol); The domain argument specifies the address family used in the communications domain. The address families supported by the system are implementation-dependent. The <sys/socket.h> header defines at least the following values for the domain argument: AF_UNIX File system pathnames. AF_INET internet address.

The type argument specifies the socket type, which determines the semantics of communication over the socket. The socket types supported by the system are implementation-dependent. Possible socket types include: SOCK_STREAM :Establishes a virtual circuit for communication. Messages are sent in a sequenced,reliable, SOCK_DGRAM Provides datagrams, which are connectionless-mode, unreliable messages of fixed maximum length. SOCK_SEQPACKET Provides sequenced, reliable, bidirectional, connection-mode transmission path for records. A record can be sent using one or more output operations and received using one or more input operations, but a single operation never transfers part of more than one record. Record boundaries are visible to the receiver via the MSG_EOR flag.

Protocol :Specifies a particular protocol to be used with the socket Protocol :Specifies a particular protocol to be used with the socket. Specifying a protocol of 0 causes socket() to use an unspecified default protocol appropriate for the requested socket type. If the protocol argument is non-zero, it must specify a protocol that is supported by the address family. The protocols supported by the system are implementation-dependent. RETURN VALUE Upon successful completion, socket() returns a nonnegative integer, the socket file descriptor. Otherwise a value of -1 is returned and errno is set to indicate the error.

bind()- binds a name to a socket The bind() function assigns an address to an unnamed socket. Sockets created with socket() function are initially unnamed; they are identified only by their address family. Syntax: #include<sys/types.h> #include <sys/socket.h> int bind(int socketid, const struct sockaddr *address, socklen_t address_len); The function takes the following arguments: socketid :Specifies the socket descriptor of the socket to be bound. address: Points to a sockaddr structure containing the address to be assigned to the socket. The length and format of the address depend on the address family of the socket. address_len Specifies the length of the sockaddr structure pointed to by the address argument. Upon successful completion, bind() returns 0. Otherwise, -1 is returned and errno is set to indicate the error

listen() listen for socket connections and limit the queue of incoming connections . Syntax: #include<sys/types.h> #include <sys/socket.h> int listen(int socketid, int backlog); The listen() function marks a connection-mode socket, specified by the socket argument, as accepting connections, and limits the number of outstanding connections in the socket's listen queue to the value specified by the backlog argument. The socketid argument is a socket descriptor , as returned by a socket sunction call. The backlog argument specifies the maximum number of connection requests may be queued for the socket. In most UNIX systems , the maximum allowed value for yhe backlog argument is 5 Upon successful completions, listen() returns 0. Otherwise, -1 is returned and errno is set to indicate the error.

accept() - accept a new connection on a socket A server accepts a connection request from a client socket. This is called in a server process to establish a connection based socket connection with a client socket(which calls connect to request connection establishment Syntax #include<sys/types.h> #include <sys/socket.h> int accept (int socketid, struct sockaddr *address, socklen_t *address_len); The accept() function extracts the first connection on the queue of pending connections, creates a new socket with the same socket type protocol and address family as the specified socket, and allocates a new file descriptor for that socket.

Argumentts of accept() The socketid argument is a socket descriptor , as returned by a socket function call. The address argument is a pointer to the address of a socketaddr typed object that holds the name of a client socket where the server socket is connected. The address_len argument is initially set to the maximum size of the object pointed to by the address argument.

Connect()-connect a socket The connect() function requests a connection to be made on a socket. A client socket sends a connection request to a server socket. Syntax: #include<sys/types.h> #include <sys/socket.h> int connect(int socketid, const struct sockaddr *address, socklen_t address_len); socketid Specifies the file descriptor associated with the socket. address Points to a sockaddr structure that holds the name of the server socket to be connected. address_len Specifies the length of the sockaddr structure pointed to by the address argument.

send(), sendto() - send a message on a socket The send() function initiates transmission of a message from the specified socket to its peer. The send() function sends a message only when the socket is connected. Syntax: #include<sys/types.h> #include <sys/socket.h> int send(int socketid, const void *buffer, size_t length, int flags); This function sends a message, contained in buffer of size length bytes, to a socket that is connected to the socket, as designated by socketid

sendto() - send a message on a socket The sendto() function sends a message through a connection-mode or connectionless-mode socket. If the socket is connectionless-mode, the message will be sent to the address specified by dest_addr. If the socket is connection-mode, dest_addr is ignored. Syntax #include<sys/types.h> #include <sys/socket.h> ssize_t sendto(int socketid, const void *buffer, size_t length, int flags, const struct sockaddr *dest_addr, socklen_t dest_len); This function is same as the send API , except that the calling process also specifies the address of the reciepent socket name via dest_addr and dest_len.

recv() - receive a message from a connected socket Syntax: #include<sys/types.h> #include <sys/socket.h> ssize_t recv(int socketid, void *buffer, size_t length, int flags); The recv() function receives a message from a connection-mode or connectionless-mode socket. It is normally used with connected sockets because it does not permit the application to retrieve the source address of received data.

The recv() function takes the following arguments: socketid Specifies the socket file descriptor. buffer Points to a buffer where the message should be stored. length Specifies the length in bytes of the buffer pointed to by the buffer argument. flags Specifies the type of message reception. Values of flags argument are formed by logically OR'ing zero or more of the following values: MSG_PEEK Peeks at an incoming message. The data is treated as unread and the next recv() or similar function will still return this data. MSG_OOB Requests out-of-band data. The significance and semantics of out-of-band data are protocol-specific. MSG_WAITALL Requests that the function block until the full amount of data requested can be returned. The function may return a smaller amount of data if a signal is caught, if the connection is terminated, if MSG_PEEK was specified, or if an error is pending for the socket

recvfrom() - receive a message from a socket Syntax #include<sys/types.h> #include <sys/socket.h> ssize_t recvfrom(int socketid, void *buffer, size_t length, int flags, struct sockaddr *address, socklen_t *address_len); The recvfrom() function receives a message from a connection-mode or connectionless-mode socket. It is normally used with connectionless-mode sockets because it permits the application to retrieve the source address of received data. This function is same as the recv API , except that the calling process also specifies the address of the sender socket name via address and address_len.

The function takes the following arguments: Socketid Specifies the socket file descriptor. Buffer Points to the buffer where the message should be stored. length Specifies the length in bytes of the buffer pointed to by the buffer argument. flags Specifies the type of message reception. Values of this argument are formed by logically OR'ing zero or more of the following values: MSG_PEEK: Peeks at an incoming message. The data is treated as unread and the next recvfrom() or similar function will still return this data. MSG_OOB :Requests out-of-band data. The significance and semantics of out-of-band data are protocol-specific. MSG_WAITALL: Requests that the function block until the full amount of data requested can be returned. The function may return a smaller amount of data if a signal is caught, if the connection is terminated, if MSG_PEEK was specified, or if an error is pending for the socke

shutdown() This function closes the connection between a server and client socket. Syntax: #include<sys/types.h> #include <sys/socket.h> int shutdown(int sid,int mode); The sid argument is a socket descriptor, as returned from a socket function call. This is the socket where the shutdown should occur.

The mode argument specifies the type of shutdown desired The mode argument specifies the type of shutdown desired . Its possible values and meanings are: Mode Meaning 0 Closes the socket for reading. All further reading will return zero bytes(EOF) 1 Closes the socket for writing. Further attempts to send data to the socket will retun a -1 failure code. 2 Closes the socket for reading and writing. Further attempts to send data the data to the socket will return a -1 failure code, and any attempt to read data from the socket will receive a zero value(EOF)

Example programs TCP/IP client server program UDP client server program