ECE 435 Network Engineering Lecture 2

Similar documents
ECE 435 Network Engineering Lecture 2

CS321: Computer Networks Socket Programming

CS321: Computer Networks Introduction to Application Layer

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

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

Application Programming Interfaces

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

Lab 0. Yvan Petillot. Networks - Lab 0 1

CS 640: Computer Networking

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

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

A Client-Server Exchange

CSE 333 SECTION 6. Networking and sockets

CSE 124 Discussion Section Sockets Programming 10/10/17

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

CS 3516: Computer Networks

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

CSE 333 Lecture 8 - file and network I/O

Piotr Mielecki Ph. D.

CSE 333 Lecture 16 - network programming intro

CSE 333 Lecture network programming intro

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

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

CSE 333 SECTION 8. Sockets, Network Programming

CS307 Operating Systems Processes

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

ICT 6544 Distributed Systems Lecture 5

Socket Programming TCP UDP

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

UNIX Network Programming. Overview of Socket API Network Programming Basics

Network Programming November 3, 2008

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

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

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

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

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

Socket Programming for TCP and UDP

Interprocess Communication Mechanisms

Network Socket Programming - 3 BUPT/QMUL

Overview. Administrative. * HW# 5 Due next week. * HW# 5 : Any Questions. Topics. * Client Server Communication. * 12.

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

SOCKETS. COMP750 Distributed Systems

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

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

CSE 333 SECTION 7. Client-Side Network Programming

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

Network Socket Programming - 3 BUPT/QMUL

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

How to write a Measurement Telnet Server

Interprocess Communication Mechanisms

CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Sample Exam 2 Questions (document version 1.0)

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

CS 450 Introduction to Networking Spring 2014 Homework Assignment 1 File Transfer and Data Bandwidth Analysis Tool

Internet protocol stack. Internetworking II: Network programming. April 20, UDP vs TCP. Berkeley Sockets Interface.

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

Internetworking II: Network programming. April 20, 2000

System Programming. Sockets

EECS122 Communications Networks Socket Programming. Jörn Altmann

Introduction to Client-Server Model

ECE 435 Network Engineering Lecture 5

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

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

CLIENT-SIDE PROGRAMMING

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

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

CSE 333 SECTION 7. Client-Side Network Programming

1 /* client.c - adapted from code for example client program that uses TCP */ 2 /*Modified by Vincent Chu, Winter

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

Programming with TCP/IP. Ram Dantu

Socket Programming. Sungkyunkwan University. Hyunseung Choo Copyright Networking Laboratory

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

How to write a Measurement Telnet Server

Tutorial on Socket Programming

Introduction to Socket Programming

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

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

CSE 333 Section 3. Thursday 12 April Thursday, April 12, 12

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

Introduction to Socket Programming

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

Network Socket Programming - 2 BUPT/QMUL

Sockets 15H2. Inshik Song

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

Department of Computer Science

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

Network Software Implementations

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

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

ECE322 Systems Programming Project 2: Networking with Matrix Multiplication in C Grant Kimes 12/16/15

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

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

IPv6 Porting Applications

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

TCP Network Programming in C

CS4514 (C04) HELP Session 1 Introduction to Network Programming (v1.3)

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

Unix Network Programming

Client/Server. Networking Approach.

Elementary TCP Sockets

A. Basic Function Calls for Network Communications

Transcription:

ECE 435 Network Engineering Lecture 2 Vince Weaver http://web.eece.maine.edu/~vweaver vincent.weaver@maine.edu 31 August 2017

Announcements Homework 1 will be posted. Will be on website, will announce via mainestreet e-mail Due next Thursday (via e-mail) 1

Socket Programming BSD sockets Sort of at the transport layer, we are skipping ahead here Going over it now as it will be HW#1 Will reuse the code throughout the semester 2

Can you be both? Client and Server 3

Low level C programming Why C code? Close to hardware. Always know what s going on. Performance. I like it. Why not C-code? Hard to code Security 4

Smallest C Program What do all the parts do? argc/argv handle command line arguments. what are sycalls? How does printf work? # include <stdio.h> int main ( int argc, char ** argv ) { return 0; } 5

Opening a socket for listening Explain system calls, and file descriptors. Can use man socket to show socket manpage, etc. /* Open a socket to listen on */ /* AF_INET means an IPv4 connection ( others are possible ) */ /* SOCK_STREAM means reliable two - way connection ( TCP ) */ /* last argument is protocol subset. We leave at zero */ int socket_fd = socket ( AF_INET, SOCK_STREAM, 0); if ( socket_fd <0) { fprintf ( stderr," Error opening socket! %s\n", strerror ( errno )); } 6

Address and Port More layer violations While in theory generic, we are coding to TCP/IP here Address is typically the global IP address can run on same machine with localhost Port is how you handle multiple applications on same machine, based on the port it can map back to which application (the OS has a table) On TCP/IP limited to a 16-bit port number (65536) 7

Setting up Address htons() has to do with endianess memset() be sure to get order of arguments right! C structures and how they work struct sockaddr_in server_addr ; /* Set up the server address to listen on */ memset (& server_addr,0, sizeof ( struct sockaddr_in )); server_addr. sin_family = AF_INET ; /* Convert the port we want to network byte order ( short ) */ server_addr. sin_port = htons ( port ); 8

bind() system call Gives the socket an address. Since we re a server and listening we don t have to give an address we use 0.0.0.0 (set by memset) which means to listen on all networks. /* Bind to the port */ if ( bind ( socket_fd, ( struct sockaddr *) & server_addr, sizeof ( server_addr )) <0) { fprintf ( stderr," Error binding! %s\n", strerror ( errno )); } 9

listen() system call Sets up a data structure to hold pending incoming connections in case more than one come in at once. /* Tell the server we want to listen on the port */ /* Second argument is backlog, how many pending connections ca /* build up */ listen ( socket_fd,5); 10

accept() system call /* Call accept to create a new file descriptor for an incoming */ /* connection. It takes the oldest one off the queue */ /* We re blocking so it waits here until a connection happens */ client_len = sizeof ( client_addr ); new_socket_fd = accept ( socket_fd, ( struct sockaddr *)& client_addr,& client_len ); if ( new_socket_fd <0) { fprintf ( stderr," Error accepting! %s\n",strerror ( errno )); } 11

read() system call Can also use recv() if need extra options. /* Someone connected! Let s try to read BUFFER_SIZE -1 bytes */ memset ( buffer,0, BUFFER_SIZE ); n = read ( new_socket_fd, buffer,( BUFFER_SIZE -1)); if (n ==0) { fprintf ( stderr," Connection to client lost \n\n"); } else if (n <0) { fprintf ( stderr," Error reading from socket %s\n", strerror ( errno )); } /* Print the message we received */ printf (" Message from client : %s\n",buffer ); 12

write() system call Can also use send() if need extra options. /* Print the message we received */ printf (" Message from client : %s\n",buffer ); /* Send a response */ n = write ( new_socket_fd," Got your message, thanks!",25); if (n <0) { fprintf ( stderr," Error writing. %s\n", strerror ( errno )); } 13

close() system call printf (" Exiting server \n\n"); /* Try to avoid TIME_WAIT */ // sleep (1); /* Close the sockets */ close ( new_socket_fd ); close ( socket_fd ); 14

Notes What if you don t want to exit after, but instead loop? What happens if you have more than one incoming connection? poll() vs busy wait? What if you want to handle multiple connections at once? 15

Client Code 16

socket() again /* Open a socket file descriptor */ /* AF_INET means an IP network socket, not a local ( AF_UNIX ) one */ /* There are other types you can open too */ /* SOCK_STREAM means reliable two - way byte stream ( TCP ) */ /* last argument is protocol subset. We leave at zero */ socket_fd = socket ( AF_INET, SOCK_STREAM, 0); if ( socket_fd <0) { fprintf ( stderr," Error socket : %s\n", strerror ( errno )); } 17

get host address / port Note for this example using localhost. This is a special case, 127.0.0.1 on IPv4. Could put in a host name, this gets looked up via DNS. Or manually put in an IP address. /* Look up the server info based on its name */ server = gethostbyname ( DEFAULT_HOSTNAME ); if ( server == NULL ) { fprintf ( stderr," ERROR! No such host!\n"); exit (0); } /* clear out the server_addr structure and set some fields */ 18

/* Set it to connect to the address and port of our server */ memset (& server_addr,0, sizeof ( server_addr )); server_addr. sin_family = AF_INET ; memcpy ( server -> h_addr,& server_addr. sin_addr. s_addr, server -> h_length ); /* port should be in " network byte order " ( big - endian ) so convert * /* htons = host to network [ byte order ] short */ server_addr. sin_port = htons ( port ); 19

connect system call /* Call the connect system call to actually connect to server */ if ( connect ( socket_fd,( struct sockaddr *) & server_addr, sizeof ( server_addr )) < 0) { fprintf ( stderr," Error connecting! %s\n", strerror ( errno )); } 20

wait for response with read() /* Prompt for a message */ printf (" Please enter a message to send : "); memset ( buffer,0, BUFFER_SIZE ); /* Read message */ fgets ( buffer, BUFFER_SIZE -1, stdin ); /* Write to socket using the " write " system call */ n = write ( socket_fd, buffer, strlen ( buffer )); if (n <0) { fprintf ( stderr," Error writing socket! %s\n", strerror ( errno )); } 21

wait for response with read() /* Clear buffer and read the response from the server */ memset ( buffer,0, BUFFER_SIZE ); n = read ( socket_fd, buffer, BUFFER_SIZE -1); if (n <0) { fprintf ( stderr," Error reading socket! %s\n", strerror ( errno )); } /* Print the response we got */ printf (" Received back from server : %s\n\n",buffer ); 22

close again /* All finished, close the socket / file descriptor */ close ( socket_fd ); 23

Notes on Homework Make the server loop forever until a string comes in. How do you loop forever? How do you compare with a string? Be careful with strcmp() You might even want to use strncmp() 24