Sie sind auf Seite 1von 28

INDUS INSTITUTE OF TECHNOLOGY & ENGINEERING Computer Engineering

Laboratory Manual Academic Year: July-2010 -2011 Semester: V

Computer Network (150702)

Prepared By: Mr. Vimalkumar B. Vaghela (Sr. Lecturer)

INDUS INSTITUTE OF TECHNOLOGY & ENGINEERING Computer Engineering


Subject: Computer Network Code: 150702 Laboratory Experiment List Semester: V Academic year: July 2010-2011

No. 1 2 3

Aim Write a C program to implement the file management concept. To compare OSI and TCP/IP protocol model. To demonstrate Networking and Internetworking Devices.(NIC, Switch, Hub, Router, Gateway, Repeater, Bridge, Cables)

Page 2 5 7

To implement straight through and cross over cable using cat 5 cable and RJ-45 connector.

10

5 6 7 8 9

Write a program which demonstrates the concept of PARITY CHECKING. To demonstrate different networking commands. Write a program which demonstrates the concept of bit stuffing. To implement I.P addressing scheme in LAN environment. Write a program which demonstrates the concept of Error Detection Methods like VRC, LRC.

12 13 14 15 16

10 11 12 13 14 15

Write a program which demonstrates the concept of Error Detection Method CRC. To Study various socket commands & to study socket programming in UNIX. To implement Echo server using TCP in Unix. To implement File Transfer using TCP in Unix. To implement Echo server using UDP in Unix. To implement File Transfer using UDP in Unix.

17 18 24 24 26 26

Computer Network (150702) Prepared By: Vimal B. Vaghela (Sr. Lecturer)

Page 1

INDUS INSTITUTE OF TECHNOLOGY & ENGINEERING Computer Engineering


Experiment 1

Aim: Write a C program to implement the file management concept. Software Required: C / C++ Hardware Required: ---Knowledge Required: Basic C Programming Theory: C supports a number of functions that have the ability to perform basic file operations, which include: 1. Naming a file 2. Opening a file 3. Reading from a file 4. Writing data into a file 5. Closing a file

Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two major problems It becomes cumbersome and time consuming to handle large volumes of data through terminals. The entire data is lost when either the program is terminated or computer is turned off therefore it is necessary to have more flexible approach where data can be stored on the disks and read whenever necessary, without destroying the data. This method employs the concept of files to store data. File operation functions in C: Function Nam fopen() Operation Creates a new file for use Opens a new existing file for use Closes a file which has been opened for use Reads a character from a file Writes a character to a file

fclose getc() putc()

Computer Network (150702) Prepared By: Vimal B. Vaghela (Sr. Lecturer)

Page 2

INDUS INSTITUTE OF TECHNOLOGY & ENGINEERING Computer Engineering fprintf() fscanf() getw() putw() fseek() ftell() rewind() Writes a set of data values to a file Reads a set of data values from a file Reads a integer from a file Writes an integer to the file Sets the position to a desired point in the file Gives the current position in the file Sets the position to the begining of the file

Example: /*Program to handle mixed data types*/

#include< stdio.h > main() { FILE *fp; int num,qty,I; float price,value; char item[10],filename[10]; printf(Input filename); scanf(%s,filename); fp=fopen(filename,w); printf(Input inventory datann0; printf(Item namem number price quantityn); for I=1;I< =3;I++) { fscanf(stdin,%s%d%f%d,item,&number,&price,&quality); fprintf(fp,%s%d%f%d,itemnumber,price,quality); } fclose (fp); fprintf(stdout,nn); fp=fopen(filename,r); printf(Item name number price quantity value); for(I=1;I< =3;I++)
Computer Network (150702) Prepared By: Vimal B. Vaghela (Sr. Lecturer) Page 3

INDUS INSTITUTE OF TECHNOLOGY & ENGINEERING Computer Engineering { fscanf(fp,%s%d%f%d,item,&number,&prince,&quality); value=price*quantity); fprintf(stdout,%s%d%f%d%dn,item,number,price,quantity,value); } fclose(fp); }

Computer Network (150702) Prepared By: Vimal B. Vaghela (Sr. Lecturer)

Page 4

INDUS INSTITUTE OF TECHNOLOGY & ENGINEERING Computer Engineering


Experiment 2

Aim: To compare OSI and TCP/IP protocol model. Software Required: ---Hardware Required: ---Knowledge Required: Knowledge about basic networking concepts like Line Configuration, Network Topology, Mode of Transmission, etc

Theory: OSI means Open System Interconnection model. This model is developed by ISO (International Standard Organization) in 1984. Open System is a model that allows any two different systems to communicate regardless of their underlying architecture. The purpose of OSI model is to open communication between different systems without requiring changes to the logic of underlying hardware and software. OSI model is not a protocol but it is a model for understanding and designing a network architecture that is flexible, robust and interoperable. OSI model consists of seven separate but related layers, each of which defines a segment of the process of moving information across the network. TCP/IP means Transmission Control Protocol / Internetworking Protocol model, sometimes called the DoD (Department of Defence) model or the ARPANET (Advance research Project Agency Network) reference model, is a layered abstract description for communications and computer network protocol design. It was created in the 1970 by DARPA (Defence Advance research Project Agency Network) for use in developing the Internet protocols. The structure of the Internet is still closely reflected by the TCP/IP model. TCP/IP is a hierarchical protocol made up of interactive modules each of which provides a specific functionality, but they are not necessarily interdependent. The term hierarchical means that each upper level protocol is supported by one or more lower level protocols.

Computer Network (150702) Prepared By: Vimal B. Vaghela (Sr. Lecturer)

Page 5

INDUS INSTITUTE OF TECHNOLOGY & ENGINEERING Computer Engineering

Program: ----Input/Output: ----Advantages: Disadvantages:

Conclusion: By using layered structure of the OSI model, the network architecture can be understood.

Question/Answer: 1) 2) 3) 4) Give the difference between Connection-Oriented and Connectionless approach. Give the difference between OSI and TCP/IP model. What is header? What is Trailer? What is the purpose of interface between each layer?

Computer Network (150702) Prepared By: Vimal B. Vaghela (Sr. Lecturer)

Page 6

INDUS INSTITUTE OF TECHNOLOGY & ENGINEERING Computer Engineering


Experiment 3

Aim: To demonstrate Networking and Internetworking Devices.(NIC, Switch, Hub, Router, Gateway, Repeater, Bridge, Cables) Software Required: ---Hardware Required: NIC, Switch, Hub, Router, Gateway, Repeater, Bridge, Cables Knowledge Required: TCP/IP model, IP address, Physical address. Theory: Hub: Hub (referred to as a concentrator) refers to a networking component which acts as a convergence point of a Network allowing the transfer of data packets. In its simplest form a hub works by duplicating the data packets received via one Port and making it available to all ports, therefore allowing data sharing between all devices connected to the hub. Three different types of hubs exist: 1) Passive Hub 2) Active Hub 3) Intelligent Hub

HUB SWITCH
Computer Network (150702) Prepared By: Vimal B. Vaghela (Sr. Lecturer) Page 7

INDUS INSTITUTE OF TECHNOLOGY & ENGINEERING Computer Engineering

Switch: Switches are another fundamental part of many networks because they speed things up. A network switch is a data switching device that forwards data packets from source network components to destination network components. Switches allow different nodes (a network connection point, typically a computer) of a network to communicate directly with one another in a smooth and efficient manner.

NIC (Network Interface Card): A network interface card is used by PC systems to connect to either an Ethernet network or a wireless network.

NIC Bridge:

BRIDGE

A bridge operates at the data link layer of OSI Reference Model and increases efficiency by filtering packets to reduce the amount of unnecessary packet propagation on each network segment. Repeater: A repeater is a hardware device that functions at the physical layer and re-transmits each received packet to every other port. Router: Routers are advanced networking components that can divide a single network into two logically separate networks. Router forms a logical boundary for the network.
Computer Network (150702) Prepared By: Vimal B. Vaghela (Sr. Lecturer) Page 8

INDUS INSTITUTE OF TECHNOLOGY & ENGINEERING Computer Engineering

Gateway: A gateway can translate information between different network data formats or network architectures. It can translate TCP/IP to AppleTalk so computers supporting TCP/IP can communicate with Apple brand computers. Most gateways operate at the application layer, but can operate at the network or session layer of the OSI model. Gateways will start at the lower level and strip information until it gets to the required level and repackage the information and work its way back toward the hardware layer of the OSI model. Program: ----Input/Output: ----Advantages: Disadvantages: Conclusion: In a computer network, network devices such as routers, hubs, switches, bridges, repeaters and others may be used to divide network traffic and boost network signals.

Question/Answer: 1) Give differences between Hub and Switch. 2) Give differences between Router and Bridge. 3) Give differences between Hub and Repeater. 4) List out the Layers name in which all devices are working.

Computer Network (150702) Prepared By: Vimal B. Vaghela (Sr. Lecturer)

Page 9

INDUS INSTITUTE OF TECHNOLOGY & ENGINEERING Computer Engineering


Experiment 4

Aim: To implement straight through and cross over cable using cat 5 cable and RJ45 connector. Software Required: --Hardware Required: RJ-45 connector, cat-5 cable, crimping tool Knowledge Required: Theory:

RJ-45 connectors - They usually come in bags of 50, 100 etc. and cost less than a quarter each. Pay attention to the type of RJ-45 connector you get and make sure it is intended for the type of Cat5 wire you're using. There are two different kind of RJ-45 connectors, depending on whether you use them with solid or stranded wire cable as mentioned above. Using the wrong kind with the wrong cable will most likely result in a bad connection.

Crimping tool - While this is the expensive part of making your own cables, it's only a one-time startup cost. They run anywhere from 10 to 50 bucks depending on the quality and features. Keep in mind that the crimpers will pay for themselves after you make a few cables. A good crimping tool has a pair of wire cutters built in, as well as a blade to strip insulation. It also might support crimping of other connectors such as RJ-11.

Straight through Cable (pc to pc) Sequence-1 Sequence-2 Orange White Green White Orange Green

Cross over cable (pc to hub) Sequence-1 Orange White Orange Sequences-2 Orange White Orange
Page 10

Computer Network (150702) Prepared By: Vimal B. Vaghela (Sr. Lecturer)

INDUS INSTITUTE OF TECHNOLOGY & ENGINEERING Computer Engineering Green White Blue Blue White Green Brown White Brown Orange White Blue Blue White Orange Brown White Brown Green White Blue Blue White Green Brown White Brown Green White Blue Blue White Green Brown White Brown

The sequence should be arranged from left to right and they should be inserted into connector with placing RJ-45 lock pin down side. If PC-to-PC connection is to be done then, at one end of cable there is sequence 1 then at the other end sequences 2 should be maintained. If PC-to-HUB connection is to be done then, at both end of cable there is sequence 1 or 2 should be maintained. When the cable arranged with these sequences is ready it is inserted into connector and fixed with the crimping tool. Now, connector is inserted into the female connector provided by NIC and connection established. Program: ----Input/Output: ----Advantages: Disadvantages:

Conclusion: By this practical we are able to prepare straight through and cross over cable. Question/Answer: 1) Explain RJ- 45 connector. 2) Give difference between straight through and cross over cable.

Computer Network (150702) Prepared By: Vimal B. Vaghela (Sr. Lecturer)

Page 11

INDUS INSTITUTE OF TECHNOLOGY & ENGINEERING Computer Engineering


Experiment 5 Aim: Write a program which demonstrates the concept of PARITY CHECKING.

Software Required: Turbo C++ Hardware Required: ---Knowledge Required: Types of Parity, Knowledge about how to write a C program, conversion of decimal value to binary and vice versa. Theory/Logic: There are two types of Parity. 1) Even Parity: Number of 1s in a value is Odd. For solving even parity we required to add 1 at the end of the data value. 2) Odd Parity: Number of 1s in a value is Even. For solving odd parity we required to add 1 at the end of the data value. Take one decimal value from user, convert it into equivalent binary value, check no. of 1s in to resultant binary value and get the original decimal value after solving parity (whether it is odd or even parity). Program: ----Input/Output: ----Advantages: Disadvantages: Conclusion: By using this program we can check whether the data received is correct or not at the destination. Question/Answer: 1) List types of Parity. 2) Why we required checking parity?
Computer Network (150702) Prepared By: Vimal B. Vaghela (Sr. Lecturer) Page 12

INDUS INSTITUTE OF TECHNOLOGY & ENGINEERING Computer Engineering


Experiment 6

Aim: To demonstrate different networking commands. Software Required: MS DOS Hardware Required: ---Knowledge Required: IP address, Physical address Theory: 1) ipconfig 2) ping {hostname} 3) arp 4) getmac 5) hostname 6) telnet {hostname} {port} 7) traceroute {hostname} 8) nslookup {hostname} 9) dig {hostname} 10) netstat a 11) netstat nt 12) host 13) whois 14) wget 15) curl 16) ssh 17) ftp 18) net view 19) net help 20) net send 21) Program: ----Input/Output: ----Advantages: Disadvantages: Conclusion: By using different types of command, we can get information about network. For example, ipconfig is used to find out IP address of device, ping command is used to know whether required device is alive or not. Question/Answer: 1) Which command is used to get IP and Physical address of a device? 2) Which command is used to get MAC address from IP address?

Computer Network (150702) Prepared By: Vimal B. Vaghela (Sr. Lecturer)

Page 13

INDUS INSTITUTE OF TECHNOLOGY & ENGINEERING Computer Engineering


Experiment 7 Aim: Write a program which demonstrates the concept of bit stuffing.

Software Required: Turbo C++. Hardware Required: ---Knowledge Required: ---Theory/Logic:

Step 1: Data input Step 2: count number of 1s from left side Step 3: if 5 consecutive 1s found then append 0 after it (6 location) Step 4: again count number of 1s from 6 location from data input Step 5: merge all the processed bits and send it
th th

Program: ----Input/Output: ----Advantages: Disadvantages: Conclusion: By using this program we can increase the speed by adding non information bits into data. Question/Answer: 1) What is Multiplexing? 2) What is Bit Rate? 3) Why we need Bit Stuffing?

Computer Network (150702) Prepared By: Vimal B. Vaghela (Sr. Lecturer)

Page 14

INDUS INSTITUTE OF TECHNOLOGY & ENGINEERING Computer Engineering


Experiment 8

Aim: To implement I.P addressing scheme in LAN environment. Software Required: ---Hardware Required: ---Knowledge Required: ---Theory: An IP address (Internet Protocol address) is a unique address that certain electronic devices currently use in order to identify and communicate with each other on a computer network utilizing the Internet protocol standard (IP)in simpler terms, a computer address. An IP address has two parts: 1) An identifier of a particular network on the Internet 2) An identifier of the particular device (which can be a server or a workstation) within that network. Class Type Net id Host id

Program: ----Input/Output: ----Advantages: Disadvantages: Conclusion: From this practical we come to know, how an IP address of the different class type is given to the device in a network. Question/Answer: 1) What is difference between Physical and Logical address? 2) What is difference between static and dynamic IP address? 3) Define Network address, Real IP address. 4) Explain Unicasting, Multicasting and Broadcasting.
Computer Network (150702) Prepared By: Vimal B. Vaghela (Sr. Lecturer) Page 15

INDUS INSTITUTE OF TECHNOLOGY & ENGINEERING Computer Engineering


Experiment 9

Aim: Write a program which demonstrates the concept of Error Detection Methods like VRC, LRC. Software Required: Turbo C++ Hardware Required: ---Knowledge Required: Types of Parity, Knowledge about how to write a C program, conversion of decimal value to binary and vice versa.

Theory/Logic:

VRC: In this technique, a redundant bit, called parity bit, is appended to every data unit so that number of 1s in the unit becomes even.

LRC: In this technique, bits are arranged in a table format (Row and Column). Then calculate the parity bit for each column and create new row, which are the parity bits for whole data. Like this way calculate parity bit for each column. Program: ----Input/Output: ----Advantages: Disadvantages: Conclusion: By using this program we are able to detect the error at the destination.

Question/Answer:
1) Write down the errors which is not detected by VRC,LRC. Computer Network (150702) Prepared By: Vimal B. Vaghela (Sr. Lecturer) Page 16

INDUS INSTITUTE OF TECHNOLOGY & ENGINEERING Computer Engineering


Experiment 10 Aim: Write a program which demonstrates the concept of Error Detection Method CRC.

Software Required: Turbo C++ Hardware Required: ---Knowledge Required: Types of Parity, Knowledge about how to write a C program, conversion of decimal value to binary and vice versa. Theory/Logic: CRC: In this technique, we have to use binary division for finding CRC. Step 1: String of N 0s is appended to data. N is one less than the number of bits in the divisor. Step 2: data is divided by the divisor using binary division process. Remainder is known as CRC. Step 3: CRC of N nits are replaces the appended 0s of the data. That is transmitted by the sender. Step 4: On the receiver side, data is divided by same divisor. And at last if remainder contains 0s means there is no error otherwise data with error. Program: ----Input/Output: ----Advantages: Disadvantages: Conclusion: By using this program we are able to detect the error at the destination.

Question/Answer: 1) Write down the errors which is not detected by CRC. 2) How is CRC superior to LRC?

Computer Network (150702) Prepared By: Vimal B. Vaghela (Sr. Lecturer)

Page 17

INDUS INSTITUTE OF TECHNOLOGY & ENGINEERING Computer Engineering


Experiment 11

AIM: To Study various socket commands & to study socket programming in UNIX. Software Required: Linux, GCC Hardware Required: ---Knowledge Required: Basic Knowledge of Network, Linux , Transport layer A socket is an abstraction of a communication endpoint. Just as they would use file descriptors to access a file, applications use socket descriptors to access sockets. Socket descriptors are implemented as file descriptors in the UNIX System To create a socket, we call the socket function. #include <sys/socket.h> int socket(int domain, int type, int protocol); Returns: file (socket) descriptor if OK, 1 on error Socket communication domains Domain AF_INET AF_INET6 AF_UNIX AF_UNSPEC Socket types Type SOCK_DGRAM SOCK_RAW SOCK_SEQPACKET Description fixed-length, connectionless, unreliable messages datagram interface to IP (optional in POSIX.1) fixed-length, sequenced, reliable, connectionPage 18

Description IPv4 Internet domain IPv6 Internet domain UNIX domain unspecified

Computer Network (150702) Prepared By: Vimal B. Vaghela (Sr. Lecturer)

INDUS INSTITUTE OF TECHNOLOGY & ENGINEERING Computer Engineering #include <sys/socket.h> int socket(int domain, int type, int protocol); oriented messages SOCK_STREAM sequenced, reliable, bidirectional, connectionoriented byte streams

Network protocols specify a byte ordering so that heterogeneous computer systems can exchange protocol information without confusing the byte ordering. The TCP/IP protocol suite uses bigendian byte order. The byte ordering becomes visible to applications when they exchange formatted data. With TCP/IP, addresses are presented in network byte order, so applications sometimes need to translate them between the processor 's byte order and the network byte order. Internet addresses are defined in <netinet/in.h>. In the IPv4 Internet domain (AF_INET), a socket address is represented by a sockaddr_in structure: struct in_addr { in_addr_t s_addr; };

/* IPv4 address */

struct sockaddr_in { sa_family_t sin_family; /* address family */ in_port_t sin_port; /* port number */ struct in_addr sin_addr; /* IPv4 address */ }; Associating Addresses with Sockets The address associated with a client's socket is of little interest, and we can let the system choose a default address for us. For a server, however, we need to associate a well-known address with the server's socket on which client requests will arrive. Clients need a way to discover the address to use to contact a server, and the simplest scheme is for a server to reserve an address and register it in /etc/services or with a name service. Use the bind function to associate an address with a socket.

Computer Network (150702) Prepared By: Vimal B. Vaghela (Sr. Lecturer)

Page 19

INDUS INSTITUTE OF TECHNOLOGY & ENGINEERING Computer Engineering #include <sys/socket.h> int bind(int sockfd, const struct sockaddr *addr, socklen_t len); Returns: 0 if OK, 1 on error There are several restrictions on the address we can use:

The address we specify must be valid for the machine on which the process is running; we can't specify an address belonging to some other machine. The address must match the format supported by the address family we used to create the socket. The port number in the address cannot be less than 1,024 unless the process has the appropriate privilege (i.e., is the superuser). Usually, only one socket endpoint can be bound to a given address, although some protocols allow duplicate bindings. Connection Establishment If we're dealing with a connection-oriented network service (SOCK_STREAM or SOCK_SEQPACKET), then before we can exchange data, we need to create a connection between the socket of the process requesting the service (the client) and the process providing the service (the server). We use the connect function to create a connection. #include <sys/socket.h> int connect(int sockfd, const struct sockaddr *addr, socklen_t len); Returns: 0 if OK, 1 on error The address we specify with connect is the address of the server with which we wish to communicate. If sockfd is not bound to an address, connect will bind a default address for the caller. When we try to connect to a server, the connect request might fail for several reasons. The machine to which we are trying to connect must be up and running, the server must be bound to the address we are trying to contact, and there must be room in the server's pending connect queue. Thus, applications must be able to handle connect error returns that might be caused by transient conditions
Computer Network (150702) Prepared By: Vimal B. Vaghela (Sr. Lecturer) Page 20

INDUS INSTITUTE OF TECHNOLOGY & ENGINEERING Computer Engineering A server announces that it is willing to accept connect requests by calling the listen function. #include <sys/socket.h> int listen(int sockfd, int backlog); Returns: 0 if OK, 1 on error The backlog argument provides a hint to the system of the number of outstanding connect requests that it should enqueue on behalf of the process. The actual value is determined by the system, but the upper limit is specified as SOMAXCONN in <sys/socket.h>. On Solaris, the SOMAXCONN value in <sys/socket.h> is ignored. The particular maximum depends on the implementation of each protocol. For TCP, the default is 128. Once the queue is full, the system will reject additional connect requests, so the backlog value must be chosen based on the expected load of the server and the amount of processing it must do to accept a connect request and start the service. Once a server has called listen, the socket used can receive connect requests. We use the accept function to retrieve a connect request and convert that into a connection. #include <sys/socket.h> int accept(int sockfd, struct sockaddr *restrict addr, socklen_t *restrict len); Returns: file (socket) descriptor if OK, 1 on error The file descriptor returned by accept is a socket descriptor that is connected to the client that called connect. This new socket descriptor has the same socket type and address family as the original socket (sockfd). The original socket passed to accept is not associated with the connection, but instead remains available to receive additional connect requests. If we don't care about the client's identity, we can set the addr and len parameters to NULL. Otherwise, before calling accept, we need to set the addr parameter to a buffer large enough to hold the address and set the integer pointed to by len to the size of the buffer. On return, accept will fill in the client's address in the buffer and update the integer pointed to by len to reflect the size of the address. If no connect requests are pending, accept will block until one arrives. If sockfd is in nonblocking mode, accept will return 1 and set errno to either EAGAIN or EWOULDBLOCK.
Computer Network (150702) Prepared By: Vimal B. Vaghela (Sr. Lecturer) Page 21

INDUS INSTITUTE OF TECHNOLOGY & ENGINEERING Computer Engineering If a server calls accept and no connect request is present, the server will block until one arrives. Alternatively, a server can use either poll or select to wait for a connect request to arrive. In this case, a socket with pending connect requests will appear to be readable. Data Transfer Since a socket endpoint is represented as a file descriptor, we can use read and write to communicate with a socket, as long as it is connected. Using read and write with socket descriptors is significant, because it means that we can pass socket descriptors to functions that were originally designed to work with local files. We can also arrange to pass the socket descriptors to child processes that execute programs that know nothing about sockets. Three functions are available for sending data, and three are available for receiving data. First, we'll look at the ones used to send data. The simplest one is send. It is similar to write, but allows us to specify flags to change how the data we want to transmit is treated. #include <sys/socket.h> ssize_t send(int sockfd, const void *buf, size_t nbytes, int flags); Returns: number of bytes sent if OK, 1 on error Like write, the socket has to be connected to use send. The buf and nbytes arguments have the same meaning as they do with write. The recv function is similar to read, but allows us to specify some options to control how we receive the data. #include <sys/socket.h> ssize_t recv(int sockfd, void *buf, size_t nbytes, int flags); Returns: length of message in bytes, 0 if no messages are available and peer has done an orderly shutdown, or 1 on error

Computer Network (150702) Prepared By: Vimal B. Vaghela (Sr. Lecturer)

Page 22

INDUS INSTITUTE OF TECHNOLOGY & ENGINEERING Computer Engineering Questions: 1. Explain the working of the following calls in detail. socket(), bind(), connect(), listen(), accept(), send(), recv() 2. What do you mean by Byte Ordering? Explain big endian and little endian byte ordering.

Computer Network (150702) Prepared By: Vimal B. Vaghela (Sr. Lecturer)

Page 23

INDUS INSTITUTE OF TECHNOLOGY & ENGINEERING Computer Engineering


Experiment 12 & 13

AIM: To implement Echo server using TCP in Unix. & AIM: To implement File Transfer using TCP in Unix. Software Required: Linux, GCC Hardware Required: ---Knowledge Required: Basic Knowledge of Network, Linux , Transport layer, TCP, Socket Functions. Use the elementary functions from the previous experiment to write a complete TCP client/server example. An echo server that performs the following steps: 1. The client reads a line of text from its standard input and writes the line to the server. 2. The server reads the line from its network input and echoes the line back to the client. 3. The client reads the echoed line and prints it on its standard output. Following figure depicts this simple client/server along with the functions used for input and output.

Following figure shows a timeline of the typical scenario that takes place between a TCP client and server. First, the server is started, then sometime later, a client is started that connects to the server. We assume that the client sends a request to the server, the server processes the request, and the server sends a reply back to the client. This continues until the client closes its end of the connection, which sends an end-of-file notification to the server. The server then closes its end of the connection and either terminates or waits for a new client connection. All clients and servers begin with a call to socket, returning a socket descriptor. Clients then call connect, while servers call bind, listen, and accept. Sockets are normally closed with the standard

Computer Network (150702) Prepared By: Vimal B. Vaghela (Sr. Lecturer)

Page 24

INDUS INSTITUTE OF TECHNOLOGY & ENGINEERING Computer Engineering close function. Most TCP servers are concurrent, with the server calling fork for every client connection that it handles

Question: Implement the above client server program using socket API of UNIX

Computer Network (150702) Prepared By: Vimal B. Vaghela (Sr. Lecturer)

Page 25

INDUS INSTITUTE OF TECHNOLOGY & ENGINEERING Computer Engineering


Experiment 14 & 15

AIM: To implement Echo server using UDP in Unix. & AIM: To implement File Transfer using UDP in Unix. Software Required: Linux, GCC Hardware Required: ---Knowledge Required: Basic Knowledge of Network, Linux , Transport layer, UDP, Socket Functions. There are some fundamental differences between applications written using TCP versus those that use UDP. These are because of the differences in the two transport layers: UDP is a connectionless, unreliable, datagram protocol, quite unlike the connection-oriented, reliable byte stream provided by TCP. Some popular applications are built using UDP: DNS, NFS, and SNMP, for example. Figure shows the function calls for a typical UDP client/server. The client does not establish a connection with the server. Instead, the client just sends a datagram to the server using the sendto function (described in the next section), which requires the address of the destination (the server) as a parameter. Similarly, the server does not accept a connection from a client. Instead, the server just calls the recvfrom function, which waits until data arrives from some client. recvfrom returns the protocol address of the client, along with the datagram, so the server can send a response to the correct client.

Computer Network (150702) Prepared By: Vimal B. Vaghela (Sr. Lecturer)

Page 26

INDUS INSTITUTE OF TECHNOLOGY & ENGINEERING Computer Engineering

Following two functions are similar to the standard read and write functions, but three additional arguments are required. #include <sys/socket.h> ssize_t recvfrom(int sockfd, void *buff, size_t nbytes, int flags, struct sockaddr *from, socklen_t *addrlen); ssize_t sendto(int sockfd, const void *buff, size_t nbytes, int flags, const struct sockaddr *to, socklen_t addrlen); Both return: number of bytes read or written if OK, 1 on error The first three arguments, sockfd, buff, and nbytes, are identical to the first three arguments for read and write: descriptor, pointer to buffer to read into or write from, and number of bytes to read or write. Question: Implement UDP client server using UNIX socket API

Computer Network (150702) Prepared By: Vimal B. Vaghela (Sr. Lecturer)

Page 27

Das könnte Ihnen auch gefallen