0% found this document useful (0 votes)
10 views32 pages

Lab 2 Network Programming in Linux (Part 1)

The document outlines the objectives and procedures for Lab 2 of the EE353 Computer Networks course, focusing on network programming in Linux. It includes instructions for partitioning a hard drive for Ubuntu, creating a bootable USB drive, and installing Ubuntu alongside Windows 10. Additionally, it covers basic commands for package installation, file management, and client-server communication using sockets in C programming.

Uploaded by

ahmadxaidi2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views32 pages

Lab 2 Network Programming in Linux (Part 1)

The document outlines the objectives and procedures for Lab 2 of the EE353 Computer Networks course, focusing on network programming in Linux. It includes instructions for partitioning a hard drive for Ubuntu, creating a bootable USB drive, and installing Ubuntu alongside Windows 10. Additionally, it covers basic commands for package installation, file management, and client-server communication using sockets in C programming.

Uploaded by

ahmadxaidi2004
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Department of Computing

EE353: Computer Networks


Class: BESE
Lab 2: Network Programming in Linux (Part 1)

Date: 19-09-2024
Time: 10:00 to 12:50 and 2:00 to 4:50
Instructor: Ms. Mehwish Kiran

Lab Engineer: Mr. Hafiz Arslan Ramzan

EE353: Computer Networks Page 1


Lab 1: Network Programming in Linux (Part 2)

Introduction/Objectives:
After this lab, the students should be able to
● Explain the concepts of client server communication

● Setup client/server communication

● Use the sockets interface of C programming language

● Implement simple Client / Server applications

Tools/Software Requirements
Ubantu
Description

How to Partition your Hard Drive for Ubuntu

You have to partition your hard drive because you must set aside at least 20Gig
for Ubuntu to live in and boot from.

To partition your hard drive, follow the steps below:

EE353: Computer Networks Page 2


Step 1: Right-click on Start and select “Disk Management”.

EE353: Computer Networks Page 3


Step 2: Right-click on your C drive and select shrink volume.

EE353: Computer Networks Page 4


Step 3: Select at least (20000) 20Gig for Ubuntu and click “Shrink”. IT can
take some time to complete, so be patient.

EE353: Computer Networks Page 5


Step 4 (optional): You can go ahead and assign a letter to the new volume.
Right-click on the unallocated space and select “New Simple Volume”.

EE353: Computer Networks Page 6


Step 5: Follow the wizard and assign a letter to the drive, then follow the rest of
it.

EE353: Computer Networks Page 7


After completing the wizard, the drive should be listed on your computer.

Congrats! You’ve successfully partitioned your hard drive.

N.B.: If you have a lot of free space in your hard drive but your PC still didn’t
give you up to 20Gig partition space, then you need to optimize your PC’s hard
drive. Proceed to the next section of this article to do that.

How to Optimize your Hard Drive for More Partition Space (Optional)

The common purpose of hard disk optimization is to speed up your computer


during boot time and make it run smoother.

EE353: Computer Networks Page 8


At the same time, the process will defragment the hard disk and make free space
more available for partitioning.

To optimize your hard drive, click Start (Windows logo key), search for
“defrag” and select “Defragment and Optimize Drives”.

EE353: Computer Networks Page 9


Make sure your C drive is highlighted, then click “Optimize”.

After you’ve been able to set aside at least 20Gig for Ubuntu by partitioning
your hard drive, then it’s time to download Ubuntu and make a bootable USB.

How to Download Ubuntu in ISO Image Format

EE353: Computer Networks Page 10


The next thing is to download Ubuntu in ISO image format so you can install
Ubuntu. You can download it from the Ubuntu distro website.

After downloading Ubuntu, don’t do anything with it yet. You need to make a
bootable USB and put it in there. That’s how you’ll be able to use it.

The reason you can’t install Ubuntu just like that is that it doesn’t come as an
executable. It comes in form of ISO (optical disk image). That means you have
to find a disk to put it in before it can work.

The next part of this guide shows how you can put the downloaded Ubuntu ISO
on a USB stick.

How to Make an Ubuntu (Linux) Bootable USB Drive

You won’t be able to make a bootable USB drive for Ubuntu by just placing the
downloaded ISO image in it. Follow these steps to get it done:

EE353: Computer Networks Page 11


Step 1: You need to download a bootable USB drive creation tool like Rufus.
You can download Rufus from their website.

Step 2: Insert the empty USB drive into your Windows 10 PC. Right-click on
Rufus and select “Open”.

Step 3: Under “Device”, select your USB drive. And under “Boot selection”,
click the “Select” button and select the Ubuntu ISO file you downloaded

EE353: Computer Networks Page 12


Step 4: Leave every other thing as default and click the “START” button to start
burning the Ubuntu distro to the drive.

EE353: Computer Networks Page 13


Step 5: Click OK to start the process.

EE353: Computer Networks Page 14


Once the process is done, you should see “READY” on a green background.
Click the Close button. Its time to install Ubuntu.

Congrats! Now you have a bootable drive with which you can install Linux.

The next step is to install the Ubuntu distro on your Windows 10 PC. To do this,
you have to boot your PC from the bootable USB drive you created.

How to Install Ubuntu Linux Distro along with Windows 10

Step 1: Ensure the bootable drive is inserted into your Windows 10 PC

Step 2: Right-click on Start, hold SHIFT, and select Restart.

EE353: Computer Networks Page 15


Step 2: Select “Use a Device”.

Step 3: On the next screen, you should see several devices you can boot from.

You may see the bootable drive as the name of the USB brand.

EE353: Computer Networks Page 16


It’s possible to see it as “Ubuntu” too. Some other times, you might not see it,
so you need to click on “See more devices".

If you still cannot see your bootable drive, head over to your boot menu by
getting into BIOS. You will see it there.

N.B.: You should be very careful while making changes in BIOS. Whatever you
do there has a lasting effect on your computer. If you are not sure of what you're
doing there, you should contact an IT professional.

Step 4: Choose "Install Ubuntu". You can also try it before installing it.

Follow other prompts of the installation wizard and make sure you don’t replace
your Windows 10 OS installation with Ubuntu. This is why I suggested you
back up all your files.

EE353: Computer Networks Page 17


When you get to the point to select the partition you made, scroll to the partition
you made earlier and press ENTER.

Click OK to select all the space in the partition.

EE353: Computer Networks Page 18


This time around, the “Install now” button will not be greyed out anymore.

Follow other prompts until Ubuntu starts installing.

After the installation is done, Ubuntu will prompt you to remove the bootable
drive and press ENTER to reboot your Computer.

Now You Can Dual Boot Ubuntu and Windows 10

Immediately after you reboot the computer, you should see a screen that looks
as shown below:

Now, you can select which one to boot into between Ubuntu and Windows 10.

EE353: Computer Networks Page 19


To boot into Ubuntu, select Ubuntu. And to boot into Windows 10, select
Windows boot manager.

You can also get into your BIOS from the same place by choosing UEFI
Firmware Settings.

Task:

Installing packages in Ubuntu


To install any package in ubuntu u have to open the terminal

EE353: Computer Networks Page 20


EE353: Computer Networks Page 21
For Package installation

Removing any package

Try following commands in your terminal

ls: Lists files and directories in the current directory.

ls

cd: Changes the current directory.

cd /path/to/directory

pwd: Prints the current working directory.

pwd

mkdir: Creates a new directory.

mkdir directory_name

EE353: Computer Networks Page 22


rmdir: Removes an empty directory.

rmdir directory_name

rm: Removes files or directories (use -r for recursive delete).

rm file_name
rm -r directory_name

cp: Copies files or directories.

cp source_file destination
cp -r source_directory destination_directory

mv: Moves or renames files or directories.

mv old_name new_name
mv file_name /path/to/destination/

File Viewing and Editing

cat: Displays the content of a file.

cat file_name

nano: A simple text editor.

nano file_name

sudo apt-get update

apt-get upgrade: Upgrades installed packages to the latest version.

sudo apt-get upgrade

Search and Find

grep: Searches text using patterns.

grep 'pattern' file_name

EE353: Computer Networks Page 23


find: Finds files and directories.

find /path -name file_name

Compression and Archiving

tar: Archives multiple files into a single file and extracts them.

tar -cvf archive_name.tar directory/


tar -xvf archive_name.tar

gzip: Compresses or decompresses files.

gzip file_name
gunzip file_name.gz

Miscellaneous

echo: Prints text to the terminal.

echo "Hello, World!"

history: Shows the command history.

history

clear: Clears the terminal screen.

clear

Application programming interface:

EE353: Computer Networks Page 24


An application programming interface (API) is a specification intended to be
used as an interface by software components to communicate with each other.
Network Application Programming Interface:
The place to start when implementing a network application is the 'interface
exported by network'. Generally all operating systems provide an interface to its
networking sub system. This interface is called as the 'Network Application
Programming Interface' (Network API) or socket interface.
Network Sockets:
A network socket is an endpoint of an inter-process communication
flow across a computer network. Today, most communication between
computers is based on the Internet Protocol; therefore most network sockets
are Internet sockets.

What is a SOCKET?
In layman’s term, a Socket is an end point of communication between two
systems on a network. To be a bit precise, a socket is a combination of IP
address and port on one system. So on each system a socket exists for a process
interacting with the socket on other system over the network.
TCP/IP networking model is the most popular and widely used.The
communication over the network in TCP/IP model takes place in form of a
client server architecture. ie, the client begins the communication and server
follows up and a connection is established.

Client/Server Communication
At a basic level, network-based systems consist of a server, client, and a media
for communication as shown in Figure 1. A computer running a program that
makes a request for services is called client machine. A computer running a
program that offers requested services from one or more clients is called server
machine. The media for communication can be wired or wireless network.

EE353: Computer Networks Page 25


Figure 1. Client server communication

Generally, programs running on client machines make requests to a program


(often called as server program) running on a server machine. They involve
networking services provided by the transport layer, which is part of the Internet
software stack, often called TCP/IP (Transport Control Protocol/Internet
Protocol) stack, the transport layer comprises two types of protocols, TCP
(Transport Control Protocol) and UDP (User Datagram Protocol)

Port Numbers
At any given time, multiple processes can be using any given transport layer
protocol: UDP or TCP. The transport layer uses 16-bit integer port numbers to
differentiate between these processes. When a client wants to contact a server,
the client must identify the server with which it wants to communicate. The
TCP and UDP protocols use ports to map incoming data to a particular process
running on a computer. Some ports have been reserved to support common/well
known services:
ftp 21/tcp
telnet 23/tcp
smtp 25/tcp
http 80/tcp,udp
User-level process/services generally use port number value >= 1024.

But have you ever given a thought over how two processes communicate across
a network?

For example, when you browse a website, on your local system the process
running is your web browser, while on the remote system the process running is
the web server. So this is also an inter process communication but the technique
through which they communicate with each other is SOCKETS, which is the
focus of this article.

EE353: Computer Networks Page 26


Socket Server Example
This server continuously runs and sends the date and time to client as soon as a
client connects to it.
#include <sys/socket.h> //For socket Creation
#include <netinet/in.h> //For handling internet addresses
#include <arpa/inet.h> //For Ip address manipulation
#include <stdio.h> //Standard Input out put function
#include <stdlib.h> //Standard library functions for example exit
#include <unistd.h> //Provide System function like sleep close etc
#include <errno.h> //For error handling and codes
#include <string.h> //For enabling string functions
#include <sys/types.h> //for sys calls low level operations in OS
#include <time.h> //For enabling time related functions

int main(int argc, char *argv[])


{
int listenfd = 0, connfd = 0;
struct sockaddr_in serv_addr; //Structure to hold Server address info
char sendBuff[1025]; //Buffer to store data to be sent to client
time_t ticks; //Store System time
listenfd = socket(AF_INET, SOCK_STREAM, 0); //Creates Socket
AF_INET //IPV4 and Sock_stream using TCP
memset(&serv_addr, '0', sizeof(serv_addr)); //Initialize buffer with zero
memset(sendBuff, '0', sizeof(sendBuff));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(5000);
bind(listenfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
listen(listenfd, 10);
while(1)
{
connfd = accept(listenfd, (struct sockaddr*)NULL, NULL); //Wait for a
client to connect
ticks = time(NULL); //Getting System Time
snprintf(sendBuff, sizeof(sendBuff), "%.24s\r\n", ctime(&ticks)); //sending
date and time to client
write(connfd, sendBuff, strlen(sendBuff)); // Send the time to connected
client

EE353: Computer Networks Page 27


close(connfd); //Closes the connection with the client
sleep(1);
}
}

▪ The call to the function ‘socket()’ creates an UN-named socket inside the
kernel and returns an integer known as socket descriptor.

▪ This function takes domain/family as its first argument. For Internet family
of IPv4 addresses we use AF_INET.

▪ The second argument ‘SOCK_STREAM’ specifies that the transport layer


protocol that we want should be reliable ie it should have acknowledgement
techniques. For example : TCP

▪ The third argument is generally left zero to let the kernel decide the default
protocol to use for this connection. For connection oriented reliable
connections, the default protocol used is TCP.

▪ The call to the function ‘bind()’ assigns the details specified in the structure
‘serv_addr’ to the socket created in the step above. The details include, the
family/domain, the interface to listen on(in case the system has multiple
interfaces to network) and the port on which the server will wait for the
client requests to come.

▪ The call to the function ‘listen()’ with second argument as ’10’ specifies
maximum number of client connections that server will queue for this
listening socket.

▪ After the call to listen(), this socket becomes a fully functional listening
socket.

▪ In the call to accept(), the server is put to sleep and when for an incoming
client request, the three way TCP handshake* is complete, the function
accept () wakes up and returns the socket descriptor representing the client
socket.

EE353: Computer Networks Page 28


▪ The call to accept() is run in an infinite loop so that the server is always
running and the delay or sleep of 1 sec ensures that this server does not eat
up all of your CPU processing.

▪ As soon as server gets a request from client, it prepares the date and time
and writes on the client socket through the descriptor returned by accept().

Three way hand shake is the procedure that is followed to establish a TCP
connection between two remote hosts.
Finally, we compile the code and run the server.

Socket Client Example


#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <arpa/inet.h>
int main(int argc, char
*argv[])
{
int sockfd = 0, n = 0;
char recvBuff[1024];
struct sockaddr_in serv_addr;
if(argc != 2)
{
printf("\n Usage: %s <ip of server> \n",argv[0]);
return 1;
}
memset(recvBuff, '0',sizeof(recvBuff));
if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
printf("\n Error : Could not create socket \n");
return 1;

EE353: Computer Networks Page 29


}
memset(&serv_addr, '0', sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(5000);
if(inet_pton(AF_INET, argv[1], &serv_addr.sin_addr)<=0)
{
printf("\n inet_pton error occured\n");
return 1;
}
if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
printf("\n Error : Connect Failed \n");
return 1;
}
while ( (n = read(sockfd, recvBuff, sizeof(recvBuff)-1)) > 0)
{
recvBuff[n] = 0;
if(fputs(recvBuff, stdout) == EOF)
{
printf("\n Error : Fputs error\n");
}
}
if(n < 0)
{
printf("\n Read error \n");
}
return 0;
}
In the above program, we create a client which will connect to the server and
receive date and time from it. In the above piece of code:
▪ We see that here also, a socket is created through call to socket() function.

▪ Information like IP address of the remote host and its port is bundled up in a
structure and a call to function connect() is made which tries to connect this
socket with the socket (IP address and port) of the remote host.

▪ Note that here we have not bind our client socket on a particular port as
client generally use port assigned by kernel as client can have its socket
associated with any port but In case of server it has to be a well-known
EE353: Computer Networks Page 30
socket, so known servers bind to a specific port like HTTP server runs on
port 80 etc. while there is no such restrictions on clients.
▪ Once the sockets are connected, the server sends the data (date+time) on
clients socket through clients socket descriptor and client can read it through
normal read call on the its socket descriptor.

We can see that we successfully got the date and time from server. We need to
send the IP address of the server as an argument for this example to run. If you
are running both server and client example on the same machine for testing
purpose, use the loop back ip address as shown above.

How to RUN C files in Ubuntu

Open terminal

Compiling and running C files on ububtu


● Open text editor in Ubuntu and paste server code and save with c
extension
● Create another c file name Client .c and paste client code in it.

● Now open two terminals run server.c code on first terminal and client.c
on the other.
● Following are the terminal commands to compile and Run server side C
code

⮚ g++ server.c -o serv.out


for executing
⮚ ./serv.out

● Now one second terminal complete and execute the client as shown
below.
⮚ g++ client.c -o cli.out
for executing

EE353: Computer Networks Page 31


⮚ ./cli.out 127.0.0.1

Task:
Perform all the steps about and run both servers and client code on same
computer.

Deliverable
A Word Document containing Client and Server codes, Explanation of function
used for Networking e.g socket ,listen, bind, accept etc and screen shots of
output.

EE353: Computer Networks Page 32

You might also like