EECS122 Communications Networks Socket Programming January 30th, 2003 Jörn Altmann.

Slides:



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

Chapter 17 Networking Patricia Roy Manatee Community College, Venice, FL ©2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E William.
© Janice Regan, CMPT 128, CMPT 371 Data Communications and Networking Socket Programming 0.
Socket Programming in C Slides Adapted on Jörn Altmann‘s Slides.
Computer Networks Sockets. Sockets and the OS F An end-point for Internet connection –What the application “plugs into” –OS provides Application Programming.
Quick Overview. 2 ISO/OSI Reference Model Application Application Presentation Presentation Session Session Transport Transport Network Network Data Link.
Socket Programming.
EECS 122 Communications Networks Department of Electrical Engineering and Computer Sciences University of California Berkeley Slides: K. Fall, K. Lai,
EECS122 Communications Networks Socket Programming January 30th, 2003 Jörn Altmann.
1 Socket Interfaces Professor Jinhua Guo CIS527 Fall 2003.
Katz, Stoica F04 EECS 122: Introduction to Computer Networks Sockets Programming Computer Science Division Department of Electrical Engineering and Computer.
Programming with Berkeley Sockets Presented by Chris GauthierDickey Written by Daniel Stutzbach (I think!) for CIS 432/532 Useful References: ● man pages.
CS3771 Today: network programming with sockets  Previous class: network structures, protocols  Next: network programming Sockets (low-level API) TODAY!
1 © 2003, Cisco Systems, Inc. All rights reserved. CCNA 1 v3.0 Module 11 TCP/IP Transport and Application Layers.
McGraw-Hill©The McGraw-Hill Companies, Inc., 2004 Application Layer PART VI.
Client Server Model The client machine (or the client process) makes the request for some resource or service, and the server machine (the server process)
Introduction to Project 1 Web Client and Server Jan 2006.
Gursharan Singh Tatla Transport Layer 16-May
IP Multiplexing Ying Zhang EECS 489 W07.
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.
Process-to-Process Delivery:
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.
TCP/IP protocols Communication over Internet is mostly TCP/IP (Transmission Control Protocol over Internet Protocol) TCP/IP "stack" is software which allows.
Chapter 17 Networking Dave Bremer Otago Polytechnic, N.Z. ©2008, Prentice Hall Operating Systems: Internals and Design Principles, 6/E William Stallings.
9/12/2015B.R1 Socket Abstraction and Interprocess Communication B.Ramamurthy CSE421.
What is a Protocol A set of definitions and rules defining the method by which data is transferred between two or more entities or systems. The key elements.
CCNA 1 v3.0 Module 11 TCP/IP Transport and Application Layers.
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.
ICOM 6115©Manuel Rodriguez-Martinez ICOM 6115 – Computer Networks and the WWW Manuel Rodriguez-Martinez, Ph.D. Lecture 26.
Dr. John P. Abraham Professor University of Texas Pan American Internet Applications and Network Programming.
Chapter 2 Applications and Layered Architectures Sockets.
The Socket Interface Chapter 21. Application Program Interface (API) Interface used between application programs and TCP/IP protocols Interface used between.
The Socket Interface Chapter 22. Introduction This chapter reviews one example of an Application Program Interface (API) which is the interface between.
1 COMP/ELEC 429/556 Introduction to Computer Networks Creating a Network Application Some slides used with permissions from Edward W. Knightly, T. S. Eugene.
Overview of TCP/IP protocols –Application layer (telnet, ssh, http, ftp, etc) The things that we use daily. –Transport layer (TCP, UDP) Allows processes.
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.
UNIX Sockets COS 461 Precept 1. Socket and Process Communication The interface that the OS provides to its networking subsystem application layer transport.
1 Socket Programming EE 122: Intro to Communication Networks Vern Paxson TAs: Lisa Fowler, Daniel Killebrew, Jorge Ortiz Materials with thanks to Sukun.
Socket Programming Tutorial Department of Computer Science Southern Illinois University Edwardsville Fall, 2015 Dr. Hiroshi Fujinoki
Socket Programming Lab 1 1CS Computer Networks.
Socket Programming Introduction. Socket Definition A network socket is one endpoint in a two-way communication flow between two programs running over.
Socket Programming.
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,
Advanced UNIX programming Fall 2002, lecture 16 Instructor: Ashok Srinivasan Acknowledgements: The syllabus and power point presentations are modified.
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.
McGraw-Hill Chapter 23 Process-to-Process Delivery: UDP, TCP Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
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.
Client-server communication Prof. Wenwen Li School of Geographical Sciences and Urban Planning 5644 Coor Hall
Socket Programming in C CS587x Lecture 3 Department of Computer Science Iowa State University.
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.
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.
The Transport Layer Implementation Services Functions Protocols
UNIX Sockets COS 461 Precept 1.
Socket Programming EE 122: Intro to Communication Networks Vern Paxson
Review: TCP Client-Server Interaction
Transport layer API: Socket Programming
Process-to-Process Delivery:
EE 122: Sockets Kevin Lai September 11, 2002.
Sockets Programming Socket to me!.
Sockets Programming Socket to me!.
Process-to-Process Delivery: UDP, TCP
Presentation transcript:

EECS122 Communications Networks Socket Programming January 30th, 2003 Jörn Altmann

EECS122 - UCB2 Questions that will be Addressed During the Lecture What mechanisms are available for a programmer who writes network applications? How to write a network application that sends packets between hosts (client and server) across an IP network? Client Server IP Network

EECS122 - UCB3 EE122 Projects – S03 First assignment: – Implement the client side of an client-server architecture (C programming) – Handle exceptions in the communication – Measure performance of the network and the server

EECS122 - UCB4 Socket Programming Table of Contents 1. Network Application Programming Interface: Sockets and Internet Sockets Network Application Programming Interface 2. Network Programming Tips Network Programming Tips 3. Client-Server Architecture Client-Server Architecture 4. Example: Client Programming Example: Client Programming 5. Example: Server Programming Example: Server Programming 6. Network Programmer’s Mistakes Network Programmer’s Mistakes

EECS122 - UCB5 Layers of the IP Protocol Suite Link Layer Transport Layer Network Layer Application Layer Link Layer Transport Layer Network Layer Application Layer Ethernet e.g. ftp e.g. TCP, UDP e.g. IP

EECS122 - UCB6 Protocol Suite Location Internet Protocol Layer Link Layer Transport Layer (TCP, UDP) Network Layer (IP) Application Layer Network Card & Device Driver (e.g. Ethernet card) Operating System (e.g. Unix) Applications (e.g. browser, game, ftp) Application Programming Interface (API) (e.g. network API) Interface to the Network Card Location

EECS122 - UCB7 Network API Operating system provides Application Programming Interface (API) for network application API is defined by a set of function types, data structures, and constants Desirable characteristics of the network interface Simple to use Flexible  independent from any application  allows program to use all functionality of the network Standardized  allows programmer to learn once, write anywhere Application Programming Interface for networks is called socket

EECS122 - UCB8 Sockets Sockets provide mechanisms to communicate between computers across a network There are different kind of sockets DARPA Internet addresses (Internet Sockets) Unix interprocess communication (Unix Sockets) CCITT X.25 addresses and many others Berkeley sockets is the most popular Internet Socket runs on Linux, FreeBSD, OS X, Windows fed by the popularity of TCP/IP

EECS122 - UCB9 Internet Sockets Support stream and datagram packets (e.g. TCP, UDP, IP) Is Similar to UNIX file I/O API ( provides a file descriptor) Based on C, single thread model does not require multiple threads

EECS122 - UCB10 Types of Internet Sockets Different types of sockets implement different communication types (stream vs. datagram) Type of socket: stream socket connection-oriented two way communication reliable (error free), in order delivery can use the Transmission Control Protocol (TCP) e.g. telnet, ssh, http Type of socket: datagram socket connectionless, does not maintain an open connection, each packet is independent can use the User Datagram Protocol (UDP) e.g. IP telephony Other types exist: similar to the one above

EECS122 - UCB11 Network Programming Tips Byte Ordering Naming Addressing

EECS122 - UCB12 Byte Ordering of Integers Different CPU architectures have different byte ordering D3 high-order bytelow-order byte memory address A memory address A +1 Stored at little-endian computer Stored at big-endian computer low-order bytehigh-order byte F2 Integer representation (2 byte)

EECS122 - UCB13 Message in Memory of of big-endian Computer Message is sent across Network C 4C 6F Byte Ordering Problem Question: What would happen if two computers with different integer byte ordering communicate? Message is: [Hello,1] Message is: [Hello,512] Processing C 4C 6F Message in Memory of little-endian Computer Processing Answer:  Nothing if they do not exchange integers!  But: If they exchange integers, they would get the wrong order of bytes, therefore, the wrong value! Example:

EECS122 - UCB14 Byte Ordering Solution There are two solutions if computers with different byte ordering system want to communicate They must know the kind of architecture of the sending computer (bad solution, it has not been implemented) Introduction of a network byte order. The functions are: uint16_t htons(uint16_t host16bitvalue) uint32_t htonl(uint32_t host32bitvalue) uint16_t ntohs(uint16_t net16bitvalue) uint32_t ntohs(uint32_t net32bitvalue) Note: use for all integers (short and long), which are sent across the network Including port numbers But not for IP addresses

EECS122 - UCB15 Network Programming Tips Byte Ordering Naming Addressing

EECS122 - UCB16 Naming and Addressing Host name identifies a single host (see Domain Name System slides) variable length string (e.g. is mapped to one or more IP addresses IP Address 32 bits (not a number!) written as dotted octets (e.g ) Port number identifies an application on a host 16 bit number

EECS122 - UCB17 Client-Server Architecture Client requests service from server Server responds with sending service or error message to client Example: Remote Procedure Call ClientServer request response

EECS122 - UCB18 Simple Client-Server Example ClientServer request response socket() connect() send() recv() close() socket() bind() listen() accept() recv() send() recv() close() Connection establishment Data response Data request End-of-file notification

EECS122 - UCB19 Example: Client Programming Create stream socket ( socket() ) Connect to server ( connect() ) While still connected: send message to server (send() ) receive (recv() ) data from server and process it

EECS122 - UCB20 Initializing Socket Getting the file descriptor int chat_sock; if ((chat_sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) { perror("socket"); printf("Failed to create socket\n"); abort (); } 1.parameter specifies protocol/address family 2.parameter specifies the communication type 3.parameter specifies the protocol

EECS122 - UCB21 Connecting to Server struct sockaddr_in sin; struct hostent *host = gethostbyname (argv[1]); unsigned int server_address = *(unsigned long *) host->h_addr_list[0]; unsigned short server_port = atoi (argv[2]); memset (&sin, 0, sizeof (sin)); sin.sin_family = AF_INET; sin.sin_addr.s_addr = server_address; sin.sin_port = htons (server_port); if (connect(chat_sock, (struct sockaddr *) &sin, sizeof (sin)) < 0) { perror("connect"); printf("Cannot connect to server\n"); abort(); }

EECS122 - UCB22 Sending Packets int send_packets(char *buffer, int buffer_len) { sent_bytes = send(chat_sock, buffer, buffer_len, 0); if (send_bytes < 0) { perror (“send"); } return 0; } Needs socket descriptor, Buffer containing the message, and Length of the message

EECS122 - UCB23 Receiving Packets: Separating Data in a Stream Use records (data structures) to partition the data stream B Fixed length record 013 C Variable length record C2 Variable length record 5 D 9687 D A

EECS122 - UCB24 Receiving Packets int receive_packets(char *buffer, int buffer_len, int *bytes_read) { int left = buffer_len - *bytes_read; received = recv(chat_sock, buffer + *bytes_read, left, 0); if (received < 0) { perror (“recv"); } if (received <= 0) { return close_connection(); } *bytes_read += received; while (*bytes_read > RECORD_LEN) { process_packet(buffer, RECORD_LEN); *bytes_read -= RECORD_LEN; memmove(buffer, buffer + RECORD_LEN, *bytes_read); } return 0; }

EECS122 - UCB25 Example: Server Programming create stream socket ( socket() ) Bind port to socket ( bind() ) Listen for new client ( listen() ) Wait ( select() ) until user connects (accept() ) data arrives from client (recv() ) data has to be send to client (send() ) timeout (select() )

EECS122 - UCB26 I/O Multiplexing using select() waits on multiple file descriptors and timeout returns when any file descriptor is ready to be read or written or indicate an error, or timeout exceeded advantages simple application does not consume CPU cycles while waiting disadvantages does not scale to large number of file descriptors

EECS122 - UCB27 Network Programmer’s Mistakes byte ordering separating records in streams use of select() misinterpreting the project specification not knowing all available system calls

EECS122 - UCB28 There are more System Calls Depends on communication type Datagram sockets use recvfrom() and sendto() for receiving and sending data Closing connection: close(), shutdown() Getting information about a host: gethostbyname()

EECS122 - UCB29 Literature Unix Network Programming, volumes 1 and 2 by W. Richard Stevens. Published by Prentice Hall; ISBNs for volumes 1 and 2: X, Advanced Programming in the Unix Environment by W. Richard Stevens. Published by Addison Wesley. ISBN man pages on a Unix computer

EECS122 - UCB30 The End

EECS122 - UCB31 EECS Syllabus 1. Logistics; Goals; Themes; Outline; Introduction 2. Examples of Network Applications/Design 3. Internet Architecture 4. Socket Programming 5. Network Performance Metrics; ns 6. DNS and WWW 7. Transport Protocols: UDP and TCP 8. Congestion Control and Avoidance 9. Congestion Control and Avoidance(cont) 10. Intradomain Routing: Distance Vector; Link State 11. Interdomain Routing: BGP 12. Switching and Forwarding; Router Architecture 13. Packet Scheduling and Classification 14. Router Support for Congestion Control: RED and FQ 15. Midterm Exam

EECS122 - UCB32 I/O Multiplexing (1) while (1) { if (receive_packets(buffer, buffer_len, &bytes_read) != 0) { break; } if (read_user(user_buffer, user_buffer_len, &user_bytes_read) != 0) { break; }

EECS122 - UCB33 I/O Multiplexing (2): Non- blocking int opts = fcntl (chat_sock, F_GETFL); if (opts < 0) { perror ("fcntl(F_GETFL)"); abort (); } opts = (opts | O_NONBLOCK); if (fcntl (chat_sock, F_SETFL, opts) < 0) { perror ("fcntl(F_SETFL)"); abort (); } while (1) { if (receive_packets(buffer, buffer_len, &bytes_read) != 0) { break; } if (read_user(user_buffer, user_buffer_len, &user_bytes_read) != 0) { break; }

EECS122 - UCB34 I/O Multiplexing (3): select() // already set descriptors non-blocking fd_set read_set; while (1) { FD_ZERO (read_set); FD_SET (stdin, read_set); FD_SET (chat_sock, read_set); select_retval = select(MAX(stdin, chat_sock) + 1, &read_set, NULL, NULL, &time_out); if (select_retval < 0) { perror ("select"); abort (); } if (select_retval > 0) { if (FD_ISSET(chat_sock, read_set)) { if (receive_packets(buffer, buffer_len, &bytes_read) != 0) { break; } if (FD_ISSET(stdin, read_set)) { if (read_user(user_buffer, user_buffer_len, &user_bytes_read) != 0) { break; }

EECS122 - UCB35 Other I/O Models Signal driven application notified when I/O operation can be initiated achieves similar CPU efficiency as select() Asynchronous application notified when I/O operation is completed can achieve higher CPU efficiency than select()/signals on architectures that have DMA and available system bus bandwidth mainly useful for very high bandwidth I/O Both add significant complexity relative to select() must use locks to deal with being interrupted at arbitrary code locations sample complexity cost as threads