Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Networks Project 0: C Programming and TCP/IP Communication - Prof. Jerry Breecher, Study notes of Computer Systems Networking and Telecommunications

Information for a university computer networking project where students are required to write a simple c program that uses tcp/ip for communication between two instances. The project aims to help students become familiar with unix/linux, c programming, and debugging using gdb. Students are expected to compile and run the program, demonstrating successful communication between two instances.

Typology: Study notes

Pre 2010

Uploaded on 08/07/2009

koofers-user-kw9
koofers-user-kw9 ๐Ÿ‡บ๐Ÿ‡ธ

10 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
Networks - Project 0 1
Computer Networks
Project 0
Prof. Jerry Breecher
CSCI 280
Fall 2003
Networks - Project 0 2
What You Will Do In This Project.
The purpose of this project 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:
1. 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.
2. You will know you are done when you have demonstrated
to me that your program works.
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Networks Project 0: C Programming and TCP/IP Communication - Prof. Jerry Breecher and more Study notes Computer Systems Networking and Telecommunications in PDF only on Docsity!

Networks - Project 0 1

Computer Networks

Project 0

Prof. Jerry Breecher

CSCI 280

Fall 2003

Networks - Project 0 2

What You Will Do In This Project.

The purpose of this project 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:

1. 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.

2. You will know you are done when you have demonstrated

to me that your program works.

Networks - Project 0 3

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:

All of these skills can be acquired (I hope) from the documentation available

on my webpage โ€“ see the bottom of the page at

babbage.clarku.edu/~jbreecher

If you donโ€™t like these documents, there are plenty of other ones out on the

web. Go wild!

Networks - Project 0 4

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

Local copy is at: http://babbage.clarku.edu/~jbreecher/docs/gdb.html

GCC โ€“ Compiler: - remote copy is at:

http://gcc.gnu.org/onlinedocs/gcc-3.0.1/gcc.html

Networks - Project 0 7

Computer Chat

  • How do we make computers talk?
  • How are they interconnected?

Internet Protocol (IP)

Networks - Project 0 8

Internet Protocol (IP)

  • Datagram (packet) protocol
  • Best-effort service
    • Loss
    • Reordering
    • Duplication
    • Delay
  • Host-to-host delivery

Networks - Project 0 9

IP Address

  • 32-bit identifier
  • Dotted-quad: 134.111.10.
  • www.clarku.edu -> 140.232.1.
  • Identifies a host interface (not a host)

         

Networks - Project 0 10

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 13

Sockets

  • Identified by protocol and local/remote address/port
  • Applications may refer to many sockets
  • Sockets accessed by many applications

Networks - Project 0 14

TCP/IP Sockets

UDP SOCK_DGRAM IPPROTO_UDP

SOCK_STREAM IPPROTO_TCP PF_INET

TCP

Family Type Protocol

  • mySock = socket(family, type, protocol);
  • TCP/IP-specific sockets
  • Socket reference
    • File (socket) descriptor in UNIX
    • Socket handle in WinSock

Networks - Project 0 15

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; /* 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) */

G

e

n

e

r ic

I P

S

p

e

c

if

ic

Networks - Project 0 16

Overview of The

Connection Mechanism

socket()

TCP Client

connect()

write()

socket()

TCP Server

bind()

listen()

accept()

read()

write()

read()

close()

read()

write()

end-of-file notification

Connection establishment

Data (Request)

Data (Reply)

Networks - Project 0 19

proj0.c โ€“ the code:

if ( argc < 2 ) { printf( "The program expects arguments\n" ); printf( "tcp <client|server>\n" ); exit(0); }

if ((fd = socket (family, type, 0)) < 0) SysError ("Error on socket");

sa.sin_family = family; sa.sin_port = htons(port); /* client & server see same port/ sa.sin_addr.s_addr = htonl(INADDR_ANY); / the kernel assigns the IP addr*/

strcpy( console_buffer, argv[1] );

Check that the argument

was input.

Open a socket. The

socket descriptor is

returned in fd.

Fill in the structure that

defines how we want to

connect to other programs.

Networks - Project 0 20

proj0.c โ€“ the code:

if ( console_buffer[0] == 's' || console_buffer[0] == 'S' ) { if (bind (fd, (struct sockaddr *)&sa, sizeof(sa) ) == -1) SysError ("Error on bind");

if (listen (fd, SOMAXCONN) == -1) /* set up for listening */ SysError ("Error on listen");

fdListen = 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

proj0.c โ€“ the code:

while( TRUE ) { if ((fdConn = accept (fdListen, (struct sockaddr *)&sa, &lsa )) <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 ); 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" );

} /* End of while recv is successful / close (fdConn); } / End of while TRUE / } / End of server case */

Repeat forever

recv from client

Send back to the client

Calculate the new value

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

proj0.c โ€“ the code:

else { if (connect(fd, (struct sockaddr *)&sa, sizeof(sa) ) ) SysError ("Error on connect");

This is the else that says we did NOT ask for a server.

So the first thing a client

does is a connect to the

server.