Upload
madirikiran
View
9
Download
0
Tags:
Embed Size (px)
DESCRIPTION
unix lab manual
Citation preview
Week 1
#include<stdio.h>
main(int argc,char *argv[])
{
int number,i,pid;
printf("enter number of process in chain\n");
scanf("%d",&number);
for(i=0;i<=number-1;i++)
{
pid=fork();
if(pid==0)
wait();
else
{
printf("i am%d child\n",i);
printf("my process id=%d\n",getpid());
printf("my parent process id=%d\n",getppid());
}
}
}
Output:
administrator@ubuntu:~/555$ cc chain.c
administrator@ubuntu:~/555$ ./a.out
enter number of process in chain
2
i am0 child
my process id=1657
my parent process id=1415
i am1 child
my process id=1657
my parent process id=1415
i am1 child
my process id=1658
my parent process id=1657
administrator@ubuntu:~/555$ ^C
administrator@ubuntu:~/555$
Week 2:
//enviroment list is usually is of the form "variable=string"
#include<stdio.h>
#include<stdlib.h>
main(argc,argv,envp)
int argc;
char *argv[];
char *envp[];
{
int i;
extern char **environ;
for(i=0;envp[i]!='\0';i++)
printf("%s\n",envp[i]);
exit(0);
}
Output :
[root@ttram home]# cc envp1.c -o environ1
[root@ttram home]# ./environ1
KDE_MULTIHEAD=false
SSH_AGENT_PID=1929
HOSTNAME=ttram
SHELL=/bin/bash
TERM=xterm
HISTSIZE=1000
GTK_RC_FILES=/etc/gtk/gtkrc:/root/.gtkrc:/root/.gtkrc-kde
GS_LIB=/root/.kde/share/fonts
QTDIR=/usr/lib/qt-3.1
USER=root
LS_COLORS=no=00:fi=00:di=00;34:ln=00;36:pi=40;33:so=00;35:bd=40;33;01:cd=40;33;01:or=01;05;37;41:mi=01;05;37;41:ex=00;32:*.cmd=00;32:*.exe=00;32:*.com=00;32:*.btm=00;32:*.bat=00;32:*.sh=00;32:*.csh=00;32:*.tar=00;31:*.tgz=00;31:*.arj=00;31:*.taz=00;31:*.lzh=00;31:*.zip=00;31:*.z=00;31:*.Z=00;31:*.gz=00;31:*.bz2=00;31:*.bz=00;31:*.tz=00;31:*.rpm=00;31:*.cpio=00;31:*.jpg=00;35:*.gif=00;35:*.bmp=00;35:*.xbm=00;35:*.xpm=00;35:*.png=00;35:*.tif=00;35:
SSH_AUTH_SOCK=/tmp/ssh-NMnf1861/agent.1861
SESSION_MANAGER=local/ttram:/tmp/.ICE-unix/2010
USERNAME=root
KONSOLE_DCOP=DCOPRef(konsole-2162,konsole)
MAIL=/var/spool/mail/root
DESKTOP_SESSION=default
PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/root/bin
KONSOLE_DCOP_SESSION=DCOPRef(konsole-2162,session-1)
INPUTRC=/etc/inputrc
PWD=/home MODIFIERS=@im=none
LANG=en_US.UTF-8
GDMSESSION=default
SSH_ASKPASS=/usr/libexec/openssh/gnome-ssh-askpass
HOME=/root
SHLVL=3
BASH_ENV=/root/.bashrc
LOGNAME=root
LESSOPEN=|/usr/bin/lesspipe.sh %s
DISPLAY=:0
G_BROKEN_FILENAMES=1
COLORTERM=
XAUTHORITY=/root/.Xauthority
_=./environ1
[root@ttram home]#
Week 3
#include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
main(int argc,char *argv[])
{
struct stat status;
int choice;
mode_t file;
stat(argv[1],& status);
printf("1--> checking input type\n");
printf("2-->checking file permission\n");
printf("enter choice\n");
scanf("%d", &choice);
switch(choice)
{
case 1:if(S_ISDIR(status.st_mode))
{
printf("input is a directory\n");
}
else if(S_ISREG(status.st_mode))
{
printf("input is regular file\n");
}
break;
case 2:if(file=S_ISREG(status.st_mode))
{
printf("given file=%s\n",argv[1]);
if(status.st_mode & S_IREAD)
printf("file has read permission\n");
if(status.st_mode & S_IWRITE)
printf("file has write permission\n");
if(status.st_mode & S_IEXEC)
printf("file has execute permission\n");
}
else
{
printf("given input is not a file\n");
}
break;
default:printf("invalid input\n");
break;
}
}
Output:
//test is directory or file
administrator@ubuntu:~/555$ cc file.c
administrator@ubuntu:~/555$ ./a.out test
1--> checking input type
2-->checking file permission
enter choice
1
input is a directory
administrator@ubuntu:~/555$ ./a.out test
1--> checking input type
2-->checking file permission
enter choice
2
given input is not a file
administrator@ubuntu:~/555$ ./a.out file.c
1--> checking input type
2-->checking file permission
enter choice
1
input is regular file
administrator@ubuntu:~/555$ ./a.out file.c
1--> checking input type
2-->checking file permission
enter choice
2
given file=file.c
file has read permission
file has write permission
administrator@ubuntu:~/555$ ./a.out a.out
1--> checking input type
2-->checking file permission
enter choice
1
input is regular file
administrator@ubuntu:~/555$ ./a.out a.out
1--> checking input type
2-->checking file permission
enter choice
2
given file=a.out
file has read permission
file has write permission
file has execute permission
week 4
mq1.c :
#include<stdio.h>
#include<fcntl.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<string.h>
main()
{
int mid,len;
struct mymsg
{
long mtype;
char mtext[100];
}msg;
if((mid=msgget((key_t)250,IPC_CREAT|0666))<0)
printf("\n error");
printf("mypid=%d\n ",getpid());
printf("To exit from queue, give string as 'cseb' \n");
for(;;)
{
printf("Enter data : ");
fgets(msg.mtext,100,stdin);
msg.mtype=getpid();
msgsnd(mid,&msg,sizeof msg.mtext,0);
if((strcmp(msg.mtext,"cseb\n"))==0)
break;
}
}
Mq2.c:
#include<stdio.h>
#include<fcntl.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<string.h>
main()
{
int mid,len;
struct mymsg
{
long mtype;
char mtext[100];
}msg;
if((mid=msgget((key_t)250,IPC_CREAT|0666))<0)
printf("\n error");
printf("mypid=%d\n",getpid());
printf("To exit from the queue, give data as 'cseb'\n");
for(;;)
{
printf("Enter data :: ");
fgets(msg.mtext,100,stdin);
msg.mtype=getpid();
msgsnd(mid,&msg,sizeof msg.mtext,0);
if((strcmp(msg.mtext,"cseb\n"))==0)
break;
}
}
Mq3.c:
#include<stdio.h>
#include<fcntl.h>
#include<sys/ipc.h>
#include<sys/msg.h>
#include<string.h>
main()
{
int mid,len,c=0;
struct mymsg
{
long mtype;
char mtext[100];
}msg;
mid=msgget((key_t)250,IPC_CREAT|0666);
for(;;)
{
if((len=msgrcv(mid,&msg,100,0,0))<0)
printf("\nerror");
printf("\nmessage from:%d ",msg.mtype);
fflush(stdout);
len=strlen(msg.mtext);
write(0,msg.mtext,len);
if((strcmp(msg.mtext,"cseb\n"))==0)
{
c++;
if(c==2)
break;
}
}
}
Output:
[root@merits new]# cc mq1.c -o 1
[root@merits new]# ./1
mypid=4816
To exit from queue, give string as 'cseb'
Enter data : hi
Enter data : me
Enter data : fine
Enter data : cseb
[root@merits new]# cc mq2.c -o 2
[root@merits new]# ./2
mypid=4823
To exit from the queue, give data as 'cseb'
Enter data :: me
Enter data :: true
Enter data :: cseb
[root@merits new]# cc mq3.c -o 3
[root@merits new]# ./3
message from:4816 hi
message from:4816 me
message from:4816 fine
message from:4816 cseb
message from:4823 me
message from:4823 true
message from:4823 cseb
week5
#include<stdio.h>
#include<sys/types.h>
#include<unistd.h>
main()
{ int filedes[2];
char wrbuff[128],rdbuff[128]="hello this is child";
pid_t pid;
int i=pipe(filedes);//creates pipe
if (i==0)
{
printf("Pipe is Successfully created\n");
}
else
{
perror("");
}
pid=fork();
if(pid==0)
{
//child process
printf("In child process====%d\n",getppid());
read(filedes[0],rdbuff,128);//reading from read-end of pipe
printf("Msg from parent is %s\n",rdbuff);
}
else
{
//parent process
printf("In parent process\npid=====%d\n",getpid());
printf("Enter msg:");
scanf(" %[^\n]",wrbuff);
write(filedes[1],wrbuff,128);//writing to the write-end of pipe
}
}
Output:
$ cc pip.c
$ ./a.out
In child process==3564
In parent process== 3564
Enter msg:
hello
Message from parent is hello
Week6:
#include<stdio.h>
#include<unistd.h>
#include<sys/stat.h>
main(int argc,char *argv[])
{
struct stat status;
int choice;
mode_t file;
stat(argv[1],& status);
printf("1--> checking input type\n");
printf("2-->checking file permission\n");
printf("enter choice\n");
scanf("%d", &choice);
switch(choice)
{
case 1:if(S_ISDIR(status.st_mode))
{
printf("input is a directory\n");
}
else if(S_ISREG(status.st_mode))
{
printf("input is regular file\n");
}
break;
case 2:if(file=S_ISREG(status.st_mode))
{
printf("given file=%s\n",argv[1]);
if(status.st_mode & S_IREAD)
printf("file has read permission\n");
if(status.st_mode & S_IWRITE)
printf("file has write permission\n");
if(status.st_mode & S_IEXEC)
printf("file has execute permission\n");
}
else
{
printf("given input is not a file\n");
}
break;
default:printf("invalid input\n");
break;
}
}
Output:
//test is directory or file
administrator@ubuntu:~/555$ cc file.c
administrator@ubuntu:~/555$ ./a.out test
1--> checking input type
2-->checking file permission
enter choice
1
input is a directory
administrator@ubuntu:~/555$ ./a.out test
1--> checking input type
2-->checking file permission
enter choice
2
given input is not a file
administrator@ubuntu:~/555$ ./a.out file.c
1--> checking input type
2-->checking file permission
enter choice
1
input is regular file
administrator@ubuntu:~/555$ ./a.out file.c
1--> checking input type
2-->checking file permission
enter choice
2
given file=file.c
file has read permission
file has write permission
administrator@ubuntu:~/555$ ./a.out a.out
1--> checking input type
2-->checking file permission
enter choice
1
input is regular file
administrator@ubuntu:~/555$ ./a.out a.out
1--> checking input type
2-->checking file permission
enter choice
2
given file=a.out
file has read permission
file has write permission
file has execute permission*/
week 7:
semacq.c:
#include<stdio.h>
#include<sys/sem.h>
#include<stdlib.h>
#define MY_SEM_ID 111
int main()
int semid;
struct sembuf sb;
/* Get the semaphore with the id MY-SEM_ID*/
semid=semget(MY_SEM_ID,1,0);
if(semid>=0)
{
/*sb- sembuf struct variable*/
sb.sem_num=0;
sb.sem_op=-1;
sb.sem_flg=0;
printf("semacq: Attempting to acquire semphore %d \n",semid);
/*Acquire the semaphore*/
if(semop(semid,&sb,1)==-1)
{
printf("semacq: semop failed\n");
exit(-1);
}
printf("semacq: semaphore acquired %d\n",semid);
}
return 0;
}
output:
structure of sembuf
struct sembuf
{
unsigned short sem_num;
short sem_op;
short sem_flg;
};
[root@merits semaphores]# cc semacq.c
[root@merits semaphores]# ls
a.out semacq.c semacq.c~ semcreate.c sem_getting.c~
[root@merits semaphores]# ./a.out
semacq: Attempting to acquire semphore 1114145
semacq: semaphore acquired 1114145
[root@merits semaphores]# ipcs -s -i 1114145
Semaphore Array semid=1114145
uid=0 gid=0 cuid=0 cgid=0
mode=0666, access_perms=0666
nsems = 1
otime = Not set
ctime = Wed Mar 23 10:19:55 2011
semnum value ncount zcount pid
0 0 1 0 0
[root@merits semaphores]#
Semcrd.c :
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#define MY_SEM_ID 111
int main()
{
int semid,cnt;
/*Get the semaphore with the id MY_SEM_ID*/
semid=semget(MY_SEM_ID,1,0);
if(semid>=0)
{
/*read the current semaphore count*/
cnt=semctl(semid,0,GETVAL);
if(cnt!=-1)
{
printf("\nsemcrd:current semaphore count %d\n",cnt);
}
}
return 0;
}
output:
[root@merits semaphores]# cc semcrd.c
semcrd.c: In function ‘main’:
semcrd.c:17: warning: incompatible implicit declaration of built-in function ‘printf’
[root@merits semaphores]# ./a.out
semcrd:current semaphore count 1
[root@merits semaphores]# */
Semcreate.c :
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#define MY_SEM_ID 111
int main()
{
int semid;
/*create the semaphore with the id MY_SEM_ID*/
semid=semget(MY_SEM_ID,1,0666|IPC_CREAT);
if(semid>=0)
printf("semcreate: create a semphore %d\n",semid);
return 0;
}
output:
[root@merits semaphores]# cc semcreate.c
sem_create.c: In function ‘main’:
sem_create.c:11: warning: incompatible implicit declaration of built-in function ‘printf’
[root@merits semaphores]# ls
a.out semcreate.c
[root@merits semaphores]# ./a.out
semcreate: create a semphore 1114145
[root@merits semaphores]#
Semrel.c
#include<stdio.h>
#include<sys/sem.h>
#include<stdlib.h>
#define MY_SEM_ID 111
int main()
{
int semid;
struct sembuf sb;
/*Get the semaphore with the id MY_SEM_ID*/
semid=semget(MY_SEM_ID,1,0);
if(semid>=0)
{
printf("semrel: Releasing semphore %d\n",semid);
sb.sem_num=0;
sb.sem_op=1;
//releasing
sb.sem_flg=0;
}
/*Release the semaphore */
if(semop(semid,&sb,1)==-1)
{
printf("semrel:semaphore released %d\n",semid);
}
return 0;
}
output:
[root@merits semaphores]# cc semrel.c
[root@merits semaphores]# ./a.out
semrel: Releasing semphore 1114145
(or)
[root@merits semaphores]# ls
semacq.c semcrd.c semcreate.c semrel.c semset.c semset.c~
[root@merits semaphores]# cc semrel.c
semrel.c:30:3: warning: no newline at end of file
[root@merits semaphores]# ./a.out
semrel: Releasing semphore 1114145
[root@merits semaphores]# cc semcrd.c
semcrd.c: In function ‘main’:
semcrd.c:17: warning: incompatible implicit declaration of built-in function ‘printf
[root@merits semaphores]# ./a.out
semcrd:current semaphore count 7
[root@merits semaphores]# cc semrel.c
semrel.c:30:3: warning: no newline at end of file
[root@merits semaphores]# ./a.out
semrel: Releasing semphore 1114145
[root@merits semaphores]# cc semcrd.c
semcrd.c: In function ‘main’:
semcrd.c:17: warning: incompatible implicit declaration of built-in function ‘printf’
[root@merits semaphores]# ./a.out
semcrd:current semaphore count 8
[root@merits semaphores]# cc semcrd.c
semcrd.c: In function ‘main’:
semcrd.c:17: warning: incompatible implicit declaration of built-in function ‘printf’
[root@merits semaphores]# ./a.out
semcrd:current semaphore count 8
[root@merits semaphores]# cc semrel.c
semrel.c:30:3: warning: no newline at end of file
[root@merits semaphores]# ./a.out
semrel: Releasing semphore 1114145
[root@merits semaphores]# cc semcrd.c
semcrd.c: In function ‘main’:
semcrd.c:17: warning: incompatible implicit declaration of built-in function ‘printf’
[root@merits semaphores]# ./a.out
semcrd:current semaphore count 9
Semset.c
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/sem.h>
#define MY_SEM_ID 111
int main()
{
int semid,ret;
/*Get the semaphore with the id MY_SEM_ID*/
semid=semget(MY_SEM_ID,1,0);
if(semid>=0)
{
/*Read the current semaphore count */
ret=semctl(semid,0,SETVAL,6);
if(ret!=-1)
printf("semcrd: semaphore count updated : %d\n",ret);
}
return 0;
}
output:
[root@merits semaphores]# cc semacq.c
[root@merits semaphores]# ./a.out
semacq: Attempting to acquire semphore 1114145
semacq: semaphore acquired 1114145
[root@merits semaphores]# cc semcrd.c
semcrd.c: In function ‘main’:semcrd.c:17: warning: incompatible implicit declaration of built-in function ‘printf’
[root@merits semaphores]# ./a.out
semcrd:current semaphore count 5
[root@merits semaphores]# cc semset.c
semset.c: In function ‘main’:
semset.c:15: warning: incompatible implicit declaration of built-in function ‘printf’
semset.c:19:2: warning: no newline at end of file
[root@merits semaphores]# ./a.out
semcrd: semaphore count updated : 0
[root@merits semaphores]# cc semcrd.c
semcrd.c: In function ‘main’:
semcrd.c:17: warning: incompatible implicit declaration of built-in function ‘printf’
[root@merits semaphores]# ./a.out
semcrd:current semaphore count 6
week 8
shwrite.c
#include<stdio.h>
#include<sys/shm.h>
#include<string.h>
#define MY_SHM_ID 111
int main()
{
int shmid,ret;
void *mem;
//Get the shared memory segment using MY_SHM_ID
shmid=shmget(MY_SHM_ID,0,0);
mem=shmat(shmid,(const void *)0,0);
strcpy((char*)mem,"This is a test string\n");
ret=shmdt(mem);
return 0;
}
output:
[root@merits shared memory]# ls
a.out getpagesize.c sh sh1 shmattach.c shmcreate.c shmread.c
shmread.c~ shmsizeget.c shmwrite.c shmwrite.c~
[root@merits shared memory]# cc shmcreate.c -o sh
shmcreate.c:19:3: warning: no newline at end of file
[root@merits shared memory]# ./sh
Created a shared memory segment 786440
[root@merits shared memory]# cc shmwrite.c -o sh1
[root@merits shared memory]# ./sh1
Shread.c:
#include<stdio.h>
#include<sys/shm.h>
#include<string.h>
#define MY_SHM_ID 111
int main()
{
int shmid,ret;
void *mem;
//Get the shared memory segment using MY_SHM_ID
shmid=shmget(MY_SHM_ID,0,0);
mem=shmat(shmid,(const void *)0,0);
printf("%s",(char *)mem);
ret=shmdt(mem);
return 0;
}
/*[root@merits shared memory]# ls
a.out getpagesize.c sh sh1 shmattach.c shmcreate.c shmread.c
shmread.c~ shmsizeget.c shmwrite.c shmwrite.c~
[root@merits shared memory]# cc shmcreate.c -o sh
shmcreate.c:19:3: warning: no newline at end of file
[root@merits shared memory]# ./sh
Created a shared memory segment 786440
[root@merits shared memory]# cc shmwrite.c -o sh1
[root@merits shared memory]# ./sh1
[root@merits shared memory]# cc shmread.c -o sh2
[root@merits shared memory]# ./sh2
This is a test string
[root@merits shared memory]#
Shcreate.c
#include<stdio.h>
#include<sys/shm.h>
#define MY_SHM_ID 111
main()
{
int shmid;
/*create the shared memory segment using MY_SHM_ID*/
shmid=shmget(MY_SHM_ID,4096,0666|IPC_CREAT);
if(shmid>=0)
{
printf("Created a shared memory segment %d \n",shmid);
}
return 0;
}
OUTPUT:
[root@merits shared memory]# cc shmcreate.c
[root@merits shared memory]# ./a.out
Created a shared memory segment 1474568
[root@merits shared memory]# ipcs -m
------ Shared Memory Segments --------
key shmid owner perms bytes nattch status
0x00000002 65536 root 600 655360 2
0x0052e2c1 131073 postgres 600 28377088 2
0x0056a4d5 262146 root 660 384 2
0x00000000 524291 root 600 393216 2 dest
0x00000000 393220 root 600 393216 2 dest
0x00000000 425989 root 600 393216 2 dest
0x00000000 557062 root 600 393216 2 dest
0x0056a4d6 1441799 root 660 131072 1
0x0000006f 1474568 root 666 4096 0
[root@merits shared memory]# ipcs -i
ipcs: option requires an argument -- i
usage : ipcs -asmq -tclup
ipcs [-s -m -q] -i id
ipcs -h for help.
You have new mail in /var/spool/mail/root
[root@merits shared memory]# ipcs -m -i 1474568
Shared memory Segment shmid=1474568
uid=0 gid=0 cuid=0 cgid=0
mode=0666 access_perms=0666
bytes=4096 lpid=0 cpid=3648 nattch=0
att_time=Not set
det_time=Not set
change_time=Wed Mar 30 15:18:02 2011
[root@merits shared memory]# ipcrm -m [root@merits shared memory]#
cc shmdel.c
ipcrm: already removed id ([root@merits)
ipcrm: unknown argument: shared
usage: ipcrm [ [-q msqid] [-m shmid] [-s semid]
[-Q msgkey] [-M shmkey] [-S semkey] ... ]
[root@merits shared memory]# [root@merits shared memory]# ./a.out
bash: [root@merits: command not found
[root@merits shared memory]# Shared Memory segment removed
bash: Shared: command not found
[root@merits shared memory]# ipcrm -m 1474568
[root@merits shared memory]# ipcs -m -i 1474568
shmctl : Invalid argument
[root@merits shared memory]#
Shdel.c
#include<stdio.h>
#include<sys/shm.h>
#include<string.h>
#include<errno.h>
#define MY_SHM_ID 111
int main()
{
int shmid,ret;
/*create the shared memory segment using MY_SHM_ID*/
shmid=shmget(MY_SHM_ID,0,0);
if(shmid>=0)
{
ret=shmctl(shmid,IPC_RMID,0);
if(ret==0)
printf("Shared Memory segment removed\n");
else
printf("shmctl failed (%d)\n",errno);
}//if
else
{
printf("shared memory segment not found\n");
return 0;
}
output:
[root@merits shared memory]# cc shmdel.c
[root@merits shared memory]# ./a.out
Shared Memory segment removed
note: if u not including the shmget() fn. u get following error
[root@merits shared memory]# cc shmdel.c
[root@merits shared memory]# ./a.out
shmctl failed (22)
Week 9:
TCPclient.c
#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<errno.h>
#include<string.h>
main(int argc,char *argv[])
{
int sockfd;
int nbytes,nbytes1;
struct sockaddr_in servaddr;
char buff[512],buff1[512];
if(argc!=3)
{
printf("use client servaddr servport");
exit(1);
}
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
perror("socket ");
exit(1);
}
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(atoi(argv[2]));
servaddr.sin_addr.s_addr=htonl(inet_addr(argv[1]));
if(connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr))<0)
{
perror("connect failed");
exit(1);
}
printf("\nconected ...\n");
while((nbytes=read(0,buff,512))>0)
{
if(write(sockfd,buff,nbytes)<0)
{
error("error");
exit(1);
}
if((nbytes1=read(sockfd,buff1,512))<0)
{
perror("read error");
exit(1);
}
if(write(1,buff1,nbytes1)<0)
{
perror("write error!");
exit(1);
}
printf("\n");
}
}
TCPserver.c
#include<sys/types.h>
#include<sys/socket.h>
#include<errno.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<stdio.h>
main(int argc, char *argv[])
{
int sockfd,nbytes,newfd;
char buff[512];
struct sockaddr_in myaddr;
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
{
perror("Socket is failed to connect");
exit(1);
}
myaddr.sin_family=AF_INET;
myaddr.sin_port= htons(atoi(argv[1]));
myaddr.sin_addr.s_addr=htonl(INADDR_ANY);
if((bind(sockfd,(struct sockaddr *)&myaddr,sizeof(myaddr)))<0)
{
perror("Binding is failed");
exit(1);
}
if(listen(sockfd,5)<0)
{
perror("Listen fail");
exit(1);
}
printf("\nwaiting for connection.......\n");
if((newfd=accept(sockfd,0,0))<0)
{
perror("Accept failed");
exit(1);
}
while((nbytes=read(newfd,buff,512))>0)
{
write(1,buff,nbytes);
if(write(newfd,buff,nbytes)<0)
{
perror("Write error");
exit(1);
}
}
}
output :
//FIRST START THE SERVER
[ttram@ttram tcpecho]$ ls
tcpechoclient.c tcpserver.c
[ttram@ttram tcpecho]$ cc tcpserver.c
[ttram@ttram tcpecho]$ ./a.out 2356
waiting for connection.......
hi
hello
i am fine
okd
bye
[ttram@ttram tcpecho]$
//THEN CLIENT
[ttram@ttram TCP AND UDP]$ cd tcpecho/
[ttram@ttram tcpecho]$ ls
a.out tcpechoclient.c tcpserver.c
[ttram@ttram tcpecho]$ cc tcpechoclient.c -o cli
[ttram@ttram tcpecho]$ ./cli 0.0.0.0 2356
conected ...
hi
hi
hello
hello
i am fine
i am fine
okd
okd
bye
bye
^C
[ttram@ttram tcpecho]$
Week 10:
Udpclient:
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<errno.h>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
main(int argc,char *argv[])
{
int sockfd,nbytes,nbytes1,addrlen;
char buff[512];
struct sockaddr_in myaddr,servaddr;
if(argc!=3)
{
perror("usage client,serverport,serveraddress");
exit(1);
}
if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0)
{
perror("Socket failed");
exit(1);
}
myaddr.sin_family=AF_INET;
myaddr.sin_port=htons(0);
myaddr.sin_addr.s_addr=htonl(0l);
if((bind(sockfd ,(struct sockaddr*)&myaddr,sizeof(myaddr)))<0)
{
perror("bind failed");
exit(1);
}
while(1)
{
printf("\n Enter data '#' to exit ::\n");
if((nbytes=read(0,buff,512))<0)
{
perror("read error");
exit(1);
}
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(atoi(argv[2]));
servaddr.sin_addr.s_addr=htonl(inet_addr(argv[1]));
if((sendto(sockfd,buff,nbytes,0,(struct sockaddr*)&servaddr,sizeof(servaddr)))<0)
{
perror("send to failed");
exit(1);
}
if(buff[0]=='#')
break;
printf("\nEchoed data read ::\n");
addrlen=sizeof(servaddr);
if((nbytes1=recvfrom(sockfd,buff,512,0,(struct sockaddr*)&servaddr,&addrlen))<0)
{
perror("recvfrom error");
exit(1);
}
write(1,buff,nbytes1);
bzero(buff,nbytes);
}
}
Udpserver:
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<errno.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main(int argc,char *argv[])
{
int sockfd,addrlen,nbytes,tt;
char buffer[512],buff[512];
struct sockaddr_in myaddr,clntaddr;
if(argc!=2)
{
perror("usage server port");
exit(1);
}
if((sockfd=socket(AF_INET,SOCK_DGRAM,0))<0)
{
perror("Socket failed");
exit(1);
}
myaddr.sin_family=AF_INET;
myaddr.sin_port=htons(atoi(argv[1]));
myaddr.sin_addr.s_addr=htonl(0l);
if((bind(sockfd,(struct sockaddr*)&myaddr,sizeof(myaddr)))<0)
{
perror("bind failed");
exit(1);
}
while(1)
{
addrlen=sizeof(clntaddr);
if((nbytes=recvfrom(sockfd,buff,512,0,(struct sockaddr*)&clntaddr,&addrlen))<0)
{
perror("recvfrom error");
exit(1);
}
if(buff[0]=='#')
break;
write(1,buff,nbytes);
if((sendto(sockfd,buff,nbytes,0,(struct sockaddr*)&clntaddr,addrlen))<0)
{
perror("send to error");
exit(1);
}
bzero(buff,nbytes);
}
}
output :
/HERE ALSO FIRST START THE SERVER WITH A PORT
[ttram@ttram udpecho]$ cc udpser.c -o ser
[ttram@ttram udpecho]$ ./ser 4532
hi
fine
good
[ttram@ttram udpecho]$
//NEXT CLIENT
[ttram@ttram udpecho]$ cc udpcli.c -o cli
[ttram@ttram udpecho]$ ./cli 0.0.0.0 4532
Enter data '#' to exit ::
hi
Echoed data read ::
hi
Enter data '#' to exit ::
fine
Echoed data read ::
fine
Enter data '#' to exit ::
good
Echoed data read ::
good
Enter data '#' to exit ::#
[ttram@ttram udpecho]$