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

Similar documents
Elementary TCP Sockets

How do we Communicate? Introduction to Unix Network Programming. What does Alice do? What does Bob do? Two simplest networking programs

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

STUDY OF SOCKET PROGRAMMING

Review. Preview. Closing a TCP Connection. Closing a TCP Connection. Port Numbers 11/27/2017. Packet Exchange for TCP Connection

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

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

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

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

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Group-A Assignment No. 6

Sockets 15H2. Inshik Song

sottotitolo Socket Programming Milano, XX mese 20XX A.A. 2016/17 Federico Reghenzani

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

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

Programming with TCP/IP. Ram Dantu

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

CS 3516: Computer Networks

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

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

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

Socket Programming for TCP and UDP

Lecture 3 Overview! Last Lecture! TCP/UDP and Sockets introduction!

CS 640: Computer Networking

Unix Network Programming

CSE 333 Section 8 - Client-Side Networking

TCP: Three-way handshake

A Socket Example. Haris Andrianakis & Angelos Stavrou George Mason University

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

UNIX Sockets. COS 461 Precept 1

Unix Network Programming Chapter 4. Elementary TCP Sockets 광운대학교컴퓨터과학과 정보통신연구실 석사과정안중현

UNIT IV- SOCKETS Part A

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

Tutorial on Socket Programming

CS321: Computer Networks Socket Programming

ECE 435 Network Engineering Lecture 2

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

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

What s an API? Do we need standardization?

Systems software design NETWORK COMMUNICATIONS & RPC SYSTEMS

Network programming(i) Lenuta Alboaie

CS321: Computer Networks Introduction to Application Layer

ECE 435 Network Engineering Lecture 2

CSE 124 Discussion Section Sockets Programming 10/10/17

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

Interprocess Communication

UNIX Network Programming. Overview of Socket API Network Programming Basics

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

A Client-Server Exchange

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

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

EEC-484/584 Computer Networks

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

Introduction for SPI mapping Service Discovery Interoperability Testing. 20, Sep PWG Fumio Nagasaka

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

Computer Networks Prof. Ashok K. Agrawala

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

Socket Programming. Sungkyunkwan University. Hyunseung Choo Copyright Networking Laboratory

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

Project 3. Reliable Data Transfer over UDP. NTU CSIE Computer Networks 2011 Spring

Socket Programming TCP UDP

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

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

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

WinSock. What Is Sockets What Is Windows Sockets What Are Its Benefits Architecture of Windows Sockets Network Application Mechanics

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

Network Socket Programming - 3 BUPT/QMUL

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

Network Communication

CSE 333 SECTION 8. Sockets, Network Programming

System Programming. Sockets

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

Application Programming Interfaces

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

CLIENT-SIDE PROGRAMMING

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

Department of Computer Science

Network Socket Programming - 3 BUPT/QMUL

CS4700/CS5700 Fundamentals of Computer Networking

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

Interprocess Communication Mechanisms

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

Introduction to Socket Programming

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

Introduction to Socket Programming

SOCKETS: INTRODUCTION

Introduction to Client-Server Model

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

Client Server Computing

Client software design

Interprocess Communication Mechanisms

Interprocess Communication Mechanisms

Network Software Implementations

Socket programming in C

Socket Programming(2/2)

CS307 Operating Systems Processes

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

Interprocess Communication Mechanisms

Interprocess Communication Mechanisms

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

Transcription:

NETWORK PROGRAMMING CSC- 341 25 Instructor: Junaid Tariq, Lecturer, Department of Computer Science

26 9 Lecture

Sockets as means for inter-process communication (IPC) application layer Client Process Socket OS network transport layer (TCP/UDP) network layer (IP) stack link layer (e.g. ethernet) physical layer Internet Internet Internet application layer Server Process Socket OS network transport layer (TCP/UDP) network layer (IP) stack link layer (e.g. ethernet) physical layer The interface that the OS provides to its networking subsystem 27

28 SOCKET CREATION

CREATING SOCKETS - SOCKET() int socket (int family, int type, int protocol); Create a socket. Returns file descriptor or -1. Also sets errno on failure. family: address family (namespace) or protocol family AF_INET for IPv4 other possibilities: AF_INET6 (IPv6), AF_UNIX, AF_OSI or AF_LOCAL (Unix socket), AF_ROUTE (routing) type: style of communication SOCK_STREAM for TCP (with AF_INET) SOCK_DGRAM for UDP (with AF_INET) protocol: protocol within family Usually already defined by domain & type, typically 0 (default) 29

NAMING AND IDENTIFYING SOCKETS - BIND() int bind (int sockfd, struct sockaddr* myaddr, int addrlen); Bind a socket to a local IP address and port number. Returns 0 on success, -1 and sets errno on failure. sockfd: socket file descriptor (returned from socket) myaddr: includes IP address and port number IP address: set by kernel if value passed is INADDR_ANY, else set by caller port number: set by kernel if value passed is 0, else set by caller addrlen: length of address structure = sizeof (struct sockaddr_in) 30

TCP AND UDP PORT NAMESPACES allocated and assigned by the Internet Assigned Numbers Authority (IANA) see RFC 1700 ftp://ftp.isi.edu/in-notes/iana/assignments/port-numbers i /i t /i / i t / t 1-512 standard services (see /etc/services); superuser only 513-1023 registered and controlled, also used for identity verification; super-user only 1024-49151 registered services/ephemeral ports 49152-65535 private/ephemeral ports 31

WAITING FOR CONNECTIONS - LISTEN() int listen (int sockfd, int backlog); Put socket into passive state (wait for connections rather than initiate a connection). Returns 0 on success, -1 and sets errno on failure. sockfd : socket file descriptor (returned from socket ) backlog : bound on length of unaccepted connection queue (connection backlog); kernel will cap, thus better to set high 32

CONTACT THE PEER - CONNECT() int connect (int sockfd, struct sockaddr* servaddr, int addrlen); Connect to another socket. Returns 0 on success, -1 and sets errno on failure. sockfd : socket file descriptor (returned from socket ) servaddr : IP address and port number of server addrlen : length of address structure = sizeof (struct sockaddr_in) Can use with UDP to restrict incoming datagrams and to obtain asynchronous errors. 33

WELCOME A CONNECTION - ACCEPT() int accept (int sockfd, struct sockaddr* cliaddr, int* addrlen); Accept a new connection (first one off queue of pending connections). Returns file descriptor or -1. Also sets errno. sockfd : socket file descriptor (returned from socket ) cliaddr : IP address and port number of client (returned from call) addrlen : length of address structure = pointer to int set to sizeof (struct sockaddr_in) addrlen is a value-result argument: the caller passes the size of the address structure, the kernel returns the size of the client s address (the number of bytes written) 34

35 SENDING AND RECEIVING DATA

SEND THE DATA - WRITE() int write (int sockfd, char* buf, size_t nbytes); Write data to a stream (TCP) or connected datagram (UDP) socket. Returns number of bytes written or -1. Also sets errno on failure. sockfd : socket file descriptor (returned from socket ) buf : data buffer nbytes : number of bytes to try to write some reasons for failure or partial writes: process received interrupt or signal kernel resources unavailable (e.g., buffers) 36

RECEIVE THE DATA - READ() int read (int sockfd, char* buf, size_t nbytes); Read data from a stream (TCP) or connected datagram (UDP) socket. Returns number of bytes read or -1. Also sets errno on failure. sockfd : socket file descriptor (returned from socket ) buf : data buffer nbytes : number of bytes to try to read Returns 0 if socket closed. 37

SEND DATA TO SOMEONE - SENDTO() int sendto (int sockfd, char* buf, size_t nbytes, int flags, struct sockaddr* destaddr, int addrlen); Send a datagram to another UDP socket. Returns number of bytes written or -1. Also sets errno on failure. sockfd : socket file descriptor (returned from socket ) buf : data buffer nbytes : number of bytes to try to read flags : see man page for details; typically use 0 destaddr : IP address and port number of destination socket addrlen : length of address structure = sizeof (struct sockaddr_in) 38

RECEIVE DATA FROM SOMEONE - RECVFROM() int recvfrom (int sockfd, char* buf, size_t nbytes, int flags, struct sockaddr* srcaddr, int* addrlen); Read a datagram from a UDP socket. Returns number of bytes read (0 is valid) or -1. Also sets errno on failure. sockfd : socket file descriptor (returned from socket ) buf : data buffer nbytes : number of bytes to try to read flags : see man page for details; typically use 0 srcaddr : IP address and port number of sending socket (returned from call) addrlen : length of address structure = pointer to int set to sizeof (struct sockaddr_in) 39

TEARING DOWN A CONNECTION 40

GOOD BYE - CLOSE() int close (int sockfd); Closes a socket and deletes descriptor from system tables. Returns 0 on success, -1 and sets errno on failure. sockfd : socket file descriptor (returned from socket ) Closes communication on socket in both directions. All data sent before close are delivered to other side (although this aspect can be overridden). After close(), sockfd is not valid for reading or writing. 41

CLOSE IN MY WAY - SHUTDOWN() int shutdown (int sockfd, int howto); Force termination of communication across a socket in one or both directions. Returns 0 on success, -1 and sets errno on failure. sockfd : socket file descriptor (returned from socket ) howto : SHUT_RD to stop reading SHUT_WR to stop writing SHUT_RDWR to stop both 42

EXAMPLE: DATETIME CLIENT-SERVER CHAPTER 1

COMMAND LINE ARGUMENT #include<stdio.h> int main(int argc, char * argv[]) { int i; for(i=0;i<argc;i++) { printf("%s",argv[i]); printf("\n"); } return 0; }