CS 3516: Computer Networks

Similar documents
TCP Echo Application: Client & Server. TA: Awad A Younis Class: CS457 Fall 2014

TCP/IP Sockets in C: Practical Guide for Programmers. Computer Chat. Internet Protocol (IP) IP Address. Transport Protocols. Ports

Network Programming Week #1. K.C. Kim

TCP Network Programming in C

Elementary TCP Sockets

TCP/IP Sockets in C: Practical Guide for Programmers. Computer Chat. Internet Protocol (IP) IP Address. Transport Protocols

Network Socket Programming - 3 BUPT/QMUL

Network Socket Programming - 3 BUPT/QMUL

Writing Network Applications using the TCP/IP Protocol Stack: Socket Programming

CS 640: Computer Networking

COE 4DN Advanced Internet Communications. Basic Sockets in C (with some Java and Python) version 1, 2015

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

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

CS321: Computer Networks Socket Programming

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

CS321: Computer Networks Introduction to Application Layer

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

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

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

Sockets. TCP/IP Sockets in C: Practical Guide for Programmers (we are covering all chapters of Part 1; Part 2 is a good sockets API reference)

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

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

UNIX Network Programming. Overview of Socket API Network Programming Basics

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

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

Programming with TCP/IP. Ram Dantu

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

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

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

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

Tutorial on Socket Programming

System Programming. Sockets

Introduction to Socket Programming

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

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

Unix Network Programming

ECE 435 Network Engineering Lecture 2

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

Lab 0. Yvan Petillot. Networks - Lab 0 1

Network programming(i) Lenuta Alboaie

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

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

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

Socket Programming TCP UDP

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

TCP: Three-way handshake

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

CSE 333 SECTION 8. Sockets, Network Programming

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

Socket Programming(2/2)

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

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

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

ECE 435 Network Engineering Lecture 2

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

Piotr Mielecki Ph. D.

CSE 333 SECTION 7. Client-Side Network Programming

STUDY OF SOCKET PROGRAMMING

Socket Programming for TCP and UDP

CLIENT-SIDE PROGRAMMING

Introduction to Client-Server Model

Group-A Assignment No. 6

CSE 124 Discussion Section Sockets Programming 10/10/17

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

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

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

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

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

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

Application Programming Interfaces

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

CSE 333 SECTION 6. Networking and sockets

UNIT IV- SOCKETS Part A

Network Communication

ICT 6544 Distributed Systems Lecture 5

CSE 333 Lecture 16 - network programming intro

Sockets 15H2. Inshik Song

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

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

The Socket Interface. Socket and Socket Library

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

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

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

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

Socket Programming. Sungkyunkwan University. Hyunseung Choo Copyright Networking Laboratory

How to write a Measurement Telnet Server

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

CSE 333 Lecture network programming intro

Department of Computer Science

CS4700/CS5700 Fundamentals of Computer Networking

SOCKETS. COMP750 Distributed Systems

Interprocess Communication Mechanisms

Internet applications

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

NETWORK AND SYSTEM PROGRAMMING

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

Socket Programming 2007/03/28

CSE 333 SECTION 7. Client-Side Network Programming

Transcription:

Welcome to CS 3516: Prof. Yanhua Li Time: 9:00am 9:50am M, T, R, and F Location: AK219 Fall 2018 A-term 1 Some slides are originally from the course materials of the textbook Computer Networking: A Top Down Approach, 7th edition, by Jim Kurose, Keith Ross, Addison-Wesley March 2016. Copyright 1996-2017 J.F Kurose and K.W. Ross, All Rights Reserved.

So far 1.1 what is the Internet? nuts and bolts view service view 1.2 network edge end systems, access networks, links 1.3 network core packet switching, circuit switching, network structure Reminder 1. Programming project 1 starts today, and is due on 9/11; 2. Lab 1 is due Tue (tomorrow). Introduction 1-2

Socket programming goal: learn how to build client/server applications that communicate using sockets socket: door between application process and endend-transport protocol application process socket application process controlled by app developer transport network link physical Internet transport network link physical controlled by OS Two socket types for two transport services: UDP: unreliable datagram TCP: reliable, byte stream-oriented Application Layer 2-3

IPv4 Socket Address Structure The Internet socket address structure is named sockaddr_in and is defined by including <netinet/in.h> header. struct in_addr { in_addr_t s_addr /* 32-bit IP address */ }; /* network byte ordered */ struct sockaddr_in { uint8_t sin_len; /* length of structure (16) */ sa_family_t sin_family; /* AF_INET */ in_port_t number */ sin_port; /* 16-bit TCP or UDP port /* network byte ordered */ struct in_addr sin_addr; /* 32-bit IPv4 address */ /* network byte ordered */ char sin_zero[8]; /* unused */ }; TCP/IP Sockets 4

Server socket() TCP Socket Calls bind() listen() accept() blocks until server receives a connect request from client connect negotiation Client socket() connect() read() data write() write() close() data read() close() Copyright 2000 The McGraw Hill Companies Leon-Garcia & Widjaja: Communication Networks Figure 2.17 TCP/IP Sockets 5

System Calls for Elementary TCP Sockets #include <sys/types.h> #include <sys/socket.h> socket Function int socket ( int family, int type, int protocol ); family: specifies the protocol family {AF_INET for TCP/IP} type: indicates communications semantics SOCK_STREAM stream socket TCP SOCK_DGRAM datagram socket UDP SOCK_RAW raw socket protocol: set to 0 except for raw sockets returns on success: socket descriptor {a small nonnegative integer} on error: -1 Example: if (( sd = socket (AF_INET, SOCK_STREAM, 0)) < 0) err_sys ( socket call error ); TCP/IP Sockets 6

Server socket() TCP Socket Calls bind() listen() accept() blocks until server receives a connect request from clientconnect negotiation Client socket() connect() read() data write() write() close() data read() close() Copyright 2000 The McGraw Hill Companies Leon-Garcia & Widjaja: Communication Networks Figure 2.17 TCP/IP Sockets 7

connect Function int connect (int sockfd, const struct sockaddr *servaddr, socklen_t addrlen); sockfd: a socket descriptor returned by the socket function *servaddr: a pointer to a socket address structure addrlen: the size of the socket address structure The socket address structure must contain the IP address and the port number for the connection wanted. In TCP connect initiates a three-way handshake. connect returns only when the connection is established or when an error occurs. returns on success: 0 on error: -1 Example: if ( connect (sd, (struct sockaddr *) &servaddr, sizeof (servaddr))!= 0) err_sys( connect call error ); TCP/IP Sockets 8

close Function int close (int sockfd); close marks the socket as closed and returns to the process immediately. sockfd: This socket descriptor is no longer useable. Note TCP will try to send any data already queued to the other end before the normal connection termination sequence. Returns on success: 0 on error: -1 Example: close (sfd); TCP/IP Sockets 9

Server socket() TCP Socket Calls bind() listen() accept() blocks until server receives a connect request from clientconnect negotiation Client socket() connect() read() data write() write() close() data read() close() Copyright 2000 The McGraw Hill Companies Leon-Garcia & Widjaja: Communication Networks Figure 2.17 TCP/IP Sockets 10

bind Function int bind (int sockfd, const struct sockaddr *myaddr, socklen_t addrlen); bind assigns a local protocol address to a socket. protocol address: a 32 bit IPv4 address and a 16 bit TCP or UDP port number. sockfd: a socket descriptor returned by the socket function. *myaddr: a pointer to a protocol-specific address. addrlen: the size of the socket address structure. Servers bind their well-known port when they start. returns on success: 0 on error: -1 Example: if (bind (sd, (struct sockaddr *) &servaddr, sizeof (servaddr))!= 0) errsys ( bind call error ); TCP/IP Sockets 11

Server socket() TCP Socket Calls bind() listen() accept() blocks until server receives a connect request from clientconnect negotiation Client socket() connect() read() data write() write() close() data read() close() Copyright 2000 The McGraw Hill Companies Leon-Garcia & Widjaja: Communication Networks Figure 2.17 TCP/IP Sockets 12

listen Function int listen (int sockfd, int backlog); listen is called only by a TCP server and performs two actions: 1. Converts an unconnected socket (sockfd) into a passive socket. 2. Specifies the maximum number of connections (backlog) that the kernel should queue for this socket. listen is normally called before the accept function. returns on success: 0 on error: -1 Example: if (listen (sd, 2)!= 0) errsys ( listen call error ); TCP/IP Sockets 13

Server socket() TCP Socket Calls bind() listen() accept() blocks until server receives a connect request from clientconnect negotiation Client socket() connect() read() data write() write() close() data read() close() Copyright 2000 The McGraw Hill Companies Leon-Garcia & Widjaja: Communication Networks Figure 2.17 TCP/IP Sockets 14

accept Function int accept (int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen); accept is called by the TCP server to return the next completed connection from the front of the completed connection queue. sockfd: This is the same socket descriptor as in listen call. *cliaddr: used to return the protocol address of the connected peer process (i.e., the client process). *addrlen: {this is a value-result argument} before the accept call: We set the integer value pointed to by *addrlen to the size of the socket address structure pointed to by *cliaddr; on return from the accept call: This integer value contains the actual number of bytes stored in the socket address structure. returns on success: a new socket descriptor on error: -1 TCP/IP Sockets 15

accept Function (cont.) int accept (int sockfd, struct sockaddr *cliaddr, socklen_t *addrlen); For accept the first argument sockfd is the listening socket and the returned value is the connected socket. The server will have one connected socket for each client connection accepted. When the server is finished with a client, the connected socket must be closed. Example: connected socket sfd = accept (sd, NULL, NULL); if (sfd == -1) err_sys ( accept error ); TCP/IP Sockets 16

Server socket() TCP Socket Calls bind() listen() accept() blocks until server receives a connect request from clientconnect negotiation Client socket() connect() read() data write() write() close() data read() close() Copyright 2000 The McGraw Hill Companies Leon-Garcia & Widjaja: Communication Networks Figure 2.17 TCP/IP Sockets 17

Server UDP Socket Calls socket() bind() recvfrom() Client socket() connect() Not needed blocks until server receives data from client data sendto() sendto() data recvfrom() close() close() Copyright 2000 The McGraw Hill Companies Leon-Garcia & Widjaja: Communication Networks Figure 2.18 TCP/IP Sockets 18

Suggestions for Labs/Projects Labs: Office hour Email to cs3516-ta@cs.wpi.edu Discussion forum Socket Programming Projects: Start with the simplest example in the slides Introduction 1-19

Code example Introduction 1-20

TCP Echo Server #include <stdio.h> /* for printf() and fprintf() */ #include <sys/socket.h> /* for socket(), bind(), and connect() */ #include <arpa/inet.h> /* for sockaddr_in and inet_ntoa() */ #include <stdlib.h> /* for atoi() and exit() */ #include <string.h> /* for memset() */ #include <unistd.h> /* for close() */ #define MAXPENDING 5 /* Maximum outstanding connection requests */ void DieWithError(char *errormessage); /* Error handling function */ void HandleTCPClient(int clntsocket); /* TCP client handling function */ TCP/IP Sockets 21 D&C

int main(int argc, char *argv[]) { int servsock; /*Socket descriptor for server */ int clntsock; /* Socket descriptor for client */ struct sockaddr_in echoservaddr; /* Local address */ struct sockaddr_in echoclntaddr; /* Client address */ unsigned short echoservport; /* Server port */ unsigned int clntlen; /* Length of client address data structure */ if (argc!= 2) /* Test for correct number of arguments */ { fprintf(stderr, "Usage: %s <Server Port>\n", argv[0]); } exit(1); echoservport = atoi(argv[1]); /* First arg: local port */ /* Create socket for incoming connections */ if ((servsock = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) DieWithError("socket() failed"); D&C TCP/IP Sockets 22

TCP Echo Server /* Construct local address structure */ memset(&echoservaddr, 0, sizeof(echoservaddr)); /* Zero out structure */ echoservaddr.sin_family = AF_INET; /* Internet address family */ echoservaddr.sin_addr.s_addr = htonl(inaddr_any); /* Any incoming interface */ echoservaddr.sin_port = htons(echoservport); /* Local port */ /* Bind to the local address */ if (bind (servsock, (struct sockaddr *) &echoservaddr, sizeof(echoservaddr))<0) DieWithError("bind() failed"); /* Mark the socket so it will listen for incoming connections */ if (listen (servsock, MAXPENDING) < 0) DieWithError("listen() failed"); TCP/IP Sockets 23 D&C

TCP Echo Server for (;;) /* Run forever */ { /* Set the size of the in-out parameter */ clntlen = sizeof(echoclntaddr); /* Wait for a client to connect */ if ((clntsock = accept (servsock, (struct sockaddr *) &echoclntaddr, &clntlen)) < 0) DieWithError("accept() failed"); /* clntsock is connected to a client! */ printf("handling client %s\n", inet_ntoa(echoclntaddr.sin_addr)); HandleTCPClient(clntSock); } /* NOT REACHED */ } TCP/IP Sockets 24 D&C

TCP Echo Client #include <stdio.h> /* for printf() and fprintf() */ #include <sys/socket.h> /* for socket(), connect(), send(), and recv() */ #include <arpa/inet.h> /* for sockaddr_in and inet_addr() */ #include <stdlib.h> /* for atoi() and exit() */ #include <string.h> /* for memset() */ #include <unistd.h> /* for close() */ #define RCVBUFSIZE 32 /* Size of receive buffer */ void DieWithError(char *errormessage); /* Error handling function */ TCP/IP Sockets 25 D&C

int main(int argc, char *argv[]) { int sock; /* Socket descriptor */ struct sockaddr_in echoservaddr; /* Echo server address */ unsigned short echoservport; /* Echo server port */ char *servip; /* Server IP address (dotted quad) */ char *echostring; /* String to send to echo server */ char echobuffer[rcvbufsize]; /* Buffer for echo string */ unsigned int echostringlen; /* Length of string to echo */ int bytesrcvd, totalbytesrcvd; /* Bytes read in single recv() and total bytes read */ if ((argc < 3) (argc > 4)) /* Test for correct number of arguments */ { fprintf(stderr, "Usage: %s <Server IP> <Echo Word> [<Echo Port>]\n", argv[0]); exit(1); } D&C TCP/IP Sockets 26

servip = argv[1]; /* First arg: server IP address (dotted quad) */ echostring = argv[2]; /* Second arg: string to echo */ if (argc == 4) echoservport = atoi(argv[3]); /* Use given port, if any */ else echoservport = 7; /* 7 is the well-known port for the echo service */ /* Create a reliable, stream socket using TCP */ if ((sock = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) DieWithError("socket() failed"); /* Construct the server address structure */ memset(&echoservaddr, 0, sizeof(echoservaddr)); /* Zero out structure */ echoservaddr.sin_family = AF_INET; /* Internet address family */ echoservaddr.sin_addr.s_addr = inet_addr(servip); /* Server IP address */ echoservaddr.sin_port = htons(echoservport); /* Server port */ TCP/IP Sockets 27 D&C

TCP Echo Client /* Establish the connection to the echo server */ if (connect (sock, (struct sockaddr *) &echoservaddr, sizeof(echoservaddr)) < 0) DieWithError("connect() failed"); echostringlen = strlen(echostring); /* Determine input length */ /* Send the string to the server */ if (send (sock, echostring, echostringlen, 0)!= echostringlen) DieWithError("send() sent a different number of bytes than expected"); /* Receive the same string back from the server */ totalbytesrcvd = 0; printf("received: "); /* Setup to print the echoed string */ TCP/IP Sockets 28 D&C

TCP Echo Client } while (totalbytesrcvd < echostringlen) { /* Receive up to the buffer size (minus 1 to leave space for a null terminator) bytes from the sender */ if ((bytesrcvd = recv(sock, echobuffer, RCVBUFSIZE - 1, 0)) <= 0) DieWithError("recv() failed or connection closed prematurely"); totalbytesrcvd += bytesrcvd; /* Keep tally of total bytes */ echobuffer[bytesrcvd] = '\0'; /* Terminate the string! */ printf("%s", echobuffer); /* Print the echo buffer */ } printf("\n"); /* Print a final linefeed */ close (sock); exit(0); D&C TCP/IP Sockets 29

BACKUP SLIDES

socket interface The Socket Interface Application 1 Application 2 socket interface user kernel user kernel Socket Socket Underlying communication Protocols Underlying communication Protocols Communications network Copyright 2000 The McGraw Hill Companies Leon-Garcia & Widjaja: Communication Networks Figure 2.16 TCP/IP Sockets 31