You are on page 1of 0

Publishedbywww.myignou.

in
Visitourwebsitetodownloadmoresolvedassignments.

Uploaded by : Pankaj Sethi


Call : 9781453353
Email : sethi_pankaj@yahoo.in
Course Code : BCSL-056
Title : Network Programming and Administration Lab
Assignment Number : BCA(V)-056/Assign/13-14
Maximum Marks : 50
Weightage : 25%
Last date of Submission : 15
th
October, 2013/ 15
th
April, 2014


Note: This assignment has two questions. Answer all the questions. These questions carry 40 marks.
Rest 10 marks are for viva voce.

Question 1:

Write a TCP client and TCP server program in C language, where the server can exchange text with many
client processes. A client process starts the communication with an input start. After this the client process
waits for the answer from the server. If server permits, it can further send any text message (with restriction of
not more than 100 sentences in an hour). The communication goes on in this way until the client process
sends the message "stop" to the server.
(20 Marks)

Answer1
CLIENT.C
#i ncl ude <st di o. h>
#i ncl ude <st dl i b. h>
#i ncl ude <uni st d. h>
#i ncl ude <er r no. h>
#i ncl ude <st r i ng. h>
#i ncl ude <net db. h>
#i ncl ude <sys/ t ypes. h>
#i ncl ude <net i net / i n. h>
#i ncl ude <sys/ socket . h>

/ / t he por t cl i ent wi l l be connect i ng t o
#def i ne PORT 3490
/ / max number of byt es we can get at once
#def i ne MAXDATASI ZE 300

i nt mai n( i nt ar gc, char *ar gv[ ] )
{
i nt sockf d, numbyt es;
char buf [ MAXDATASI ZE] ;
Publishedbywww.myignou.in
Visitourwebsitetodownloadmoresolvedassignments.

st r uct host ent *he;


/ / connect or s addr ess i nf or mat i on
st r uct sockaddr _i n t hei r _addr ;

/ / i f no command l i ne ar gument suppl i ed
i f ( ar gc ! = 2)
{
f pr i nt f ( st der r , " Cl i ent - Usage: %s t he_cl i ent _host name\ n" , ar gv[ 0] ) ;
/ / j ust exi t
exi t ( 1) ;
}

/ / get t he host i nf o
i f ( ( he=get host byname( ar gv[ 1] ) ) == NULL)
{
per r or ( " get host byname( ) " ) ;
exi t ( 1) ;
}
el se
pr i nt f ( " Cl i ent - The r emot e host i s: %s\ n" , ar gv[ 1] ) ;

i f ( ( sockf d = socket ( AF_I NET, SOCK_STREAM, 0) ) == - 1)
{
per r or ( " socket ( ) " ) ;
exi t ( 1) ;
}
el se
pr i nt f ( " Cl i ent - The socket ( ) sockf d i s OK. . . \ n" ) ;

/ / host byt e or der
t hei r _addr . si n_f ami l y = AF_I NET;
/ / shor t , net wor k byt e or der
pr i nt f ( " Ser ver - Usi ng %s and por t %d. . . \ n" , ar gv[ 1] , PORT) ;
t hei r _addr . si n_por t = ht ons( PORT) ;
t hei r _addr . si n_addr = *( ( st r uct i n_addr *) he- >h_addr ) ;
/ / zer o t he r est of t he st r uct
memset ( &( t hei r _addr . si n_zer o) , ' \ 0' , 8) ;

i f ( connect ( sockf d, ( st r uct sockaddr *) &t hei r _addr , si zeof ( st r uct
sockaddr ) ) == - 1)
{
per r or ( " connect ( ) " ) ;
exi t ( 1) ;
}
el se
pr i nt f ( " Cl i ent - The connect ( ) i s OK. . . \ n" ) ;
printf("\nTypeastring:(STOPtoendingconnection)\n\n");
Publishedbywww.myignou.in
Visitourwebsitetodownloadmoresolvedassignments.

do{
while((c=getchar())!='\n'&&i<MAXLENGHT){
buf[i++]=c;
}
buf[i]='\0';
len=strlen(buf);
}
while(strcmp(buf,"STOP")!=0);
close(sockfd);
exit(0);
}
}

SERVER. C
intmain(intargc,char**argv){
intsock,fd;
socklen_tclient_len;
structsockaddr_inserver,client;

if((sock=socket(AF_INET,SOCK_STREAM,0))==1){
perror("Socketfailed");
exit(1);
}

server.sin_family=AF_INET;
server.sin_addr.s_addr=htonl(INADDR_ANY);
server.sin_port=htons(SERVER_PORT);

if(bind(sock,(structsockaddr*)&server,sizeofserver)==1){
perror("bindfailed");
exit(2);
}

listen(sock,1);

while(1){
client_len=sizeof(client);
if((fd=accept(sock,(structsockaddr*)&client,&client_len))<0){
perror("acceptingconnection");
exit(3);
}
fprintf(stderr,"\nOpen.\n");
Publishedbywww.myignou.in
Visitourwebsitetodownloadmoresolvedassignments.

send(fd,"\nWelcome!\n",50,0);
check_stats(fd,fd);}

close(fd);
fprintf(stderr,"\nClose\n");
exit(0);

Question 2:

a) Write a step by step procedure to configure a remote server in Linux and transfer a Directory to
Remote Server.
(10 Marks)

b) Write a step by step procedure to create and configure samba Server in Linux. Also, transfer files from
client side.
(10 Marks)

Answer2
Connecting to a remote Linux server
To make a connection to your remote server:
1. Switch to the Remote System Explorer perspective. From the workbench menu, click Window >Open
Perspective >Remote System Explorer.
2. In the Remote Systems view, New Connection is automatically expanded to show the various remote systems
you can connect to through the Remote System Explorer. Expand Linux or Unix to invoke the new connection
dialog box and configure a connection.
3. Enter a name for your first profile and click Next. (This step only occurs if you have never defined a connection
before.)
4. Enter a connection name. This name displays in your tree view and must be unique to the profile.
5. Enter the name or TCP/IP address of your Linux server in the Host name field, for example, LINUX_A.
6. (Optional) Enter a Description. The description appears in the Properties view after the connection is created.
7. Click Finish to define your system.

TotransferaDirectorytoRemoteServer.
scprC:/siteuser@server_ip:path
path is the place, where site will be copied into the remote server


Publishedbywww.myignou.in
Visitourwebsitetodownloadmoresolvedassignments.

(B)
Setting Up A Linux File Server Using Samba

A quick Google will show many ways to to setup a Linux file server running Samba, most of them however don't
work! Some leave out important bits leaving you stuck and some will only work with one version of a specific Distro
(but of course don't mention this). I struggled for ages getting Samba to work reliably and made quite a few wrong
turns on the way. I was just trying to set up a simple Linux file server to store music, photos etc. but eventually
found a foolproof (probably) way to do it. The following works and has been tested several times on fresh
installations. This is not meant to be a high security setup, all folders are accessible to everybody for read, write and
delete. If you have stroppy teenagers who want exclusive access to their own area on the server, then you can use
this as a starting point. A few simple changes would achieve that level of security but it is beyond the scope of this
tutorial.
You could of course setup a Windows box running file sharing but there are some good reasons not to. Windows
would need constant monitoring to ensure updates are installed (requiring lots of reboots). Windows is very
vulnerable to viruses so your virus software will need kept up to date. Windows has no option to setup folder
security so if you find you need it in the future you'll have to start again from scratch, Windows is not as stable. I
setup a Linux server in 2007 and it ran non stop for 3 years. I didn't have to reboot it even when the Windows PC's
got viruses. In fact the Windows files stored on the server were infected with the virus but the Linux operating
system was unaffected. Ubuntu Server can be installed on any old PC you have lying around, even a 486. There are
other reasons too but enough talk, lets get started, if all goes well this should take no more than an hour or two.
NB The following Tutorial assumes you have a working network with a router providing a connection to the Internet
for both your server and client PC's.

Step1
Download Ubuntu Server and create the boot CD, then download the corresponding Ubuntu Server Guide
from https://help.ubuntu.com/. I will be referring to this guide a lot so print it off or keep it open on another PC.
** It is important you use the guide written for your version of Ubuntu Server as some commands change between
versions (this was one of the issues I had with other tutorials). This may also cause problems if you upgrade to a
new version of Ubuntu so I suggest you use the most recent LTS (long term support) version and stick to it.
Boot off the CD and follow installation instructions. I selected Guided - Use entire Disk. I did not select LVM as I
had issues retrieving data from an earlier attempt, where I screwed up the interfaces file and it wouldn't boot (yes I
know you can mount LVM from a Live Disk but I can't be bothered with the hassle) this is a simple home server so I
like to keep everything as simple as possible.
Give your server a name, create a user and set a sensible password. (this will also be your admin password so it
should include capitals and symbols or numbers), I'll explain more about this later.
**** NB When creating the user make it something like serveradmin. Do not make this username the same as your
Windows username. If you do it may cause issues later.****
Select Samba and SSH during the installation procedure by moving up and down with the arrow keys and pressing
the space bar. Use the Tab key to skip to the next or back buttons. Now continue with the installation and in a few
minutes you will be presented with the Ubuntu Server login screen. If at this stage you were expecting a graphical
Publishedbywww.myignou.in
Visitourwebsitetodownloadmoresolvedassignments.

user interface with shiny buttons and a mouse then you will be disappointed. This can be installed if you really wish
but that defeats the purpose of using the server edition of Ubuntu. On this version of Ubuntu you will have to get
your hands dirty on the keyboard.

Step 2
Login with your user name and password.
Install updates :-
sudo apt-get update
This just ensures everything is up to date.
You should be prompted for a password. Use the one you created earlier.

Step 3
In the Ubuntu Server Guide you downloaded earlier look for the chapter on Windows Networking and follow the
instructions for Samba File Server.
NB You shouldn't need to run the install command for Samba as it was installed earlier but if you have run it and are
being asked which version of smb.conf you want, I suggest you keep the one already installed. This is especially
true if you have already made changes to it.
** Before you start editing the smb.conf file, copy it to something like smb.conf_backup. You will probably cock
things up the first time you attempt this so it is nice to be able to get back to square one. :-
sudo cp /etc/samba/smb.conf /etc/samba/smb.conf_backup
I use Nano for editing text files from the CLI (command line interface). I hate Vi with a passion, that may be cause
I'm a bit thick, but I find Nano much easier to use. So your command to edit the smb.conf file will be :-
sudo nano /etc/samba/smb.conf
You need to be an admin user to save any changes when you're finished editing, hence the sudo in front of nano.
In Ubuntu any command which may change the server configuration needs to be carried out by the admin user.
Since for security reasons you cannot login as root (the admin user) the alternative is to insert sudo before every
command then provide the admin password, this keeps things nice and secure. Other Distros allow you to type su
(for super user) then the password and this will give admin access until you logout.
Check the workgroup name in your Windows PC by right clicking on My Computer, select properties and Computer
name. It will probably be WORKGROUP or MSHOME, depending on your version of XP. Sorry not sure how to find out in
Vista or W7 but probably something similar.
Publishedbywww.myignou.in
Visitourwebsitetodownloadmoresolvedassignments.

Now follow the instructions carefully. Some of the commands are already in the smb.conf file but have a # symbol
in front of them, removing the # symbol makes the command active.
To close Nano and save your changes press CTRL and X, type y and press enter. CTRL and O is shown as Write out
which just means SAVE, if you want to save it as you go along.
NB. Pretty much everything in Linux is case sensitive, i.e. If you gave your folder names capitals when creating,
them then you MUST use capitals when referring to them or moving around the file structure. Linux will see Music
and music as two completely different folders.

Step 4
This bit may or may not be necessary, as far as I can see it depends on the version of Ubuntu Server and whether
or not you installed Samba as part of the server installation. To connect from either a Linux or Windows PC you
need to make one more small change. Look for the area in smb.conf marked Authentication, and just below the bit
where you set security to user, change:-
encr ypt passwor ds = f al se
to
encr ypt passwor ds = t r ue
Save it, restart Samba by the method suggested for your version of Ubuntu Server (it seems to change between
versions) or just reboot the server and you should done.

Step 5
Now to the bit nobody else mentions in their tutorials and I had to figure out for myself. How do I connect to the
bugger?!!! I tried browsing "Network Connections" but that didn't work, possibly because at this point we haven't
created Samba users to match our Windows users but anyhow we don't need to. Here is how I did it.
Note you experts out there may criticize my way of doing this but it's simple and it works so don't knock it.
If not sure of your server IP address just type ifconfig at the command prompt. This will give you a full breakdown
of your network details.
From a Win XP Pro machine (sorry not sure it this is the same for other versions of Windows but it should be
similar).
Open My Computer.
Click on Tools and Map network Drive.
In the window labeled Folder, enter the IP address of your server preceded by \\ and followed
by \sharename (where sharename is the name of the share you created earlier).
e.g. :- \\192.168.1.4\Music or \\192.168.0.5\Photos
Press Finish and if everything has worked you should now see the network folder. You can create a shortcut to it
for easy access in the future but Windows should remember the network drive name you just created.
To connect from a Linux PC:- (I am using Ubuntu 10.10, sorry not sure about other Distros but other versions of
Ubuntu should be similar)
Click on Places and select Connect to Server.
Select Windows Share and type in the server IP address in the window labeled Server.
Publishedbywww.myignou.in
Visitourwebsitetodownloadmoresolvedassignments.

Enter the share name you created, put a tick in create bookmark (this just makes Ubuntu remember the path after
logout or reboot) and click on connect. Do not enter any username or password, it shouldn't be necessary if you
followed the guide earlier.
And that's it. Yippee you now have a working file server.

Step 5
During installation you installed SSH and having SSH running on your server in default mode can be a security risk,
so we want to sort that. We need to change the default port to a random number of your own choosing. To do this
follow the instructions in the Ubuntu Server Guide under the chapter Remote Administration. The default port is
22 which many people don't bother changing so this is the one used by hackers and hacking software to try and
gain access to your server. There are a few other options there but I haven't bothered with them.

Step 6
Unless you chose to give your server a static IP address during installation it will have a DHCP address assigned to it
by your router. This would be fine so long as you never reboot your router or shutdown the server. Sooner or later
though it will pick up a different IP address and you will be left disconnected, so we will give it a static one. Check
the current IP address by typing
ifconfig
at the command prompt. You only want to change the last part so for example if it is 192.168.1.5 then change it
to 192.168.1.101. You can set the last part to be any number between 2 and 254, mine is 101.
In the Ubuntu Server Guide open the chapter Networking Network Configuration - IP addressing and
select Static IP Address Assignment.
sudo nano /etc/network/interfaces
It should look something like
aut o et h0
i f ace et h0 i net st at i c
addr ess 192. 168. 1. 101
net mask 255. 255. 255. 0
gat eway 192. 168. 1. 1
The gateway is normally the IP address of your router which is usually 192.168.1.1 or 192.168.0.1 but may be
something completely different. If you ran
ipconfig
Publishedbywww.myignou.in
Visitourwebsitetodownloadmoresolvedassignments.

on your Windows PC it should have shown you the gateway address. If in doubt check your manual or Google the
model of your router.
You might also want to check the IP address of your Windows PC, it should be similar to the server but it may
previously have been set to a static address.
Run
cmd
from the Start button (or Start and Run in XP). Then type
ipconfig
at the command prompt. This should show your IP address which will be something
like 192.168.1.5 or 192.168.0.3. Whatever it is your PC and server should be in the same range of IP addresses
(this just means the first three parts should be the same).

Step 7
Now that you've a working server you'll want to tuck it away somewhere, minus monitor, k/b and mouse of course
(if your system board can boot without a k/b that is, it should be an option in the BIOS to ignore errors on boot).
During the initial installation of Ubuntu Server you selected to install SSH. This is a protocol (program) which allows
you to administer your server remotely from any PC (or even smart phone). If you didn't don't worry, just type:-
sudo apt-get install openssh-server
The easiest way to connect from another PC is to install Putty. A quick Google of Putty will have you up and running
in a few seconds. When you launch Putty you will see it asking for a Host Name (or IP address). Just type in your
server IP address and new port number, ensure SSH is selected, type a name into Saved sessions and click save,
then click open. Say Yes to the security question and your connected. Now you can administer the server from your
PC/laptop or even smart phone, how cool is that!
So there you have it. You've installed Ubuntu Server and configured it to let your Windows or Linux box connect to
it. There are many more things your server can do for you, it can act as a Proxy server screening your network from
the Internet and caching frequently used web sites, or as an email server, or a print server etc.

Published by www.myignou.in
Uploaded by Pankaj Sethi
Call 9781453353
Email sethi_pankaj@yahoo.in

You might also like