CS 640: Computer Networking

Similar documents
Network Socket Programming - 3 BUPT/QMUL

Network Socket Programming - 3 BUPT/QMUL

CS 3516: Computer Networks

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

Network Programming Week #1. K.C. Kim

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

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

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

A Client-Server Exchange

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

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

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

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

TCP Network Programming in C

Unix Network Programming

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

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

Socket Programming for TCP and UDP

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

Programming with TCP/IP. Ram Dantu

TCP: Three-way handshake

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

Socket Programming TCP UDP

Network Communication

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

EEC-484/584 Computer Networks

CSE 124 Discussion Section Sockets Programming 10/10/17

STUDY OF SOCKET PROGRAMMING

CS321: Computer Networks Introduction to Application Layer

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

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

CS321: Computer Networks Socket Programming

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

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

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

Tutorial on Socket Programming

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

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

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)

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

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

ECE 435 Network Engineering Lecture 2

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

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

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

Group-A Assignment No. 6

System Programming. Sockets

ECE 435 Network Engineering Lecture 2

Network Programming November 3, 2008

Department of Computer Science

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

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

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

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

Introduction to Socket Programming

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

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

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

Introduction to Socket Programming

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

UNIX Sockets. COS 461 Precept 1

CS4700/CS5700 Fundamentals of Computer Networking

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

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

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

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

Socket Programming(2/2)

Elementary TCP Sockets

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

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

Sockets 15H2. Inshik Song

UNIX Network Programming. Overview of Socket API Network Programming Basics

CLIENT-SIDE PROGRAMMING

Network programming(i) Lenuta Alboaie

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

CSE 333 SECTION 8. Sockets, Network Programming

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

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

CSE 333 SECTION 7. Client-Side Network Programming

Socket Programming 2007/03/28

Lab 0. Yvan Petillot. Networks - Lab 0 1

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

ICT 6544 Distributed Systems Lecture 5

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

CSE 333 Section 8 - Client-Side Networking

Communication. Sockets (Haviland Ch. 10)

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

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

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

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

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

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

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

Communication Networks ( ) / Spring 2011 The Blavatnik School of Computer Science, Tel-Aviv University. Allon Wagner

CSE 333 Lecture 16 - network programming intro

Communication Networks ( ) / Fall 2013 The Blavatnik School of Computer Science, Tel-Aviv University

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

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

Systems software design NETWORK COMMUNICATIONS & RPC SYSTEMS

Transcription:

CS 640: Computer Networking Yu-Chi Lai Lecture 3 Network Programming

Topics Client-server model Sockets interface Socket primitives Example code for echoclient and echoserver Debugging With GDB Programming Assignment 1 (MNS)

Client/server model Client asks (request) server provides (response) Typically: single server - multiple clients The server does not need to know anything about the client even that it exists The client should always know something about the server at least where it is located 4. Client handles response Client process 1. Client sends request 3. Server sends response Server process 2. Server handles request Resource Note: clients and servers are processes running on hosts (can be the same or different hosts).

Internet Connections (TCP/IP) Address the machine on the network By IP address Address the process By the port -number The pair of IP-address + port makes up a socket-address Client socket address 128.2.194.242:3479 Server socket address 208.216.181.15:80 Client Connection socket pair (128.2.194.242:3479, 208.216.181.15:80) Server (port 80) Client host address 128.2.194.242 Note: 3479 is an ephemeral port allocated by the kernel Server host address 208.216.181.15 Note: 80 is a well-known port associated with Web servers

Clients Examples of client programs Web browsers, ftp, telnet, ssh How does a client find the server? The IP address in the server socket address identifies the host The (well-known) port in the server socket address identifies the service, and thus implicitly identifies the server process that performs that service. Examples of well known ports Port 7: Echo server Port 23: Telnet server Port 25: Mail server Port 80: Web server

Using Ports to Identify Services Server host 128.2.194.242 Client host Client Service request for 128.2.194.242:80 (i.e., the Web server) Kernel Web server (port 80) Echo server (port 7) Client Service request for 128.2.194.242:7 (i.e., the echo server) Kernel Web server (port 80) Echo server (port 7)

Servers Servers are long-running processes (daemons). Created at boot-time (typically) by the init process (process 1) Run continuously until the machine is turned off. Each server waits for requests to arrive on a well-known port associated with a particular service. Port 7: echo server Port 23: telnet server Port 25: mail server Port 80: HTTP server See /etc/services for a comprehensive list of the services available on a Linux machine. Other applications should choose between 1024 and 65535

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

Sockets What is a socket? To the kernel, a socket is an endpoint of communication. To an application, a socket is a file descriptor that lets the application read/write from/to the network. Remember: All Unix I/O devices, including networks, are modeled as files. Clients and servers communicate with each by reading from and writing to socket descriptors. The main distinction between regular file I/O and socket I/O is how the application opens the socket descriptors.

Socket Programming Cliches Network Byte Ordering Network is big-endian, host may be big- or little-endian Functions work on 16-bit (short) and 32-bit (long) values htons() / htonl() : convert host byte order to network byte order ntohs() / ntohl(): convert network byte order to host byte order Use these to convert network addresses, ports, struct sockaddr_in serveraddr; /* fill in serveraddr with an address */ /* Connect takes (struct sockaddr *) as its second argument */ connect(clientfd, (struct sockaddr *) &serveraddr, sizeof(serveraddr)); Structure Casts You will see a lot of structure casts

Socket primitives SOCKET: int socket(int domain, int type, int protocol); domain := AF_INET (IPv4 protocol) type := (SOCK_DGRAM or SOCK_STREAM ) protocol := 0 (IPPROTO_UDP or IPPROTO_TCP) returned: socket descriptor (sockfd), -1 is an error BIND: int bind(int sockfd, struct sockaddr *my_addr, int addrlen); sockfd - socket descriptor (returned from socket()) my_addr: socket address, struct sockaddr_in is used addrlen := sizeof(struct sockaddr) struct sockaddr_in { unsigned short sin_family; /* address family (always AF_INET) */ unsigned short sin_port; /* port num in network byte order */ struct in_addr sin_addr; /* IP addr in network byte order */ unsigned char sin_zero[8]; /* pad to sizeof(struct sockaddr) */ };

LISTEN: int listen(int sockfd, int backlog); backlog: how many connections we want to queue ACCEPT: int accept(int sockfd, void *addr, int *addrlen); addr: here the socket-address of the caller will be written returned: a new socket descriptor (for the temporal socket) CONNECT: int connect(int sockfd, struct sockaddr *serv_addr, int addrlen); //used by TCP client parameters are same as for bind() SEND: int send(int sockfd, const void *msg, int len, int flags); msg: message you want to send len: length of the message flags := 0 returned: the number of bytes actually sent RECEIVE: int recv(int sockfd, void *buf, int len, unsigned int flags); buf: buffer to receive the message len: length of the buffer ( don t give me more! ) flags := 0 returned: the number of bytes received

SEND (DGRAM-style): int sendto(int sockfd, const void *msg, int len, int flags, const struct sockaddr *to, int tolen); msg: message you want to send len: length of the message flags := 0 to: socket address of the remote process tolen: = sizeof(struct sockaddr) returned: the number of bytes actually sent RECEIVE (DGRAM-style): int recvfrom(int sockfd, void *buf, int len, unsigned int flags, struct sockaddr *from, int *fromlen); buf: buffer to receive the message len: length of the buffer ( don t give me more! ) from: socket address of the process that sent the data fromlen:= sizeof(struct sockaddr) flags := 0 returned: the number of bytes received CLOSE: close (socketfd);

Client+server: connectionless CREATE BIND SEND RECEIVE SEND CLOSE

Client+server: connection-oriented CONNECT SOCKET TCP three-way handshake BIND LISTEN ACCEPT SEND RECEIVE RECEIVE SEND Concurrent server CLOSE

Echo Client-Server

#include s #include <stdio.h> /* for printf() and fprintf() */ #include <sys/socket.h> /* for socket(), connect(), sendto(), and recvfrom() */ #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() */ #include <netdb.h> /* Transform the ip address string to real uint_32 */ #define ECHOMAX 255 /* Longest string to echo */

EchoClient.cpp -variable declarations int main(int argc, char *argv[]) { int sock; /* Socket descriptor */ struct sockaddr_in echoservaddr; /* Echo server address */ struct sockaddr_in fromaddr; /* Source address of echo */ unsigned short echoservport =2000; /* Echo server port */ unsigned int fromsize; /* address size for recvfrom() */ char *servip= 172.24.23.4 ; /* IP address of server */ char *echostring= I hope this works ; echo server */ /* String to send to char echobuffer[echomax+1]; echoed string */ /* Buffer for receiving int echostringlen; /* Length of string to echo */ int respstringlen; /* Length of received response */

EchoClient.c - creating the socket /* Create a datagram/udp socket and error check */ sock = socket(af_inet, SOCK_DGRAM, 0); if(sock <= 0){ } printf("socket open error\n"); exit(1);

EchoClient.cpp sending /* Construct the server address structure */ memset(&echoservaddr, 0, sizeof(echoservaddr)); /* Zero out structure */ echoservaddr.sin_family = AF_INET; /* Internet addr family */ inet_pton(af_inet, servip, &echoservaddr.sin_addr); /* Server IP address */ echoservaddr.sin_port = htons(echoservport); /* Server port */ /* Send the string to the server */ echostringlen = strlen(echostring); sendto(sock, echostring, echostringlen, 0, (struct sockaddr *) &echoservaddr, sizeof(echoservaddr);

EchoClient.cpp receiving and printing /* Recv a response */ fromsize = sizeof(fromaddr); recvfrom(sock, echobuffer, ECHOMAX, 0, (struct sockaddr *) &fromaddr, &fromsize); /* Error checks like packet is received from the same server*/ /* null-terminate the received data */ echobuffer[echostringlen] = '\0'; printf("received: %s\n", echobuffer); /* Print the echoed arg */ close(sock); exit(0); } /* end of main () */

EchoServer.cpp creating socket int main(int argc, char *argv[]) { int sock; /* Socket */ struct sockaddr_in echoservaddr; /* Local address */ struct sockaddr_in echoclntaddr; /* Client address */ unsigned int cliaddrlen; /* Length of incoming message */ char echobuffer[echomax]; /* Buffer for echo string */ unsigned short echoservport =2000; /* Server port */ int recvmsgsize; /* Size of received message */ /* Create socket for sending/receiving datagrams */ sock = socket(af_inet, SOCK_DGRAM, 0); if(sock <= 0){ printf("socket open error\n"); exit(1); }

EchoServer.cpp binding /* 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); echoservaddr.sin_port = htons((uint16_t) echoservport); /* Local port */ /* Bind to the local address */ int error_test = bind(sock, (struct sockaddr *) &echoservaddr, sizeof(echoservaddr)); if(error_test < 0){ printf("binding error\n"); exit(1); }

EchoServer.cpp receiving and echoing for (;;) /* Run forever */ { cliaddrlen = sizeof(echoclntaddr); /* Block until receive message from a client */ recvmsgsize = recvfrom(sock, echobuffer, ECHOMAX, 0, (struct sockaddr *) &echoclntaddr, &cliaddrlen); printf("handling client %s\n", inet_ntoa(echoclntaddr.sin_addr)); } /* Send received datagram back to the client */ sendto(sock, echobuffer, recvmsgsize, 0, (struct sockaddr *) &echoclntaddr, sizeof(echoclntaddr); } /* end of main () */ Error handling is must

Socket Programming Help man is your friend man accept man sendto Etc. The manual page will tell you: What #include<> directives you need at the top of your source code The type of each argument The possible return values The possible errors (in errno)

Debugging with gdb Prepare program for debugging Compile with -g (keep full symbol table) Don t use compiler optimization ( -O, O2, ) Two main ways to run gdb On program directly gdb progname Once gdb is executing we can execute the program with: run args On a core (post-mortem) gdb progname core Useful for examining program state at the point of crash Extensive in-program documentation exists help (or help <topic> or help <command> )

More information Socket programming W. Richard Stevens, UNIX Network Programming Infinite number of online resources http://www.cs.rpi.edu/courses/sysprog/sockets/sock.html GDB Official GDB homepage: http://www.gnu.org/software/gdb/ gdb.html GDB primer: http://www.cs.pitt.edu/~mosse/gdb-note.html

Project Partners If you don t have a partner Stay back after class Now Overview of PA 1