Computer Networks Project 0 Prof Jerry Breecher Networks
Computer Networks Project 0 Prof. Jerry Breecher Networks - Project 0 1
What You Will Do In This Project. The purpose of this project is to help you become familiar with the 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 two tasks before you: 1. 2. 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 with this part when your program works successfully. Be able to demonstrate that you understand what this program is doing – this means you can answer questions about the various code within this program. You should also be able to answer simple questions about gdb and vi. Networks - Project 0 2
Where To Get Documentation There are many sources of information to help you with this project. Here are some of those sources: Learning C: Learning GDB – how to debug: Learning UNIX: Many people find it useful to use an IDE to accomplish programming and debugging tasks. Networks - Project 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/software/gdb/documentation/ GCC – Compiler: - remote copy is at: http: //gcc. gnu. org/onlinedocs/gcc-4. 0. 1/gcc. html Networks - Project 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 - Project 0 5
Project 0: Here’s the code for this project. 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 proj 0. c Get a random port number. Available numbers are between 0 and 65536. I recommend you stay away from numbers lower than 1000 – they tend to be reserved for system applications. Using the last four digits of your student ID is probably a fairly random way to pick a number. To compile this code, say “gcc –g project 0. c –o proj 0” This will produce an output file that you can run. As the code explains, there are several modes of execution. proj 0 s& proj 0 c creates a process running the code as a server creates a process running the code as a client Networks - Project 0 6
Computer Chat • How do we make computers talk? • How are they interconnected? Internet Protocol (IP) Networks - Project 0 7
Internet Protocol (IP) • Datagram (packet) protocol • Best-effort service – Loss – Reordering – Duplication – Delay • Host-to-host delivery Networks - Project 0 8
IP Address • • 32 -bit identifier Dotted-quad: 134. 111. 10. 43 cs. clarku. edu -> 140. 232. 229. 9 Identifies a host interface (not a host) 192. 18. 22. 13 209. 134. 16. 123 Networks - Project 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 - Project 0 10
Ports Identifying the ultimate destination • IP addresses identify hosts (machines) • Host has many applications • Ports (16 -bit identifier identifies application within host) Application WWW Port 80 E-mail Telnet 25 23 140. 232. 1. 19 Networks - Project 0 11
Socket How does one speak TCP/IP? • Sockets provides interface to TCP/IP from application • Generic interface for many protocols Networks - Project 0 12
Sockets • Identified by protocol and local/remote address/port • Applications may refer to many sockets • Sockets accessed by many applications Networks - Project 0 13
TCP/IP Sockets • my. Sock = socket(family, type, protocol); • These are TCP/IP-specific sockets Family TCP UDP PF_INET Type Protocol SOCK_STREAM IPPROTO_TCP SOCK_DGRAM IPPROTO_UDP • Socket reference – File (socket) descriptor in Linux – Socket handle in Win. Sock (Windows) Networks - Project 0 14
IP Specific Generic Specifying Addresses • 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; unsigned short sin_port; struct in_addr sin_addr; char sin_zero[8]; }; struct in_addr { unsigned long s_addr; }; /* Internet protocol (AF_INET) */ /* Port (16 -bits) */ /* Internet address (32 -bits) */ /* Not used */ /* Internet address (32 -bits) */ Networks - Project 0 15
Overview of The Connection Mechanism TCP Server socket() bind() TCP Client listen() socket() connect() write() Connection establishment Data (Request) Data (Reply) accept() read() write() read() end-of-file notification read() close() Networks - Project 0 write() 16
Project 0. c – The Code: In the slides that follow, is a partial listing of the code for this project. It’s not exactly right. It’s given here with annotation so that you can better understand how it works. MAKE SURE YOUR INSTRUCTOR HAS GIVEN YOU ACCESS TO WORKING CODE! Networks - Project 0 17
Project 0. c – The Code: /********************************** proj 0. 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 data and sends it back. This program expects two arguments: proj 0 <client|server> <udp|tcp> The argument says whether this program is running as a client or server. The second argument says whether the connection is TCP or UDP. Version 1. 0 January, 2017 Jerry Breecher Initial Coding. **********************************/ #include #include <stdlib. h> <sys/socket. h> <netinet/in. h> <stdio. h> <string. h> These say to include more information from include files. #define TRUE FALSE BUFFER_SIZE 1 0 20 void Sys. Error( char * ); The compiler substitutes these values whenever it sees the define. A prototype. Networks - Project 0 18
proj 0. c – the code: int main ( int argc, char *argv[] ) { long int in_port_t int struct sockaddr_in socklen_t int char A C program always starts at main() input_value; family = AF_INET; // The default for most cases type = SOCK_STREAM; // Says it's TCP connection port = 54321; result; sa; lsa = sizeof(sa); fd. Listen, fd. Conn, fd; console_buffer[BUFFER_SIZE]; ip_input_buffer[BUFFER_SIZE]; ip_output_buffer[BUFFER_SIZE]; This section is declaring the variables. Networks - Project 0 19
proj 0. c – the code: Check that the argument if ( argc < 2 ) { was ); input. printf( "The program expects argumentsn" printf( "tcp <client|server>n" ); exit(0); } Open a socket. The socket if ((fd = socket (family, type, 0)) < 0) Sys. Error ("Error on socket"); sa. sin_family sa. sin_port sa. sin_addr. s_addr descriptor is returned in fd. = family; // client & server see same port = htons(port); = htonl(INADDR_ANY); // kernel assigns IP addr strcpy( console_buffer, argv[1] ); Fill in the structure that defines how we want to connect to other programs. Networks - Project 0 20
proj 0. c – the code: if ( console_buffer[0] == 's' || console_buffer[0] == 'S' ) { if (bind (fd, (struct sockaddr *)&sa, sizeof(sa) ) == -1) Sys. Error ("Error on bind"); if (listen (fd, SOMAXCONN) == -1) Sys. Error ("Error on listen"); // set up for listening fd. Listen = fd; Check for an “S”. If found, it means we want a server program here. Then do the bind and listen. Networks - Project 0 21
proj 0. c – the code: while( TRUE ) Repeat forever { if ((fd. Conn = accept (fd. Listen, (struct sockaddr *)&sa, &lsa )) <0) Sys. Error ("Error on accept"); bzero( ip_input_buffer, sizeof( ip_input_buffer )); while ( recv( fd. Conn, ip_input_buffer, BUFFER_SIZE - 2, 0 ) > 0 ) { input_value = atoi( ip_input_buffer ); recv from client input_value = input_value + 1; Calculate the new value bzero( ip_output_buffer, sizeof( ip_output_buffer )); sprintf( ip_output_buffer, "%d", input_value ); Send back if ( send( fd. Conn, ip_output_buffer, strlen(ip_output_buffer) +1, 0) <= 0 ) Sys. Error( "Error on send" ); } close (fd. Conn); } } to the client /* End of while recv is successful */ /* 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 - Project 0 22
proj 0. c – the code: else This is the else that says we did NOT ask for a server. { if (connect(fd, (struct sockaddr *)&sa, sizeof(sa) Sys. Error ("Error on connect"); ) ) So the first thing a client does is a connect to the server. Networks - Project 0 23
proj 0. c – the code: Loop here forever. while( TRUE ) { printf( "> " ); scanf( "%s", console_buffer ); Get data from console 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 ); send data to server if ( send( fd, ip_output_buffer, strlen(ip_output_buffer) + 1, 0 ) <= 0 ) Sys. Error( "Error on send" ); bzero( ip_input_buffer, sizeof(ip_input_buffer) ); } } } if ( recv( fd, ip_input_buffer, sizeof(ip_input_buffer) Sys. Error( "Error on recv" ); printf( "%sn", ip_input_buffer ); /* End of main Networks - Project 0 recv data from server - 2, 0 ) <= 0 ) while TRUE client case main */ */ */ 24
proj 0. c – the code: void Sys. Error( char *string ) { printf( "Error found: exit(0); How to use a subroutine. String given is --> %sn", string ); } Networks - Project 0 25
- Slides: 25