Operating Systems Chapter 9 Distributed Communication.

Slides:



Advertisements
Similar presentations
Introduction to Sockets Jan Why do we need sockets? Provides an abstraction for interprocess communication.
Advertisements

Socket Programming CS3320 Fall 2010.
Sockets: Network IPC Internet Socket UNIX Domain Socket.
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.
Networks: TCP/IP Socket Calls1 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.
1 Netcomm Recitation 1: Sockets Communication Networks Recitation 1.
1 Netcomm Sockets Communication Networks Recitation 1.
Socket Programming: a Primer Socket to me!. Feb. 23, 2001EE122, UCB2 Why does one need sockets? application network protocol sockets network.
1 Socket Interfaces Professor Jinhua Guo CIS527 Fall 2003.
Tutorial 8 Socket Programming
CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS Operating Systems.
Introduction to Socket Programming April What is a socket? An interface between application and network –The application creates a socket –The socket.
ISP – 9 th Recitation Socket programming – Client side.
Introduction to Project 1 Web Client and Server Jan 2006.
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.
1 Socket Programming r What is a socket? r Using sockets m Types (Protocols) m Associated functions m Styles.
Network Programming Sockets and Winsock. Please Be Responsible We all know that the Internet is full of security holes –most of them do not require any.
Basic Socket Programming TCP/IP overview. TCP interface Reference: –UNIX Network Programming, by Richard Stevens. –UNIX man page.
Socket Programming Based on tutorial prepared by EUISOK CHUNG CS3320 Spring2008.
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.
1 Networking (Stack and Sockets API). 2 Topic Overview Introduction –Protocol Models –Linux Kernel Support TCP/IP Sockets –Usage –Attributes –Example.
Zhu Reference: Daniel Spangenberger Computer Networks, Fall 2007 PPT-4 Socket Programming.
CS345 Operating Systems Φροντιστήριο Άσκησης 2. Inter-process communication Exchange data among processes Methods –Signal –Pipe –Sockets.
Sirak Kaewjamnong Computer Network Systems
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.
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.
CS x760 Computer Networks1 Socket Programming. CS 6760 Computer Networks2 Socket Programming  What is a socket?  Using sockets  Types (Protocols) ‏
Remote Shell CS230 Project #4 Assigned : Due date :
Computer Networks : TP1 Prof. Dr. Amine Berqia and Prof. Dr. Fernando Lobo {bamine,
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.
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.
1 Introduction to Computer Networks Ilam University By: Dr. Mozafar Bag Mohammadi Sockets.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
TELE202 Lecture 15 Socket programming 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »TCP/UDP (2) »Source: chapter 17 ¥This Lecture »Socket programming.
UNIT8: SOCKETS Topics Introduction to sockets Socket Addresses
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
Socket Programming Lab 1 1CS Computer Networks.
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 m Note:
S OCKET P ROGRAMMING IN C Professor: Dr. Shu-Ching Chen TA: HsinYu Ha.
Socket address structures Byte ordering IP address conversion
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.
Read() recv() connection establishment Server (connection-oriented protocol) blocks until connection from client Client socket() bind() listen() accept()
2: Application Layer 1 Socket Programming UNIX Network Programming, Socket Programming Tutorial:
CSCI 330 UNIX and Network Programming Unit XIV: User Datagram Protocol.
回到第一頁 Client/sever model n Client asks (request) – server provides (response) n Typically: single server - multiple clients n The server does not need.
1 Spring Semester 2008, Dept. of Computer Science, Technion Internet Networking recitation #7 Socket Programming.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
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 What is a socket? Using sockets Types (Protocols)
CSCE 313 Network Socket MP8 DUE: FRI MAY 5, 2017
Jim Fawcett CSE 681 – Software Modeling & Analysis Fall 2002
Socket Programming in C
Introduction to Computer Networks
Tutorial on Socket Programming
Transport layer API: Socket Programming
Socket Programming What is a socket? Using sockets Types (Protocols)
Socket Programming.
Socket Programming What is a socket? Using sockets Types (Protocols)
Socket Programming What is a socket? Using sockets Types (Protocols)
Internet Networking recitation #8
Socket Programming What is a socket? Using sockets Types (Protocols)
Sockets.
Presentation transcript:

Operating Systems Chapter 9 Distributed Communication

Socket Programming What is a socket? Using sockets Types (Protocols) Associated functions Styles

What is a socket? An interface between application and network The application creates a socket The socket type dictates the style of communication reliable vs. best effort connection-oriented vs. connectionless Once configured the application can pass data to the socket for network transmission receive data from the socket (transmitted through the network by some other host)

Two essential types of sockets SOCK_STREAM a.k.a. TCP reliable delivery in-order guaranteed connection-oriented bidirectional SOCK_DGRAM a.k.a. UDP unreliable delivery no order guarantees can send or receive App socket Dest. App socket D1 D3 D2

A Socket-eye view of the Internet Each host machine has an IP address Each host has 65,536 ports Some ports are reserved for specific apps 20,21: FTP 23: Telnet 80: HTTP cluster.cs.columbia.edu ( , , , ) Port 0 Port 1 Port 65535

Addresses, Ports and Sockets Like apartments and mailboxes You are the application Your apartment building address is the address Your mailbox is the port The post-office is the network The socket is the key that gives you access to the right mailbox (one difference: assume outgoing mail is placed by you in your mailbox)

Sockets System Calls Creating a Socket (both client and server) #include int socket(int format,int type,int protocol) format – AF_UNIX, AF_INET type – SOCK_STREAM, SOCK_DGRAM protocol – 0 return value – a socket descriptor or -1

Socket Addresses struct in_addr { unsigned long int s_addr; }; struct sockaddr_in { short sin_family; u_short sin_port; struct in_addr sin_addr; }; sin_family = AF_INET sin_port: port # ( ) sin_addr: IP-address

Naming a socket - bind(server) associates and (can exclusively) reserves a port for use by the socket #include int bind(int socket,const struct socket *address,size_t address_len) socket – the socket descriptor address – mainly the port number address_len – the structure size return value – 0 on success or -1

Creating a Socket Queue – listen (server) #include int listen(int socket,int backlog) socket – the socket descriptor backlog – maximum queue length return value – 0 on success or -1 listen is non-blocking: returns immediately

Accepting Connections – accept (server) #include int accept(int sock,sockaddr *name,size_t *namelen); s: integer, the new socket (used for data-transfer) sock: integer, the orig. socket (being listened on) name: struct sockaddr, address of the client namelen: sizeof(name): the client structure size accept is blocking: waits for connection before returning

Requesting Connection – connect (client) #include int connect(int sock,const struct sockaddr *name, size_t namelen); return value: 0 if successful connect, -1 otherwise sock: integer, socket to be used in connection name: struct sockaddr: address server namelen: integer, sizeof(name) connect is blocking

Address and port byte-ordering Address and port are stored as integers (32 bit or 16 bit) 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 WRONG!!!

Solution: Network Byte-Ordering 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)

UNIX’s byte-ordering funcs u_long htonl(u_long x); u_short htons(u_short x); u_long ntohl(u_long x); u_short ntohs(u_short x); On big-endian machines, these routines do nothing On little-endian machines, they reverse the byte order Same code would have worked regardless of endian-ness of the two machines Little-Endian machine htonl ntohl Big-Endian machine

Reading / Writing - functions ret = write(sd,buff,buff_len) ret = read(sd,buff,buff_len) ret = close(sd)

Other useful functions strucet hostent *gethostname(char *name, int len) gets the name of the current host struct hostent *gethostbyaddr(char *addr, int len, int type) converts IP hostname to structure containing long integer long int inet_addr(const char *cp) converts dotted-decimal char-string to long integer char* inet_ntoa(const struct in_addr in) converts long to dotted-decimal notation

Release of ports Sometimes, a “rough” exit from a program (e.g., ctrl-c) does not properly free up a port Eventually (after a few minutes), the port will be freed To reduce the likelihood of this problem, include the following code: #include void cleanExit(){exit(0);} in socket code: signal(SIGTERM, cleanExit); signal(SIGINT, cleanExit);