Introduction to Computer Systems. Networks 2. c Theodore Norvell. The Sockets API

Similar documents
Elementary TCP Sockets

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

CSE 333 Lecture 16 - network programming intro

Socket Programming. Sungkyunkwan University. Hyunseung Choo Copyright Networking Laboratory

CSE 333 SECTION 6. Networking and sockets

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

CSE 333 Lecture network programming intro

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

Sockets 15H2. Inshik Song

Tutorial on Socket Programming

STUDY OF SOCKET PROGRAMMING

QUIZ: Longest Matching Prefix

Server-side Programming

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

Network Implementation

Server-side Programming

UNIX Sockets. COS 461 Precept 1

A Client-Server Exchange

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

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

CSE 333 SECTION 7. C++ Virtual Functions and Client-Side Network Programming

Lecture 24. Thursday, November 19 CS 375 UNIX System Programming - Lecture 24 1

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

CSE 333 Lecture 8 - file and network I/O

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

Operating Systems. 17. Sockets. Paul Krzyzanowski. Rutgers University. Spring /6/ Paul Krzyzanowski

Motivation of VPN! Overview! VPN addressing and routing! Two basic techniques for VPN! ! How to guarantee privacy of network traffic?!

UNIX System Programming Lecture 19: IP Sockets

Systems software design NETWORK COMMUNICATIONS & RPC SYSTEMS

Sockets Sockets Communication domains

Socket Programming for TCP and UDP

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

C18: Network Fundamentals and Reliable Sockets

CS 326: Operating Systems. Networking. Lecture 17

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

Chapter 2 Applications and

CSE 333 SECTION 8. Sockets, Network Programming

What s an API? Do we need standardization?

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

COMMUNICATION PROTOCOLS: REMOTE PROCEDURE CALL (RPC)

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

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

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

Introduction to Lab 2 and Socket Programming. -Vengatanathan Krishnamoorthi

Chapter 8: I/O functions & socket options

Overview. Last Lecture. This Lecture. Daemon processes and advanced I/O functions

UNIX input and output

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

Unix Network Programming

Computer Networks Prof. Ashok K. Agrawala

Multicast. Note. Sending Multicast Messages

Transport Layer (TCP/UDP)

EECS122 Communications Networks Socket Programming. Jörn Altmann

IP Addresses, DNS. CSE 333 Summer Teaching Assistants: Renshu Gu William Kim Soumya Vasisht

Lecture 5 Overview! Last Lecture! This Lecture! Next Lecture! I/O multiplexing! Source: Chapter 6 of Stevens book!

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

System Programming. Sockets

CSE 333 SECTION 7. Client-Side Network Programming

UNIT 1 TCP/IP PROGRAMMING CONCEPTS

UNIT IV- SOCKETS Part A

Computer Network Lab, SS Fachgebiet Technische Informatik, Joachim Zumbrägel. Overview. Sockets. Sockets in C.

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

Interprocess Communication

CS321: Computer Networks Introduction to Application Layer

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

CSE 124 Discussion Section Sockets Programming 10/10/17

Outline. Option Types. Socket Options SWE 545. Socket Options. Out-of-Band Data. Advanced Socket. Many socket options are Boolean flags

Socket Programming. Omer Ozarslan

Application Architecture

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

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

Network Programming. Introduction to Sockets. Dr. Thaier Hayajneh. Process Layer. Network Layer. Berkeley API

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

Lecture 20 Overview. Last Lecture. This Lecture. Next Lecture. Transport Control Protocol (1) Transport Control Protocol (2) Source: chapters 23, 24

??? Traceroute. app. app. host. host. Apps talk to other apps with no real idea of what is inside the network

UNIX Network Programming. Overview of Socket API Network Programming Basics

SOCKETS. COMP750 Distributed Systems

Introduction to Socket Programming

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

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

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

Sockets and Parallel Computing. CS439: Principles of Computer Systems April 11, 2018

ECE 435 Network Engineering Lecture 2

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

Interprocess Communication Mechanisms

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

Transport Layer. Chapter 3: Transport Layer

Network Programming in Python. based on Chun, chapter 2; plus material on classes

Packet Sniffing and Spoofing

Application Programming Interfaces

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

CSE 333 Lecture server sockets

NETWORK PROGRAMMING AND MANAGEMENT 1 KINGS DEPARTMENT OF INFORMATION TECHNOLOGY QUESTION BANK

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

Figure 1: Graphical representation of a client-server application

CSE 333 Section 8 - Client-Side Networking

Group-A Assignment No. 6

Computer Network Programming

Introduction to Python Network Programming for Network Architects and Engineers

Connections. Topics. Focus. Presentation Session. Application. Data Link. Transport. Physical. Network

Transcription:

The Sockets API [Wait! If you are not familiar with file descriptors and the UNIX read and write system calls, read chapter 10 of Bryant and O Hallaron and/or my summary before going on.] In this section we take a closer look at how to use TCP from C using the UNIX interface. In UNIX and POSIX, TCP is accessed via the Sockets interface. Sockets can be used with a number of networking protocols, not just TCP. The main data structures include. struct sockaddr includes an IP address and a port number struct sockaddrinfo includes asockaddr a family (e.g. IPv4 or IPv6) a socket type (e.g. streaming or datagram) a protocol (e.g. TCP or UDP) file descriptors As with disk files, file descriptors are integers that are obtained from the operating system and that can be used with theread andwrite functions. Making things confusing sockaddr structures look quite different depending on whether IPv4 or IPv6 is Typeset June 11, 2016 1

being used. The calls use pointers of type struct sockaddr, but the actual objects should be stored in a struct sockaddr_storage structure, to ensure there is enough space. Some useful functions in the API include getaddrinfo Does DNS lookup A host name. E.g. "server.black.com" A port number. A set of restrictions. E.g. must be TCP. Output: A list ofsockaddrinfo records It returns a list because a single host name may map to several IP addresses and the host may support both IPv4 and IPv6 and TCP and UDP. socket Creates, but does not open, a socket a family (e.g. IPv4 or IPv6) a socket type (e.g. streaming or datagram) a protocol (e.g. TCP or UDP) Output: a file descriptor for the socket connect Used by the client to create a connection. Typeset June 11, 2016 2

Analogous toopen for the client a file descriptor for a socket the socket address (struct sockaddr) for the service bind Used by the server to connect a server socket to a port number. a file descriptor for a socket the socket address (struct sockaddr) for the service listen Used by the server to initiate listening on a port a file descriptor for a bound socket the number of connection requests that may be queued accept Used by the server to create a new connection Analogous toopen for the server Waits until a connection is requested by a client and the connection is established a file descriptor for a server socket that is listening Output: Typeset June 11, 2016 3

A file descriptor for a new socket. The socket address of the new client. read Waits until there is at least one byte can be read or the connection is closed. Input A file descriptor for a socket. Output A sequence of 0 or more bytes. 0 bytes means the connection was closed. write A file descriptor for a socket. A sequence of bytes to send. close A file descriptor Half closes the connection select Input A set of file descriptors Output A set of file descriptors that can be read from select blocks until at least on file descriptor in the set can be read The output is a subset of the input. Typeset June 11, 2016 4

Calling read on any file descriptor in the output set will not block. Typeset June 11, 2016 5

Some examples The following images illustrate use of the sockets interface in UNIX Legend Solid horizontal arrows represent calls from the application the OS. Dashed horizontal arrows represent returns from those calls. Diagonal solid arrows represent TCP messages. The thin yellow rectangles show activities. For example the execution of a subroutine. Typeset June 11, 2016 6

Establishing a connection Typeset June 11, 2016 7

Buffered communication. It is important to understand that each socket endpoint has two buffers one for sending and one for receiving. read calls transfer data from the read buffer to the application. write calls transfer data from the application to the write buffer. The OSs use TCP to transfer from the write buffer on one host to the read buffer on the other. The follow image shows reading and writing Note that the amount read by each read may not relate to the amount written. In this case, the server sends ni hao with one write, but it takes 2 reads to read everything written. Typeset June 11, 2016 8

Nonblocking writes: Calls to write might not write the entire message. We may have to callwrite many times to completely transfer our message to the write buffer. Typeset June 11, 2016 9

Blocking reads. read will block (i.e. wait) until one of the following happens There is a positive number of bytes it can read. The socket is closed. An error happens. The diagram below shows the first two cases Typeset June 11, 2016 10

Closing a connection Each process closes its endpoint. End of networks-02. Typeset June 11, 2016 11