EE 122: Sockets. Motivation. Sockets. Types of Sockets. Kevin Lai September 11, 2002

Similar documents
EECS122 Communications Networks Socket Programming. Jörn Altmann

ICT 6544 Distributed Systems Lecture 5

CS321: Computer Networks Introduction to Application Layer

ECE 435 Network Engineering Lecture 2

Client software design

Chapter 6. The Transport Layer. Transport Layer 3-1

CS321: Computer Networks Socket Programming

ECE 435 Network Engineering Lecture 2

Interprocess Communication Mechanisms

Socket Programming for TCP and UDP

Lecture 7. Followup. Review. Communication Interface. Socket Communication. Client-Server Model. Socket Programming January 28, 2005

The User Datagram Protocol

Interprocess Communication Mechanisms

CLIENT-SIDE PROGRAMMING

Sockets. Dong-kun Shin Embedded Software Laboratory Sungkyunkwan University Embedded Software Lab.

Linux Programming

Session NM056. Programming TCP/IP with Sockets. Geoff Bryant Process software

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

CSE 43: Computer Networks Structure, Threading, and Blocking. Kevin Webb Swarthmore College September 14, 2017

CS4700/CS5700 Fundamentals of Computer Networking

Today: VM wrap-up Select (if time) Course wrap-up Final Evaluations

Lab 0. Yvan Petillot. Networks - Lab 0 1

Embedded System Design

UNIX Network Programming. Overview of Socket API Network Programming Basics

A Client-Server Exchange

Socket Programming. Sungkyunkwan University. Hyunseung Choo Copyright Networking Laboratory

Randall Stewart, Cisco Systems Phill Conrad, University of Delaware

Appendix B: NET_CON Source Code

Simple network applications using sockets (BSD and WinSock) Revision 1 Copyright Clifford Slocombe

Outline. Distributed Computer Systems. Socket Basics An end-point for a IP network connection. Ports. Sockets and the OS. Transport Layer.

Outline. Operating Systems. Socket Basics An end-point for a IP network connection. Ports. Network Communication. Sockets and the OS

CISC2200 Threads Spring 2015

CSC209H Lecture 10. Dan Zingaro. March 18, 2015

Context. Distributed Systems: Sockets Programming. Alberto Bosio, Associate Professor UM Microelectronic Departement

Chapter 8: I/O functions & socket options

Outline. Distributed Computing Systems. Socket Basics (1 of 2) Socket Basics (2 of 2) 3/28/2014

CompSci 356: Computer Network Architectures Lecture 3: Hardware and physical links References: Chap 1.4, 1.5 of [PD] Xiaowei Yang

Systems software design NETWORK COMMUNICATIONS & RPC SYSTEMS

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

Client-server model The course that gives CMU its Zip! Network programming Nov 27, Using ports to identify services.

Sockets 15H2. Inshik Song

CS 43: Computer Networks. 05: Socket Programming September 12-14, 2018

The BSD UNIX Socket Interface (CS 640 Lecture) Assignment 1. Interprocess Communication (IPC) Work Individually (no groups)

Topics for this Week

Oral. Total. Dated Sign (2) (5) (3) (2)

Christian Tschudin (basierend auf einem Foliensatz von C. Jelger und T. Meyer) Departement Mathematik und Informatik, Universität Basel

G Honors Operating Systems

CS 640: Computer Networking

CMPSC 311- Introduction to Systems Programming Module: Network Programming

Application Programming Interfaces

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

CS307 Operating Systems Processes

Processes. Process Concept. The Process. The Process (Cont.) Process Control Block (PCB) Process State

Network Programming in C: The Berkeley Sockets API. Networked Systems 3 Laboratory Sessions

SOCKETS. COMP750 Distributed Systems

Tutorial on Socket Programming

Introduction and Overview Socket Programming Higher-level interfaces Final thoughts. Network Programming. Samuli Sorvakko/Nixu Oy

CS 43: Computer Networks. 07: Concurrency and Non-blocking I/O Sep 17, 2018

ECE322 Systems Programming Project 2: Networking with Matrix Multiplication in C Grant Kimes 12/16/15

Introduction and Overview Socket Programming Higher-level interfaces Final thoughts. Network Programming. Samuli Sorvakko/Nixu Oy

Network Software Implementations

Εργαστήριο 9 I/O Multiplexing

Department of Computer Science

Types (Protocols) Associated functions Styles We will look at using sockets in C Java sockets are conceptually quite similar

Introduction to Socket Programming

Hybrid of client-server and P2P. Pure P2P Architecture. App-layer Protocols. Communicating Processes. Transport Service Requirements

NETWORK PROGRAMMING. Instructor: Junaid Tariq, Lecturer, Department of Computer Science

Asynchronous Events on Linux

Introduction and Overview Socket Programming Lower-level stuff Higher-level interfaces Security. Network Programming. Samuli Sorvakko/Nixu Oy

CSCE 463/612 Networks and Distributed Processing Spring 2017

Internet protocol stack. Internetworking II: Network programming. April 20, UDP vs TCP. Berkeley Sockets Interface.

UDP CONNECT TO A SERVER

Internetworking II: Network programming. April 20, 2000

SOCKET PROGRAMMING. What is a socket? Using sockets Types (Protocols) Associated functions Styles

Network Programming Worksheet 2. Simple TCP Clients and Servers on *nix with C.

Introduction to Client-Server Model

MSc Integrated Electronics Networks Assignment. Investigation of TCP/IP Sockets and Ports. Gavin Cameron

Computer Network Programming

CMPSC 311- Introduction to Systems Programming Module: Network Programming

CS118 Discussion 1B, Week 1. Taqi Raza BUNCHE 1209B, Fridays 12:00pm to 1:50pm

PA #2 Reviews. set_name, get_name, del_name. Questions? Will be modified after PA #4 ~

Linux Network Programming, Part 1

CS 3516: Computer Networks

Socket Programming. CSIS0234A Computer and Communication Networks. Socket Programming in C

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

UNIX Sockets. Developed for the Azera Group By: Joseph D. Fournier B.Sc.E.E., M.Sc.E.E.

Processes communicating. Network Communication. Sockets. Addressing processes 4/15/2013

Assignment 2 Group 5 Simon Gerber Systems Group Dept. Computer Science ETH Zurich - Switzerland

CompSci 356: Computer Network Architectures. Lecture 3: Network Architecture Examples and Lab 1. Xiaowei Yang

Socket Programming 2007/03/28

Applications and Layered Architectures. Chapter 2 Communication Networks Leon-Garcia, Widjaja

Distributed Systems. 02. Networking. Paul Krzyzanowski. Rutgers University. Fall 2017

// socket for establishing connections

Interprocess Communication Mechanisms

shared storage These mechanisms have already been covered. examples: shared virtual memory message based signals

IP Addresses, DNS. CSE 333 Spring Instructor: Justin Hsia

Interprocess Communication

UNIT 1 TCP/IP PROGRAMMING CONCEPTS

Problem 1: Concepts (Please be concise)

Assignment 1: tcpbridge

Transcription:

Motivation EE 122: Sockets Kevin Lai September 11, 2002 Applications need Application Programming Interface (API) to use the network API: set of function types and data structures and constants Desirable characteristics - Standardized allows programmer to learn once, write anywhere - Flexible support multiple protocols - Simple to use - Complete allows program to use all functionality of a protocol protocols and APIs usually evolve in parallel laik@cs.berkeley.edu 2 Sockets Types of Sockets Berkeley sockets is the most popular network API - runs on Linux, FreeBSD, OS X, Windows - fed/fed off of popularity of TCP/IP Supports TCP/IP, UNIX interprocess communication Similar to UNIX file I/O API Based on C, single threaded model - does not require multiple threads Can build higher-level interfaces on top of sockets - e.g., Remote Procedure Call (RPC) Different types of sockets implements different service models - Stream v.s. datagram Stream socket - connection-oriented - reliable, in order delivery - e.g., ssh, http Datagram socket - connectionless - best-effort delivery, possibly lower delay - e.g., IP Telephony laik@cs.berkeley.edu 3 laik@cs.berkeley.edu 4 1

Chat Client Naming and Addressing create stream socket connect to server while still connected: - if user types data, send to server - if receive data from server, print IP Address - identifies a single host - 32 bits (not a number!) - written as dotted octets e.g., 0x0a000001 is 10.0.0.1 Host name - identifies a single host - variable length string - maps to one or more IP address e.g., www.berkeley.edu Port number - identifies an application on a host - 16 bit number laik@cs.berkeley.edu 5 laik@cs.berkeley.edu 6 Presentation Byte Ordering Solution Different CPU architectures have different byte ordering - why? Many errors for novice network programmers littleendian increasing memory addresses address A +1 address A high-order byte low-order byte 16-bit value uint16_t htons(uint16_t host16bitvalue); uint32_t htonl(uint32_t host32bitvalue); uint16_t ntohs(uint16_t net16bitvalue); uint32_t ntohs(uint32_t net32bitvalue); Use for all integers sent across network - including port numbers, but not IP addresses Floating point numbers - no widely used standard bigendian low-order byte high-order byte laik@cs.berkeley.edu 7 laik@cs.berkeley.edu 8 2

Initializing (1) Initializing (2) allocate socket int chat_sock = socket(af_inet, SOCK_STREAM, IPPROTO_TCP); int chat_sock; if ((chat_sock = socket(af_inet, SOCK_STREAM, IPPROTO_TCP)) < 0) { perror("socket"); printf("failed to create socket\n"); Why would socket() fail? Handling errors that occur rarely usually consumes most of systems code - exceptions (e.g., in java) helps this a somewhat laik@cs.berkeley.edu 9 laik@cs.berkeley.edu 10 Connecting (1) Connecting (2) struct sockaddr_in sin; struct hostent *host = gethostbyname (argv[1]); unsigned int server_addr = *(unsigned long *) host- >h_addr_list[0]; unsigned short server_port = atoi (argv[2]); sin.sin_family = AF_INET; sin.sin_addr.s_addr = htonl(server_addr); sin.sin_port = server_port; connect(chat_sock, (struct sockaddr *) &sin, sizeof(&sin)); struct sockaddr_in sin; struct hostent *host = gethostbyname (argv[1]); unsigned int server_addr = *(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_addr; 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(); laik@cs.berkeley.edu 11 laik@cs.berkeley.edu 12 3

Separating Data in a Stream Receiving Packets (1) Fixed length Record Fixed length int received; char buffer[record_len]; A B C 3 C 2 received = recv(chat_sock, buffer, RECORD_LEN, 0); process_packet(buffer, received); 0 1 2 3 4 5 6 7 8 9 Use s to partition Tradeoffs of two variable schemes? C 0 C 0 5 6 7 8 9 laik@cs.berkeley.edu 13 laik@cs.berkeley.edu 14 Receiving Packets (2) I/O Multiplexing (1) 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 ("Read in read_client"); printf("recv in %s\n", FUNCTION ); 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; while (1) { if (receive_packets(buffer, buffer_len, &bytes_read)!= 0) { if (read_user(user_buffer, user_buffer_len, &user_bytes_read)!= 0) { laik@cs.berkeley.edu 15 laik@cs.berkeley.edu 16 4

I/O Multiplexing (2): Non-blocking I/O Multiplexing using select() int opts = fcntl (chat_sock, F_GETFL); if (opts < 0) { perror ("fcntl(f_getfl)"); opts = (opts O_NONBLOCK); if (fcntl (chat_sock, F_SETFL, opts) < 0) { perror ("fcntl(f_setfl)"); while (1) { if (receive_packets(buffer, buffer_len, &bytes_read)!= 0) { if (read_user(user_buffer, user_buffer_len, &user_bytes_read)!= 0) { select() - wait on multiple file descriptors/sockets and timeout - application does not consume CPU cycles while waiting - return when file descriptors/sockets are ready to be read or written or they have an error, or timeout exceeded advantages - simple - more efficient than polling disadvantages - does not scale to large number of file descriptors/sockets - more awkward to use than it needs to be laik@cs.berkeley.edu 17 laik@cs.berkeley.edu 18 I/O Multiplexing (3): select() Other I/O Models // already set descriptors non-blocking fd_set read_set; while (1) { Signal driven FD_ZERO (read_set); - application notified when I/O operation can be initiated FD_SET (stdin, read_set); FD_SET (chat_sock, read_set); - achieves similar CPU efficiency as select() select_retval = select(max(stdin, chat_sock) + 1, &read_set, NULL, NULL, &time_out); Asynchronous if (select_retval < 0) { - application notified when I/O operation is completed perror ("select"); - can achieve higher CPU efficiency than select()/signals on architectures that have DMA and available system bus if (select_retval > 0) { bandwidth if (FD_ISSET(chat_sock, read_set)) { if (receive_packets(buffer, buffer_len, &bytes_read)!= 0) { - mainly useful for very high bandwidth I/O Both add significant complexity relative to select() if (FD_ISSET(stdin, read_set)) { if (read_user(user_buffer, user_buffer_len, - must use locks to deal with being interrupted at arbitrary code &user_bytes_read)!= 0) { locations - sample complexity cost as threads laik@cs.berkeley.edu 19 laik@cs.berkeley.edu 20 5

Chat Server Summary create stream socket while 1: - if user connects, add to list of users - if receive data from client, send to all other clients Major sources of error for network programmers using sockets: - byte ordering - separating s in streams - using select() - misinterpreting the specification (not covered here) laik@cs.berkeley.edu 21 laik@cs.berkeley.edu 22 6