Lab 0. Yvan Petillot. Networks - Lab 0 1

Similar documents
TCP Network Programming in C

CS321: Computer Networks Socket Programming

CLIENT-SIDE PROGRAMMING

CS321: Computer Networks Introduction to Application Layer

ECE 435 Network Engineering Lecture 2

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

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

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

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

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

CS 3516: Computer Networks

ECE 435 Network Engineering Lecture 2

Sockets. Jin-Soo Kim Computer Systems Laboratory Sungkyunkwan University

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

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

The Berkeley Sockets API. Networked Systems Architecture 3 Lecture 4

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

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

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

System Programming. Sockets

Tutorial on Socket Programming

CSE 333 SECTION 6. Networking and sockets

Socket Programming. Sungkyunkwan University. Hyunseung Choo Copyright Networking Laboratory

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

CS 640: Computer Networking

Application Programming Interfaces

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

CSC209H Lecture 9. Dan Zingaro. March 11, 2015

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

Network Programming Week #1. K.C. Kim

A Client-Server Exchange

Network Socket Programming - 3 BUPT/QMUL

Network Socket Programming - 3 BUPT/QMUL

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

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

Unix Network Programming

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

CS4514 B08 HELP Session 1

SOCKETS. COMP750 Distributed Systems

Introduction to Socket Programming

Communication. Communication. Distributed Systems. Networks and protocols Sockets Remote Invocation Messages Streams. Fall /10/2001 DoCS

ICT 6544 Distributed Systems Lecture 5

Programming with TCP/IP. Ram Dantu

CSE 124 Discussion Section Sockets Programming 10/10/17

UNIX Network Programming. Overview of Socket API Network Programming Basics

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

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

Socket Programming TCP UDP

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

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

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

Network Socket Programming - 2 BUPT/QMUL

Sockets. 1 Introduction. (Reference:, Gray Chapter 10) Network Programming Lecture Notes by. Turhan TUNALI

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

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

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

CSE 333 SECTION 7. Client-Side Network Programming

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

CSE 333 Lecture 16 - network programming intro

Distributed programming

CSE 333 SECTION 8. Sockets, Network Programming

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

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

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

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

Ken French HELP Session 1 CS4514

Internet applications

Interprocess Communication Mechanisms

CS4700/CS5700 Fundamentals of Computer Networking

CSE 333 SECTION 7. Client-Side Network Programming

CSE 333 Lecture network programming intro

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

Department of Computer Science

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

How to write a Measurement Telnet Server

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

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

Interprocess Communication Mechanisms

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

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

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

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

Elementary TCP Sockets

Chapter 2 Applications and

C Structures in Practice

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

TCP: Three-way handshake

Socket Programming for TCP and UDP

EECS122 Communications Networks Socket Programming. Jörn Altmann

Piotr Mielecki Ph. D.

Introduction to Lab 2 and Socket Programming. -Vengatanathan Krishnamoorthi

Sockets 15H2. Inshik Song

Topics for this Week

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

CompSci 356: Computer Network Architectures. Lecture 3: Network Architecture Examples and Lab 1. Xiaowei Yang

Communication. Sockets (Haviland Ch. 10)

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

Network Programming November 3, 2008

Internetworking II: Network programming. April 20, 2000

CSE 333 Section 8 - Client-Side Networking

Transcription:

Lab 0 Yvan Petillot Networks - Lab 0 1

What You Will Do In This Lab. The purpose of this lab is to help you become familiar with the UNIX/LINUX on the lab network. This means being able to do editing, compiling, etc. of simple programs. These programs will be written in C, so you may have some more learning/reviewing ahead of you. You have one task before you: Using your favorite editor, type in (or paste) the program given later in this document. Compile it and run it and show that it produces communication between two instances of the program. You will know you are done when you have demonstrated to me that your program works. Networks - Lab 0 2

Where To Get Documentation There are many sources of information to help you with this lab. Here are some of those sources: Learning C: Learning GDB how to debug: Learning UNIX: All of these skills can be acquired (I hope) from the documentation available on my webpage If you don t like these documents, there are plenty of other ones out on the web. Go wild! Networks - Lab 0 3

Where To Get Documentation For information in more detail than is available off of my home page, see the following links: GNU Debugger remote copy is at: http://www.gnu.org/manual/gdb-4.17/html_mono/gdb.html GCC Compiler: - remote copy is at: http://gcc.gnu.org/onlinedocs/gcc-3.0.1/gcc.html Networks - Lab 0 4

Detour a gdb quickstart Here s all you need to know to get started using gdb: Start the debugger with gdb program_name List the lines with l Set a breakpoint with b <line_number> Print the value of a variable with p <variable_name> To run the first time, say run <optional arguments> To continue from a breakpoint, use c To single step, use s To stop the debugger, use q Networks - Lab 0 5

Project 0: Here s the code for this lab. We will be going through it so that you understand what it does. Type it in using your favorite editor. In this example, the source file is named proj0.c Get a port number from me. This way you won t all be colliding with each other. To compile this code, say gcc g proj0.c o proj0 This will produce an output file that you can run. As the code explains, there are several modes of execution. proj0 s& creates a process running the code as a server proj0 c creates a process running the code as a client Networks - Lab 0 6

Computer Chat How do we make computers talk? How are they interconnected? Internet Protocol (IP) Networks - Lab 0 7

Internet Protocol (IP) Datagram (packet) protocol Best-effort service Loss Reordering Duplication Delay Host-to-host delivery Networks - Lab 0 8

IP Address 32-bit identifier Dotted-quad: 134.111.10.43 www.clarku.edu -> 140.232.1.19 Identifies a host interface (not a host) Networks - Lab 0 9

Transport Protocols Best-effort not sufficient! Add services on top of IP User Datagram Protocol (UDP) Data checksum Best-effort Transmission Control Protocol (TCP) Data checksum Reliable byte-stream delivery Flow and congestion control Networks - Lab 0 10

Ports Identifying the ultimate destination IP addresses identify hosts Host has many applications Ports (16-bit identifier) $SSOLFDWLRQ:::(PDLO7HOQHW 3RUW Networks - Lab 0 11

Socket How does one speak TCP/IP? Sockets provides interface to TCP/IP Generic interface for many protocols Networks - Lab 0 12

Sockets Identified by protocol and local/remote address/port Applications may refer to many sockets Sockets accessed by many applications Networks - Lab 0 13

TCP/IP Sockets mysock = socket(family, type, protocol); TCP/IP-specific sockets Family Type Protocol TCP UDP PF_INET SOCK_STREAM SOCK_DGRAM IPPROTO_TCP IPPROTO_UDP Socket reference File (socket) descriptor in UNIX Socket handle in WinSock Networks - Lab 0 14

Specifying Addresses *HQHULF,36SHFLILF struct sockaddr { unsigned short sa_family; /* Address family (e.g., AF_INET) */ char sa_data[14]; /* Protocol-specific address information */ }; struct sockaddr_in { unsigned short sin_family; /* Internet protocol (AF_INET) */ unsigned short sin_port; /* Port (16-bits) */ struct in_addr sin_addr; /* Internet address (32-bits) */ char sin_zero[8]; /* Not used */ }; struct in_addr { unsigned long s_addr; /* Internet address (32-bits) */ }; Networks - Lab 0 15

Overview of The Connection Mechanism TCP Client socket() connect() Connection establishment TCP Server socket() bind() listen() accept() write() Data (Request) read() Data (Reply) write() read() close() end-of-file notification read() write() Networks - Lab 0 16

server.c the code: /*************************************************************************** server.c Designed as a simple class example. The program waits for a request. It assumes that request is numerical. It adds +1 to the input and sends it back. This program expects no arguments: server Yvan Petillot October 2002 ****************************************************************************/ #include <stdlib.h> #include <sys/socket.h> #include <netinet/in.h> #define TRUE 1 #define FALSE 0 #define BUFFER_SIZE 20 #define PORT 10000 void SysError( char * ); These say to include more information from include files. The compiler substitutes these values whenever it sees the define. A prototype. Networks - Lab 0 17

main ( int argc, char *argv[] ) { long input_value; server.c the code: A C program always starts at main() int family = AF_INET; /* The default for most cases */ int type = SOCK_STREAM; /* Says it s a TCP connection */ in_port_t port=port; int result; struct sockaddr_in server; int lserver = sizeof(server); int fdlisten, fdconn, fd; char console_buffer[buffer_size]; char ip_input_buffer[buffer_size]; char ip_output_buffer[buffer_size]; This section is declaring the variables. Networks - Lab 0 18

server.c the code: if ((fd = socket (family, type, 0)) < 0) SysError ("Error on socket"); Open a socket. The socket descriptor is returned in fd. server.sin_family = family; server.sin_port = port; /* client & server see same port*/ server.sin_addr.s_addr = htonl(inaddr_any); /* the kernel assigns the IP addr*/ Fill in the structure that defines how we want to connect to other programs. Networks - Lab 0 19

server.c the code: if (bind (fd, (struct sockaddr *)&server, sizeof(server) ) == -1) SysError ("Error on bind"); if (listen (fd, SOMAXCONN) == -1) /* set up for listening */ SysError ("Error on listen"); fdlisten = fd; Server program here. Then do the bind and listen. Networks - Lab 0 20

server.c the code: while( TRUE ) Repeat forever { if ((fdconn = accept (fdlisten, (struct sockaddr *)&server, &lserver )) <0) SysError ("Error on accept"); bzero( ip_input_buffer, sizeof( ip_input_buffer )); while ( recv( fdconn, ip_input_buffer, BUFFER_SIZE - 2, 0 ) > 0 ) { input_value = atoi( ip_input_buffer ); recv from client input_value = input_value + 1; bzero( ip_output_buffer, sizeof( ip_output_buffer )); sprintf( ip_output_buffer, "%d", input_value ); if ( send( fdconn, ip_output_buffer, strlen(ip_output_buffer) +1, 0) <= 0 ) SysError( "Error on send" ); Calculate the new value Send back to the client } /* End of while recv is successful */ close (fdconn); } /* End of while TRUE */ } /* End of server case */ recv will keep on working until the client closes the connection. The recv will then take an error in that case. Networks - Lab 0 21

main ( int argc, char *argv[] ) { long input_value; client.c the code: A C program always starts at main() int family = AF_INET; /* The default for most cases */ int type = SOCK_STREAM; /* Says it s a TCP connection */ in_port_t port=port; int result; struct sockaddr_in client; struct sockaddr_in server; int lclient = sizeof(client); int fdlisten, fdconn, fd; char console_buffer[buffer_size]; char ip_input_buffer[buffer_size]; char ip_output_buffer[buffer_size]; struct hostent *host; This section is declaring the variables. Networks - Lab 0 22

client.c the code: /* Checks first we have the correct number of arguments */ if ( argc < 2 ) { printf( "The program expects arguments\n" ); printf( "tcp_client <hostname>\n" ); exit(0); } Check Number of arguments of program if ((host = gethostbyname(argv[1])) == (struct hostent *)NULL) { SysError("Error on gethostbyname"); return(-1); } if ((fd = socket (family, type, 0)) < 0) SysError ("Error on socket"); Find Host IP address based on Name using DNS server Fill in the structure that defines how we want to connect to other programs. client.sin_family = family; client.sin_port = port; /* client & server see same port*/ client.sin_addr.s_addr = htonl(inaddr_any); /* the kernel assigns the IP addr*/ Networks - Lab 0 23

Client.c the code: /* Fills server socket structure with correct fields */ server.sin_family = family; server.sin_port = port; /* client & server see same port*/ memcpy((char *)&server.sin_addr, (char *)host->h_addr, host->h_length); Initialise server characteristics. Networks - Lab 0 24

client.c the code: This is for a client. if (connect(fd, (struct sockaddr *)&server, sizeof(server) ) ) SysError ("Error on connect"); So the first thing a client does is a connect to the server. Networks - Lab 0 25

client.c the code: while( TRUE ) { printf( "> " ); scanf( "%s", console_buffer ); if ( atoi( console_buffer ) == -1 ) { printf( "We Have Successfully Finished.\n" ); exit(0); } bzero( ip_output_buffer, sizeof( ip_output_buffer )); strcpy( ip_output_buffer, console_buffer ); if ( send( fd, ip_output_buffer, strlen(ip_output_buffer) + 1, 0 ) <= 0 ) SysError( "Error on send" ); bzero( ip_input_buffer, sizeof(ip_input_buffer) ); if ( recv( fd, ip_input_buffer, sizeof(ip_input_buffer) - 2, 0 ) <= 0 ) SysError( "Error on recv" ); printf( "%s\n", ip_input_buffer ); } /* End of while TRUE */ } /* End of client case */ } /* End of main */ End of main Loop here forever. Get data from console send data to server recv data from server Networks - Lab 0 26

server.c / client.c the code: How to use a void SysError( char *string ) { subroutine. printf( "Error found: String given is --> %s\n", string ); exit(0); } Networks - Lab 0 27