Quick Overview. 2 ISO/OSI Reference Model Application Application Presentation Presentation Session Session Transport Transport Network Network Data Link.

Slides:



Advertisements
Similar presentations
Today’s topic: Basic TCP API –Socket –Bind –Listen –Connect –Accept –Read –Write –Close.
Advertisements

15-441: Computer Networking Lecture 3: Application Layer and Socket Programming.
Elementary TCP Sockets Computer Networks Computer Networks Term B10 UNIX Network Programming Vol. 1, Second Ed. Stevens Chapter 4.
Sockets Programming CS144 Review Session 1 April 4, 2008 Ben Nham.
Networks: TCP/IP Socket Calls1 Elementary TCP Sockets Chapter 4 UNIX Network Programming Vol. 1, Second Ed. Stevens.
EECS122 Communications Networks Socket Programming January 30th, 2003 Jörn Altmann.
1 Socket Interfaces Professor Jinhua Guo CIS527 Fall 2003.
Tutorial 8 Socket Programming
UDP: User Datagram Protocol. UDP: User Datagram Protocol [RFC 768] r “bare bones”, “best effort” transport protocol r connectionless: m no handshaking.
CS 311 – Lecture 18 Outline IPC via Sockets – Server side socket() bind() accept() listen() – Client side connect() Lecture 181CS Operating Systems.
Socket Addresses. Domains Internet domains –familiar with these Unix domains –for processes communicating on the same hosts –not sure of widespread use.
Winsock programming.  TCP/IP UDP TCP  Winsock #include wsock32.lib.
Introduction to Project 1 Web Client and Server Jan 2006.
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.
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.
Lecture 2 Socket Programming Spring 2004
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.
1 Networking (Stack and Sockets API). 2 Topic Overview Introduction –Protocol Models –Linux Kernel Support TCP/IP Sockets –Usage –Attributes –Example.
TCP/IP Protocol Stack IP Device Drivers TCPUDP Application Sockets (Gate to network) TCP: –Establish connection –Maintain connection during the communication.
Operating Systems Chapter 9 Distributed Communication.
Week 13 - Monday.  What did we talk about last time?  Low level file I/O practice  File systems.
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.
 Wind River Systems, Inc Chapter - 13 Network Programming.
University of Texas at Austin CS 378 – Game Technology Don Fussell CS 378: Computer Game Technology Networking Concepts, Socket Programming Spring 2012.
1 Internet Socket programming Behzad Akbari. 2 Sharif University of Technology, Kish Island Campus What is an API? API – stands for Application Programming.
Introduction to Socket Programming Yen-Nien Wu CSIE, NTU.
1 Introduction to Computer Networks Ilam University By: Dr. Mozafar Bag Mohammadi Sockets.
The Pocket Guide to TCP/IP Sockets: C Version Michael J. Donahoo Kenneth L. Calvert.
Networking Tutorial Special Interest Group for Software Engineering Luke Rajlich.
1 Computer Networks An Introduction to Computer Networks University of Tehran Dept. of EE and Computer Engineering By: Dr. Nasser Yazdani Lecture 3: Sockets.
TELE202 Lecture 15 Socket programming 1 Lecturer Dr Z. Huang Overview ¥Last Lecture »TCP/UDP (2) »Source: chapter 17 ¥This Lecture »Socket programming.
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 Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
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.
2: Application Layer1 Chapter 2: Application layer r 2.1 Principles of network applications r 2.2 Web and HTTP r 2.3 FTP r 2.4 Electronic Mail  SMTP,
Intro to Socket Programming CS 360. Page 2 CS 360, WSU Vancouver Two views: Server vs. Client Servers LISTEN for a connection and respond when one is.
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.
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.
Socket Programming in C CS587x Lecture 3 Department of Computer Science Iowa State University.
Socket Programming(1/2). Outline  1. Introduction to Network Programming  2. Network Architecture – Client/Server Model  3. TCP Socket Programming.
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.
Network Programming CSC- 341
Network Programming with Sockets
Review: TCP Client-Server Interaction
Introduction to Computer Networks
Imam Ahmad Trinugroho, ST., MMSI
Transport layer API: Socket Programming
Chapter 2 Application Layer
Tutorial on Socket Programming
Socket Programming(1/2)
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Internet Networking recitation #8
Outline Communications in Distributed Systems Socket Programming
Today’s topic: Basic TCP API
Presentation transcript:

Quick Overview

2 ISO/OSI Reference Model Application Application Presentation Presentation Session Session Transport Transport Network Network Data Link Data Link Physical Physical

3 TCP/IP Model Application Application Transport Transport Network Network Data Link Data Link Physical Physical

Berkeley Sockets Based on a presentation originally by Josh Brock circa 1999.

5 Server and Client TCP/UDP IP Ethernet Adapter Server TCP/UDP IP Ethernet Adapter Clients Server and Client exchange messages over the network through a common Socket API Socket API hardware kernel space user space ports

6 UDP vs. TCP User Datagram Protocol (UDP) –Unreliable, datagram –Similar to communication via Each message is an independent chunk of data. Each message is an independent chunk of data. Transmission Control Protocol (TCP) Transmission Control Protocol (TCP) –reliable, byte-stream –similar to communication on the telephone No messages, just a continuous stream of bytes. No messages, just a continuous stream of bytes. Example applications Example applications –UDP: multimedia applications –TCP: Web, , Telnet

7 A socket is a file descriptor that lets an application read/write data from/to the network A socket is a file descriptor that lets an application read/write data from/to the network socket returns an integer (socket descriptor) socket returns an integer (socket descriptor) –fd < 0 indicates that an error occurred –socket descriptors are similar to file descriptors AF_INET: associates a socket with the Internet protocol family AF_INET: associates a socket with the Internet protocol family SOCK_STREAM: selects the TCP protocol SOCK_STREAM: selects the TCP protocol SOCK_DGRAM: selects the UDP protocol SOCK_DGRAM: selects the UDP protocol What is a Socket? int fd; /* socket descriptor */ if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) } perror(“socket”); exit(1); }

8 What is a Port? A Port Number? –Port numbers are used to identify “entities” on a host –Port numbers can be well-known (port ) well-known (port ) dynamic or private (port ) dynamic or private (port ) –Servers/daemons usually use well-known ports any client can identify the server/service any client can identify the server/service HTTP = 80, FTP = 21, Telnet = 23,... HTTP = 80, FTP = 21, Telnet = 23,... /etc/service defines well-known ports /etc/service defines well-known ports –Clients usually use dynamic ports assigned by the kernel at run time assigned by the kernel at run time TCP/UDP IP Ethernet Adapter NTP daemon Web server port 123 port 80

9 TCP IP Ethernet Adapter Web Server TCP Server Port 80 What does a server need to do so that a client can connect to it? What does a server need to do so that a client can connect to it?

10 Socket I/O: socket() Since web traffic uses TCP, the web server must create a socket of type SOCK_STREAM Since web traffic uses TCP, the web server must create a socket of type SOCK_STREAM int fd;/* socket descriptor */ if((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror(“socket”); exit(1); } socket returns an integer (socket descriptor) socket returns an integer (socket descriptor) –fd < 0 indicates that an error occurred AF_INET associates a socket with the Internet protocol family AF_INET associates a socket with the Internet protocol family SOCK_STREAM selects the TCP protocol SOCK_STREAM selects the TCP protocol

11 #include /* Internet address structure */ struct in_addr { u_long s_addr;/* 32-bit IPv4 address */ };/* network byte ordered */ /* Socket address, Internet style. */ struct sockaddr_in { u_char sin_family;/* Address Family */ u_short sin_port;/* UDP or TCP Port# */ /* network byte ordered */ struct in_addr sin_addr; /* Internet Address */ char sin_zero[8];/* unused */ }; Internet Addressing Data Structure

12 Byte Ordering Big Endian Big Endian –Sun Solaris, PowerPC,... Little Endian Little Endian –i386, alpha,... Network byte order = Big Endian Network byte order = Big Endian union { u_int32_t addr; /* 4 bytes address */ char c[4]; } un; /* */ un.addr = 0x8002c25f; /* c[0] = ? */ c[0] c[1] c[2] c[3]

13 Byte Ordering Functions Converts between host byte order and network byte order Converts between host byte order and network byte order –‘h’ = host byte order –‘n’ = network byte order –‘l’ = long (4 bytes), converts IP addresses –‘s’ = short (2 bytes), converts port numbers #include unsigned long int htonl(unsigned long int hostlong); unsigned short int htons(unsigned short int hostshort); unsigned long int ntohl(unsigned long int netlong); unsigned short int ntohs(unsigned short int netshort);

14 struct sockaddr_in srv; srv.sin_addr.s_addr = inet_addr(“ ”); if(srv.sin_addr.s_addr == (in_addr_t) -1) { fprintf(stderr, "inet_addr failed!\n"); exit(1); } Converting a numerical address to a string: Dealing with IP Addresses IP Addresses are commonly written as strings (“ ”), but programs deal with IP addresses as integers. IP Addresses are commonly written as strings (“ ”), but programs deal with IP addresses as integers. struct sockaddr_in srv; char *t = inet_ntoa(srv.sin_addr); if(t == 0) { fprintf(stderr, “inet_ntoa failed!\n”); exit(1); } Converting strings to numerical address:

15 Socket I/O: bind() A socket can be bound to a port int fd;/* socket descriptor */ struct sockaddr_in srv;/* used by bind() */ /* create the socket */ srv.sin_family = AF_INET; /* use the Internet addr family */ srv.sin_port = htons(80); /* bind socket ‘fd’ to port 80*/ /* bind: a client may connect to any of my addresses */ srv.sin_addr.s_addr = htonl(INADDR_ANY); if(bind(fd, (struct sockaddr*) &srv, sizeof(srv)) < 0) { perror("bind"); exit(1); } Still not quite ready to communicate with a client... Still not quite ready to communicate with a client...

16 Socket I/O: listen() listen indicates that the server will accept a connection listen indicates that the server will accept a connection int fd; /* socket descriptor */ struct sockaddr_in srv; /* used by bind() */ /* 1) create the socket */ /* 2) bind the socket to a port */ if(listen(fd, 5) < 0) { perror(“listen”); exit(1); } Still not quite ready to communicate with a client... Still not quite ready to communicate with a client... listen returns 0 to indicate success -1 to indicate error

17 Socket I/O: accept() accept blocks waiting for a connection 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() */ /* 1) create the socket */ /* 2) bind the socket to a port */ /* 3) listen on the socket */ newfd = accept(fd, (struct sockaddr*) &cli, &cli_len); if(newfd < 0) { perror("accept");exit(1); } accept returns a new socket (newfd) with the same properties as the original socket (fd)accept returns a new socket (newfd) with the same properties as the original socket (fd) newfd < 0 indicates that an error occurrednewfd < 0 indicates that an error occurred

18 Socket I/O: accept() continued... struct sockaddr_in cli;/* used by accept() */ int newfd;/* returned by accept() */ int cli_len = sizeof(cli);/* used by accept() */ newfd = accept(fd, (struct sockaddr*) &cli, &cli_len); if(newfd < 0) { perror("accept"); exit(1); } How does the server know which client it is? How does the server know which client it is? –cli.sin_addr.s_addr contains the client’s IP address –cli.sin_port contains the client’s port number Now the server can exchange data with the client by using read and write on the descriptor newfd. Now the server can exchange data with the client by using read and write on the descriptor newfd. Why does accept need to return a new descriptor? Why does accept need to return a new descriptor?

19 TCP IP Ethernet Adapter 2 Web Clients TCP Client ports How does a client connect How does a client connect to a server? to a server?

20 Socket I/O: connect() connect is used by the client instead of socket(), bind(), listen(), accept(). It allows a client to connect to a server... connect is used by the client instead of socket(), bind(), listen(), accept(). It allows a client to connect to a server... int fd;/* socket descriptor */ struct sockaddr_in srv;/* used by connect() */ /* create the socket */ /* connect: use the Internet address family */ srv.sin_family = AF_INET; /* connect: socket ‘fd’ to port 80 */ srv.sin_port = htons(80); /* connect: connect to IP Address “ ” */ srv.sin_addr.s_addr = inet_addr(“ ”); if(connect(fd, (struct sockaddr*) &srv, sizeof(srv)) < 0) { perror(”connect"); exit(1); }

21 Socket I/O: write() write can be used with a socket int fd;/* socket descriptor */ struct sockaddr_in srv;/* used by connect() */ char buf[512];/* used by write() */ int nbytes;/* used by write() */ /* … */ /* Example: A client could “write” a request to a server */ if((nbytes = write(fd, buf, sizeof(buf))) < 0) { perror(“write”); exit(1); }

22 Socket I/O: read() read can be used with a socket read can be used with a socket read blocks waiting for data read blocks waiting for data 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; /* used by accept() */ char buf[512];/* used by read() */ int nbytes; /* used by read() */ /* … */ if((nbytes = read(newfd, buf, sizeof(buf))) < 0) { perror(“read”); exit(1); }

23 Review: TCP Client-Server Interaction socket() bind() listen() accept() write() read() TCP Server close() socket() TCP Client connect() write() read() close() connection establishment data request data reply end-of-file notification from UNIX Network Programming Volume 1, figure 4.1