Lab 2 Network Programming in Linux (Part 1)
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
Introduction/Objectives:
After this lab, the students should be able to
● Explain the concepts of client server communication
Tools/Software Requirements
Ubantu
Description
You have to partition your hard drive because you must set aside at least 20Gig
for Ubuntu to live in and boot from.
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)
To optimize your hard drive, click Start (Windows logo key), search for
“defrag” and select “Defragment and Optimize Drives”.
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.
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.
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:
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
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.
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.
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.
After the installation is done, Ubuntu will prompt you to remove the bootable
drive and press ENTER to reboot your Computer.
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.
You can also get into your BIOS from the same place by choosing UEFI
Firmware Settings.
Task:
ls
cd /path/to/directory
pwd
mkdir directory_name
rmdir directory_name
rm file_name
rm -r directory_name
cp source_file destination
cp -r source_directory destination_directory
mv old_name new_name
mv file_name /path/to/destination/
cat file_name
nano file_name
tar: Archives multiple files into a single file and extracts them.
gzip file_name
gunzip file_name.gz
Miscellaneous
history
clear
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.
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.
▪ 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 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.
▪ 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.
▪ 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.
Open terminal
● 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
● Now one second terminal complete and execute the client as shown
below.
⮚ g++ client.c -o cli.out
for executing
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.