You are on page 1of 18

PROGRAM: WAP TO IMPLEMENT CLIENT-SERVER

CONNECTION USING TCP.


//tcp_client
#include<arpa/inet.h>
#include<sys/socket.h>
#include<string.h>
#include<unistd.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<stdio.h>
#define BUFFSIZE 32
struct sockaddr_in echoserver;
int main(int argc, char *argv[])
{ int sock;
char buffer[BUFFSIZE];
unsigned int echolen;
int received =0;
if (argc != 4) {
fprintf(stderr, "USAGE: TCPecho <server_ip> <word> <port>\n");
exit(1);
}
/* Create the TCP socket */
if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
perror("Failed to create socket");}
/* Construct the server sockaddr_in structure */
memset(&echoserver, 0, sizeof(echoserver)); // clear struct
echoserver.sin_family = AF_INET;
// Internet IP
echoserver.sin_addr.s_addr = inet_addr(argv[1]); // IP address
echoserver.sin_port = htons(atoi(argv[3]));
//server port
/* Establish connection*/
if (connect(sock,(struct sockaddr *) &echoserver,sizeof(echoserver)) < 0) {
perror("Failed to connect with server"); }
/* Send the word to the server */
echolen = strlen(argv[2]);
if (send(sock, argv[2], echolen, 0) != echolen) {
perror("Mismatch in number of sent bytes");
}
/* Receive the word back from the server */
fprintf(stdout, "Received: ");
while(received < echolen)
{int bytes=0;
if ((bytes = recv(sock, buffer, BUFFSIZE-1, 0)) < 1) {
perror("Failed to receive bytes from server");
}
received += bytes;
buffer[bytes] = '\0';
/* Assure null terminated string */
fprintf(stdout, buffer);
}
fprintf(stdout, "\n");
close(sock);

exit(0); }
// tcp_server
#include<sys/socket.h>
#include<arpa/inet.h>
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
#include<unistd.h>
#include<netinet/in.h>
#define MAXPENDING 5
#define BUFFSIZE 32
struct sockaddr_in echoserver;
void HandleClient(int sock) {
char buffer[BUFFSIZE];
int received = -1;
// Receive message
if ((received = recv(sock, buffer, BUFFSIZE, 0)) < 0) {
perror("Failed to receive initial bytes from client");
}
/* Send bytes and check for more incoming data in loop */
while (received > 0) {
/* Send back received data */
if (send(sock, buffer, received, 0) != received) {
perror("Failed to send bytes to client");
}
/* Check for more data */
if ((received = recv(sock, buffer, BUFFSIZE, 0)) < 0) {
perror("Failed to receive additional bytes from client");
}
}
close(sock);
}
int main(int argc, char *argv[]) {
int serversock, clientsock,rc;
struct sockaddr_in echoclient;
if (argc != 2) {
fprintf(stderr, "USAGE: echoserver <port>\n");
exit(1);
}
/* Create the TCP socket */
if ((serversock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0) {
perror("Failed to create socket");
}
/* Construct the server sockaddr_in structure */
memset(&echoserver, 0, sizeof(echoserver)); /* Clear struct */
echoserver.sin_family = AF_INET;
/* Internet/IP */
echoserver.sin_addr.s_addr = htonl(INADDR_ANY); /* Incoming addr */
echoserver.sin_port = htons(atoi(argv[1])); //server port
//echoserver.sin_port = htons(SERPORT);
/* Bind the server socket */

if (rc=(bind(serversock, (struct sockaddr *)&echoserver,


sizeof(echoserver))) < 0) {
perror("Failed to bind the server socket");
}
/* Listen on the server socket */
if (listen(serversock, MAXPENDING) < 0) {
perror("Failed to listen on server socket");
}
/* Run until cancelled */
while (1) {
unsigned int clientlen = sizeof(echoclient);
/* Wait for client connection */
if ((clientsock =
accept(serversock, (struct sockaddr *) &echoclient,
&clientlen)) < 0) {
perror("Failed to accept client connection");
}
fprintf(stdout, "Client connected: %s\n",
inet_ntoa(echoclient.sin_addr));
HandleClient(clientsock);
}
}

OUTPUT:

PROGRAM: WAP TO COPY A FILE WITH THE READ AND WRITE


SYSTEM CALLS.
#include<fcntl.h>
#include<sys/stat.h>
#include<stdio.h>
#define BUFSIZE 1024
int main(void)
{
int fd1,fd2;
int n;
char buf[BUFSIZE];
fd1=open("etc/passwd", O_RDONLY);
fd2=open("passwd.bak", O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|
S_IWGRP|S_IROTH);
while((n==read(fd1,buf,BUFSIZE))>0)
write(fd1,buf,n);
printf("file copied \n");
close(fd1);
close(fd2);
}

OUTPUT:

PROGRAM: WAP TO REVERSE THE CONTENT OF A FILE.


#include<fcntl.h>
#include<unistd.h>
#include<stdio.h>
#define bufsize 100
void main(int argc, char **argv)
{
int fd,size;
char buf;
fd=open(argv[1],O_RDONLY);
size=lseek(fd,-1,SEEK_END);
while(size-- >= 0)
{
read(fd,&buf,1);
write(STDOUT_FILENO, &buf, 1);
lseek(fd,-2,SEEK_CUR);
}
}

OUTPUT:

PROGRAM: CREATING A FILE WITH IDENTICAL TIME STAMPS.


#include<sys/stat.h>
#include<fcntl.h>
#include<utime.h>
void main(int argc, char **argv)
{
struct stat statbuf;
struct utimbuf timebuf;
if(lstat(argv[1],&statbuf)==-1)
perror("stat");
timebuf.actime=statbuf.st_atime;
timebuf.modtime=statbuf.st_mtime;
if(open(argv[2],O_RDWR|O_CREAT | O_EXCL,0644)==-1)
perror("open");
close(argv[2]);
if(utime(argv[2],&timebuf)==-1)
perror("utime");
}

OUTPUT:

PROGRAM: CHECKING THE OWNERS PERMISSIONS.


#include<unistd.h>
#include<stdio.h>
void main(int argc,char *argv[])
{
short count ;
for(count=1;count<argc;count++)
{
printf("%s: ",argv[count]);
if(access(argv[count],F_OK)==-1)
perror("file not found");
if(access(argv[count],R_OK)==-1)
printf("not readable");
if(access(argv[count],W_OK)==-1)
printf("not writeable");
if(access(argv[count],X_OK)==-1)
printf("not executable");
printf("\n");
}
}

OUTPUT:

PROGRAM: CHECKING ALL 12 PERMISSIONS OF A FILE.


#include<sys/stat.h>
#include<fcntl.h>
#include<stdio.h>
void print_per(char *,struct stat *);
void check_per(int ,int, char *);
void main(int argc,char *argv[])
{
int i,fd,perm;
char *filename=argv[1];
struct stat statbuf;
mode_t perm_flag[]={S_IRUSR,S_IWUSR,S_IXUSR,S_IRGRP,S_IWGRP,
S_IXGRP,S_IROTH,S_IWOTH,S_IXOTH,S_ISUID,S_ISGID,S_ISVTX};
char *mesg[]={"user-readable","user-writable","user-executable","group-readable","groupwriteable","group-executable","others-readable","others-writable","othersexecutable","SUID bit
set","SGID bit set","sticky bit set"};
print_per(filename,&statbuf);
perm=statbuf.st_mode & ~S_IFMT;
for(i=0;i<12;i++)
check_per(perm,perm_flag[i],mesg[i]);
}
void print_per(char *fname, struct stat *addbuf)
{
if(lstat(fname, addbuf)<0)
{
perror("stat");
}
else
printf("File :%s permissions: %o\n", fname, addbuf->st_mode & ~S_IFMT);
}
void check_per(int perm, int flag, char *msg)
{
if(perm & flag)
printf("%s\n", msg);
}

OUTPUT:

PROGRAM: LISTING ONLY DIRECTORIES.


#include<sys/stat.h>
#include<sys/types.h>
#include<stdio.h>
#include<dirent.h>
void main(int argc,char *argv[])
{
DIR *dir;
struct dirent *direntry;
struct stat statbuf;
mode_t file_type,file_perm;
if((dir=opendir(argv[1]))==NULL)
quit("could not open directory",1);
if((chdir(argv[1])==-1))
quit("chdir",2);
while((direntry=readdir(dir))!=NULL)
{
if(lstat(direntry->d_name, &statbuf)<0)
{
perror("lstat");
continue;
}
if(S_ISDIR(statbuf.st_mode))
{
file_type=statbuf.st_mode & S_IFMT;
file_perm=statbuf.st_mode & ~S_IFMT;
printf("%o %4o %s\n",file_type,file_perm,direntry->d_name);
}}}

OUTPUT:

PROGRAM: DISPLAYING SOME FILE ATTRIBUTES.


#include<stdio.h>
#include<sys/stat.h>
void main(int argc, char **argv)
{
struct stat statbuf;
if(lstat(argv[1], &statbuf)==-1)
perror("could not stat file");
printf("file: %s\n", argv[1]);
printf("inode number: %d \n" statbuf.st-ino);
printf("uid: %d",statbuf.st_uid);
printf("gid: %d \n",statbuf.st_gid);
printf("type and permissions: %o \n",statbuf.st_mode);
printf("number of links: %d \n", statbuf.st_nlink);
printf("size in bytes: %d \n", statbuf.st_size);
printf("blocks allocated: %d \n", statbuf.st_blocks);
//printf("last modification time: %s \n", ctime(&statbuf.st_mtime));
//printf("last access time: %s \n", ctime(&statbuf.st_atime));
}

OUTPUT:

PROGRAM: WAP TO SEARCHING A CONTENT USING GREP


COMMAND.
Linux.txt file
emp_no. emp_name

emp_designation

salary

1001

abc

manager

10000

1002

xyz

supervisor

2000

1003

pqr

director

100000

Linux.sh file
echo "Enter the pattern name to be searched:\c"
read pattern
echo "Enter the file name to be used:\c"
read filename
echo "seaching for pattern from file $filename"
grep $pattern $filename

OUTPUT:

PROGRAM: WAP TO SHOW THE SEARCHING THE CONTENT


USING COMMAND LINE ARGUMENTS.
Linux.txt file
emp_no. emp_name

emp_designation

salary

1001

abc

manager

10000

1002

xyz

supervisor

2000

1003

pqr

director

100000

emp1.sh file
echo "program=$0"
echo "No. of agruments are $#"
echo "agruments are $*"
grep $1 $2
echo "\n job over"

OUTPUT:

PROGRAM: WAP TO COPY A FILE USING SHELL SCRIPT


COMMAND
filecopy.sh file
#filecopy.sh: using if only
echo "Enter source and target filename"s
read source target
if cp $source $target
then echo "file copied succesfully"
fi
copied file abc.txt
emp_no. emp_name

emp_designation

salary

1001

abc

manager

10000

1002

xyz

supervisor

2000

1003

pqr

director

100000

OUTPUT:

PROGRAM: WAP TO IMPLEMENT THE WHILE LOOP IN SHELL


SCRIPTING.
answer=y
while [ $answer="y" ]
do
echo "Enter code and description of the product"
read code description
echo "$code $description">>product
echo "Enter any more(Y/N)"
read anymore
case $anymore in
y*/Y*) answer =y;;
n*/N*) answer =n;;
esac
done

OUTPUT:

PROGRAM: WAP TO IMPLEMENT A FOR LOOP TO FIND THE


PATH OF THE DIRECTORY.
for var in $PATH $HOME $SHELL
do
echo $var
done

OUTPUT:

PROGRAM: WAP TO IMPLEMENT A FOR LOOP TO COUNT THE


CHARACTER IN THE FILE.
#fordemo.sh
for cmd in `pwd` `wc -c a.txt`
do
echo $cmd
done
a.txt
this is linux lab

OUTPUT:

PROGRAM: WAP TO IMPLEMENT IF ELSE.


if [ ! -e $1 ] ; then
echo "file does not exist"
elif [ ! -r $1 ] ; then
echo "file is not readable"
elif [ ! -w $1 ] ; then
echo "file is not writeable"
else
echo "file is both readable and writable"
fi

my.txt
Name: abc
123
eno: 06715604413 456
phno 9999999999 789
ctc: 3.2 lacs
111

OUTPUT:

PROGRAM: WAP TO IMPLEMENT SWITCH CASE.


echo "menu \n 1.list of files\n 2.today's date\n 3.processes\n 4.users of system \n 5.quit to linux\n
enter your choice: "
read choice
case $choice in
1) ls -l ;;
2) date ;;
3) ps;;
4) who;;
5) exit;;
*) echo"invalid option"
esac

OUTPUT:

You might also like