EECS122 Communications Networks Socket Programming. Jörn Altmann

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

ICT 6544 Distributed Systems Lecture 5

Socket Programming for TCP and UDP

CS321: Computer Networks Introduction to Application Layer

A Client-Server Exchange

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

UNIX Network Programming. Overview of Socket API Network Programming Basics

CS321: Computer Networks Socket Programming

ECE 435 Network Engineering Lecture 2

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

Tutorial on Socket Programming

CLIENT-SIDE PROGRAMMING

The User Datagram Protocol

ELEC / COMP 177 Fall Some slides from Kurose and Ross, Computer Networking, 5 th Edition

Socket Programming. Sungkyunkwan University. Hyunseung Choo Copyright Networking Laboratory

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

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

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

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

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

SOCKETS. COMP750 Distributed Systems

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

Sockets 15H2. Inshik Song

Interprocess Communication Mechanisms

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

UNIX Sockets. COS 461 Precept 1

Ports under 1024 are often considered special, and usually require special OS privileges to use.

Elementary TCP Sockets

ECE 435 Network Engineering Lecture 2

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

Socket Programming. Dr. -Ing. Abdalkarim Awad. Informatik 7 Rechnernetze und Kommunikationssysteme

Linux Programming

Interprocess Communication Mechanisms

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

Client software design

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

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

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

Lecture 2. Outline. Layering and Protocols. Network Architecture. Layering and Protocols. Layering and Protocols. Chapter 1 - Foundation

CS4700/CS5700 Fundamentals of Computer Networking

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

CS 640: Computer Networking

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

Interprocess Communication Mechanisms

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

Introduction to Socket Programming

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

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

Interprocess Communication

Programming with TCP/IP. Ram Dantu

Socket Programming. #In the name of Allah. Computer Engineering Department Sharif University of Technology CE443- Computer Networks

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

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

Network Software Implementations

Memory-Mapped Files. generic interface: vaddr mmap(file descriptor,fileoffset,length) munmap(vaddr,length)

UNIT 1 TCP/IP PROGRAMMING CONCEPTS

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

Department of Computer Science

Lab 0. Yvan Petillot. Networks - Lab 0 1

Socket Programming 2007/03/28

Network Implementation

CS 3516: Computer Networks

Computer Networks Prof. Ashok K. Agrawala

CSMC 412. Computer Networks Prof. Ashok K Agrawala Ashok Agrawala Set 2. September 15 CMSC417 Set 2 1

Randall Stewart, Cisco Systems Phill Conrad, University of Delaware

CSE/EE 461 Lecture 14. Connections. Last Time. This Time. We began on the Transport layer. Focus How do we send information reliably?

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

Networked Applications: Sockets. End System: Computer on the Net

UDP CONNECT TO A SERVER

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

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

Ch 7. Network Interface

ECE 650 Systems Programming & Engineering. Spring 2018

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

CS4514 B08 HELP Session 1

Sockets Sockets Communication domains

Computer Network Programming

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

Overview. Exercise 0: Implementing a Client. Setup and Preparation

Programming Ethernet with Socket API. Hui Chen, Ph.D. Dept. of Engineering & Computer Science Virginia State University Petersburg, VA 23806

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

Networked Applications: Sockets. Goals of Todayʼs Lecture. End System: Computer on the ʻNet. Client-server paradigm End systems Clients and servers

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

Application Programming Interfaces

Transport Layer (TCP/UDP)

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

1.2 The first Internet (i.e., one of the first packet switched networks) was referred to as the ARPANET.

Socket Programming TCP UDP

CSE 461 Connections. David Wetherall

Group-A Assignment No. 6

Socket Programming. Omer Ozarslan

The Transmission Control Protocol (TCP)

CSc 450/550 Computer Networks Network Architectures & Client-Server Model

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Announcements. CS 5565 Network Architecture and Protocols. Queuing. Demultiplexing. Demultiplexing Issues (1) Demultiplexing Issues (2)

Network Programming in C. Networked Systems 3 Laboratory Sessions and Problem Sets

Programming Ethernet with Socket API. Hui Chen, Ph.D. Dept. of Engineering & Computer Science Virginia State University Petersburg, VA 23806

CptS 360 (System Programming) Unit 17: Network IPC (Sockets)

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

Internet Connectivity with

Transcription:

EECS122 Communications Networks Socket Programming Jörn Altmann

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? IP Network Client Server EECS122 - UCB 2

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 - UCB 3

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

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

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

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 - UCB 7

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 - UCB 8

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 - UCB 9

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 - UCB 10

Network Programming Tips Byte Ordering Naming Addressing EECS122 - UCB 11

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

Byte Ordering Problem Question: What would happen if two computers with different integer byte ordering communicate? 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: Message is: [Hello,1] Message is: [Hello,512] Message in Memory of little-endian Computer Processing 48 45 4C 4C 6F 00 01 Message is sent Message in Memory of of big-endian Computer Processing across Network 48 45 4C 4C 6F 00 01 EECS122 - UCB 13

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 - UCB 14

Network Programming Tips Byte Ordering Naming Addressing EECS122 - UCB 15

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

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

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

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 - UCB 19

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 - UCB 20

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 - UCB 21

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 - UCB 22

Receiving Packets: Separating Data in a Stream Fixed length record Fixed length record Variable length record Variable length record A B C3 C2 0 1 2 3 4 5 6 7 8 9 Use records (data structures) to partition the data stream Variable length record D 5 0 6 7 8 Variable length record D 0 9 EECS122 - UCB 23

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 - UCB 24

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 - UCB 25

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 - UCB 26

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

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 - UCB 28

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