48
Ex No:1 Date: 31-12-2011  BASIC COMMANDS AIM: To write a UNIX program that can b e used to executethe basic UNIX commands ALGORITHM: Step 1: Start the process Step 2: Include the necessary header files Step 3: Declare the varriables Step 4: Get the command to be executed Step 5: Using fork(), create child process Step 6: In the child process, create input command Step 7: Get the status of the child using waitpid() and terminate Step 8: Compile and execute the program Step 9: Stop the process 1

Unix Rec Full New

Embed Size (px)

Citation preview

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 1/48

Ex No:1

Date: 31-12-2011

  BASIC COMMANDS

AIM:

To write a UNIX program that can be used to executethe basic UNIX commands

ALGORITHM:

Step 1: Start the process

Step 2: Include the necessary header files

Step 3: Declare the varriables

Step 4: Get the command to be executed

Step 5: Using fork(), create child process

Step 6: In the child process, create input command

Step 7: Get the status of the child using waitpid() and terminate

Step 8: Compile and execute the program

Step 9: Stop the process

1

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 2/48

PROGRAM

1.ls – list content of directories:

[admin@niitm]$ ls

d Documents . desktop.c Pictures Templates.c

Desktop Downloads Music Public Videos varun.c

2.mv- move (or) rename files:

[admin@niitm]$mv desktop.c desk.c

3.Clear- Clears the terminal screen

4. copy files (or) directory

[admin@niitm]$ cp a.c b.c

5.rm – remove (delete) files (or) directories:

6.cat- concatenation

[admin@niitm]$ cat a.c b.c

#include<stdio.h>

main()

{

Printf(“haii welcome”);

}

7. cmp – compare

[admin@niitm] $ cmp a.c d.c

a.c d.c differ: byte 36, line 4

2

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 3/48

8.help- display help for built in command

9. mkdir- make(create) new directories

[admin@niitm]$mkdir new

10.free- display memory usage

[admin@niitm] $free

11. netstat

[admin@niitm]$ netstat –a

unix 3 [ ] STREAM CONNECTED 14813

unix 2 [ ] DGRAM 14578

unix 2 [ ] DGRAM 14412

unix 3 [ ] STREAM CONNECTED 14332

12. ping

[admin@niitm ~]$ ping 192.168.1.99

PING 192.168.1.99 (192.168.1.99) 56(84) bytes of data.

64 bytes from 192.168.1.99: icmp_seq=1 ttl=128 time=0.237 ms

64 bytes from 192.168.1.99: icmp_seq=2 ttl=128 time=0.203 ms

RESULT:

  The program was executed successfully and the output was verified

Ex No:2

3

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 4/48

Date: 07-01-2011

  SYSTEM CALLS

AIM:

To write a UNIX program to illustrate the implementation of various System calls

ALGORITHM:

Step 1: Start the process

Step 2: Include the header files “stdio.h”

Step 3: Declare the necessery variables n and buf[512]

Step 4: Read the input from the keyboard until either ctrl+d press or reach the

maximum size limit values stored into buf[512]

Step 5: write the values into the display from the buf[512]

Step 6: save and exit the vi editor by pressing Esc+wq :

Step 7: Run the program

Step 8: Stop the process

PROGRAM:

4

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 5/48

#include<stdio.h>

#include<sys/types.h>

#define SIZE 512

main()

{

Char buf[SIZE];

Int n;

while((n=read(0,buf,sizeof(buf))>0)

write(1,buf,n);

exit(0);

}

OUTPUT:

5

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 6/48

Welcome to UNIX Programming

Welcome to UNIX Programming

RESULT:

  The program was executed successfully and the output was verified.

Ex No:3

Date: 28-01-2011

6

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 7/48

  IPC USING PIPES

AIM:

  To write a UNIX program to implement Interprocess Communication Using Pipes

ALGORITHM:

Step 1: Start the process

Step 2: Include The necessary header files “stdio.h” and define MAXSIZE as 20

Step 3: Create pipe using the pipe function

Step 4: Create the child process using fork function

Step 5: In the parent , close the write end of the pipe and read the messages from pipe

using the read function

Step 6: In the child process, close the read end of the pipe

Step 7: Get messages to be send to the server from the console

Step 8: Write the message in the pipe using write () function .

Step 9: Compile and execute the program

Step 10: Stop the process

PROGRAM:

#include<stdio.h>

#include<unistd.h>

7

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 8/48

#define MAXSIZE 20

Int server(int readfd)

{

char buf[MAXSIZE];

read(readfd,buf,MAXSIZE);

printf(“\nString in the server\t: %s”,buf);

}

Int client(int writefd)

{

char cbuf[MAXSIZE];

printf(“Enter the message\t”);

fgets(cbuf,MAXSIZE,stdin);

write(writefd,cbuf,MAXSIZE);

}

main()

{

Int p1[2],pid;

pipe(p1);

pid=fork();

if(pid>0)

{

Close(p1[1]);

Server(p1[0]);

}

Else

8

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 9/48

{

Close(p1[0]);

Client(p1[1]);

Sleep(3);

}

}

b)

#include<stdio.h>

#include<unistd.h>

9

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 10/48

int main()

{

int n;

int fd[2];

   pid_t pid;

char line[max];

if(pipe(fd)<0)

{

 printf(“pipe error”);

}

if((pid=fork())<0)

{

Printf(“fork is executed”);

}

else if(pid>0)

{

close(fd[0]);

write(fd[1],”hellow world”,12);

}

else

{

close(fd[1]);

n=read(fd[0],line,max);

write(TDOUT_FILENO,line,n);

}

10

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 11/48

exit(0)

}

OUTPUT:

a)

  [admin@niitm]$cc ipc1.c

11

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 12/48

[admin@niitm]$./a.out

Enter the message : hello

String in server : hello

b)

[admin@niitm]$cc ipc2.c

[admin@niitm]$./a.out

Hello world

RESULT:

  The program was executed successfully and the output was verified.

Ex No:4

Date: 04-01-2011 

MESSAGE QUEUE

12

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 13/48

AIM:

  To write a UNIX program to illustrate the operation of message queue

ALGORITHM:

SENDING MESSAGES:

Step 1: Start the process

Step 2: Include the necessary header files

Step 3: Astructure is created with members mtype of type long and char array mtext[]

Step 4: The msgget function is called to create a new queue or to open an existing

queue

Step 5: Create a do…while loop to get messages to be sent

Step 6: Get the messages from the user , and store it in the msg.mtext variable

Step 7: The msgsnd function is called . data is placed onto a messsage queue by

calling msgsnd ()

Step 8: If the function display 1 then display message sending failed, else message sent

Step 9: If ch meets the condition ‘y’or’Y’ then iterate the loop for sending more number 

of messages

Step 10: Compile and execute the program

Step 11: Stop the process

RECEIVING MESSAGES:

Step 1: Start the process

Step 2: Include the necessary header files

13

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 14/48

Step 3: Declare the necessary vvarriables and structure with members mtext[] and

mtype

Step 4: The msgget function is called to create a new queue or open an existing queue

Step 5: The sent message is received using msgrcv function

Step 6: Display the received message by using while loop

Step 7: Compile and execute the program

Step 8: Stop the process

PROGRAM:

SENDING MESSAGES:

#include<stdio.h>

#include<sys/types.h>

14

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 15/48

#include<sys/msg.h>

#include<sys/ipc.h>

#define SIZE 20

main()

{

char ch;

int qid;

key_t key=21;

size_t msgsz;

struct msgbuf 

{

long mtype;

char mtext[SIZE];

}msg;

qid=msgget(key,IPC_CREATE|0666);

do

{

printf(“\n Enetr the message \t”);

scanf(“%s”,msg.mtext);

msgsz=sizeof(msg.mtext);

msg.mtype=1;

if(msgsnd(qid,&msg,msgsz,0)==-1)

{

 printf(“message sending failed\n”);

exit(1);

}

else

15

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 16/48

{

 printf(“message sent”);

}

fgetc(stdin);

 printf(“\nDo you want to continue(y/Y)”);

scanf(“%c”,&ch);

if((ch!=’y’) && (ch!=’Y’))

{ break; }

}while(1);

}

RECEIVING MESSAGES:

#include<stdio.h>

#include<sys/ipc.h>

#include<sys/msg.h>

#define SIZE 20

16

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 17/48

main()

{

Int qid,I;

key_t key=21;

struct msgbuf 

{

long mtype;

char mtext[SIZE];

}msg;

qid=msgget(key,IPC_CREAT|0666);

while(msgrcv(qid,&msg,SIZE,0,IPC_NOWAIT|MSG_NOERROR)>=0)

{

 printf(“\n Message Received \t\t:%s\n”,msg.mtext);

}

}

OUTPUT:

SEND:

[admin@niitm]$cc msg.c

[admin@niitm]$ ./a.out

17

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 18/48

Enter the message : haii

Message Sent

Do you want to continue(y/n) : y

Enter the message : welcome

Message sent

Do you want to continue(y/n) : n

RECEIVE:

[admin@niitm]$ cc ms.c

[admin@niitm]$./a.out

Message Received :haii

Message Received :welcome

RESULT:

  The program was executed successfully and the output was verified

Ex No:5

Date: 07-02-2011

  IPC USING SHARED MEMORY

AIM:

18

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 19/48

  To write a UNIX program to illustrate the operation of shared memory

ALGORITHM:

Step 1: Start the process

Step 2: Include the necessary header files

Step 3: Declare variables id of type integer and key of type key_t

Step 4: The shmget()function is used to get the shared memory identifier 

Step 5: If shmid<1 , display ‘shared Memory failed’

Step 6: else display the shared memory identifier 

Step 7: Save the process using Esc:wq

Step 8: Compile and execute the program

Step 9: Stop the process

PROGRAM:

#include<stdio.h>

#include<sys/ipc.h>

#include<sys/shm.h>

main()

{

19

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 20/48

int shmid;

key_t key=0*5001;

shmid=shmget(key,10,IPC_CREAT|0666);

if(shmid<0)

{

 printf(“shared memory failed”);

exit(1);

}

 printf(“success shmid is\t: %d”,shmid);

}

OUTPUT:

[admin@niitm]$cc ipc1.c

[admin@niitm]$./a.out

Success shmid is :131075

20

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 21/48

[admin@niitm]$ipc –m

Key shmid owner perms bytes nattch status

0x00000000 65537 admin 666 10 0

0x00000000 98306 admin 666 10 0

RESULT:

  The program was executed successfully and the output was verified.

Ex No:6

Date: 14-02-2011

  SYNCHRONIZATION USING SEMAPHORE

AIM:

  To write a UNIX program to illustrate the operation of shared memory

21

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 22/48

ALGORITHM:

Step 1: Start the process

Step 2: Include the necessary header files

Step 3: Declare variables id of type integer and key of type key_t

Step 4: The semget()function is used to get the shared memory identifier 

Step 5: If semid<1 , display ‘shared Memory failed’

Step 6: else display the shared memory identifier 

Step 7: Save the process using Esc:wq

Step 8: Compile and execute the program

Step 9: Stop the process 

PROGRAM:

#include<stdio.h>

#include<sys/ipc.h>

#include<sys/shm.h>

main()

{

int shmid;

22

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 23/48

key_t key=21;

semid=semget(key,10,IPC_CREAT|0666);

if(semid<0)

{

 printf(“shared memory failed”);

exit(1);

}

 printf(“success semid is\t: %d”,semid);

}

OUTPUT:

[admin@niitm]$cc ipc2.c

[admin@niitm]$./a.out

Success semid is : 65536

23

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 24/48

RESULT:

  The program was executed successfully and the output was verified.

Ex No:7

Date: 28-02-2011 

PACKET SNIFFER 

AIM:

To write a UNIX program to perform a packet sniffer.

 ALGORITHM:

24

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 25/48

Step 1 :Start the process.

Step 2: Include the header file ”stdio.h” “ctype.h” “stdlib.h” “string.h”

“errno.h” “netdb.h” “sys/socket.h” “linux/in.h” “arpa/inet.h”

“linux/if_ether.h”.

Step 3: Declare the necessary variables s1,c1,n,clilen as integer and buf[100],s as char.

Step 4: Create the socket and check the condition

If(bind(s1,(struct sockaddr*)&ser,sizeof(ser))==-1)

If the condition is true print bind error.

Step 5: Find the cli by using sizeof function and assign it clilen.

Step 6: Check if((c1=accept(s1,(struct sockaddr*)&cli,&client))==-1)

If the condition is true print the error is accepted.

Step 7: Check if ((n=read(c1,buf,sizeof(buf)))!=-1) true get message from the client.

Step 8: Store the client message in buffer.

Step 9: Write c1,buf and size of the buffer.

Step 10: Compile and execute the program.

Step 11: Stop the process.

 

PROGRAM:

#include<stdio.h>

#include<ctype.h>

#include<stdlib.h>

#include<string.h>#include<errnoo.h>

#include<netdb.h>

#include<sys/socket.h>

#include<linux/in.h>

25

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 26/48

#include<arpa/inet.h>

#include<linux/if_ether.h>

#define ETH_HW_ADDR_LEN 6

#define IP_ADDR_LEN 4

#define ARP_FRAME_TYPE 0x0806

#define ETHER_HW_TYPE 1

#define IP_PROTO_TYPE 0x0800

#define OP_ARP_REQUEST 2

#define DEFAULT_DEVICE “eth0”

Char usage[]={“send_arp:sends out custom ARP packet.yurivolobuev97\n\tusage:

Send_arp src_ip_addr src_hw_addr targ_ip_addrtar_hw_addr\n\n”};

Struct arp_packet

{

U_char targ_hw_addr[ETH_HW_ADDR_LEN];

U_char src_hw_addr[ETH_HW_ADDR_LEN];

U_short frame_type;U_short hw_type;

U_short prot_type;

U_char hw_addr_size;

U_char prot_addr_size;

U_short op;

U_char sndr_hw_addr[ETH_HW_ADDR_LEN];

U_char sndr_ip_addr[IP_ADDR_LEN];U _char rcpt_hw_addr[ETH_HW_ADDR_LEN];

U_char rcpt_ip_addr[IP_ADDR_LEN];

U_char padding[18];

};

26

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 27/48

void die(char*);

void get_ip_addr(struct in_addr*,char*);

void get_hw_addr(char*,char*);

int amin(int argc,char** argv)

{

struct in_addr src_in_addr,targ_in_addr;

struct arp_packet pkt;

struct sockaddr sa;

int sock;

if(argc!=5)

die(usage);

sock=socket(AF_INET,SOCK_PACKET,htons(ETH_P_RARP));

if(sock<0)

{

perror(“socket”);

exit(1);

}

pkt.frame_type=htons(ARP_FRAME_TYPE);pkt.hw_type=htons(ETHER_HW_TYPE);

pkt.prot_type=htons(IP_PROTO_TYPES);

pkt.hw_addr_size=ETH_HW_ADDR_LEN;

pkt.prot_addr_size=IP_ADDR_LEN;

pkt.op=htons(OP)_ARP_REQUEST);

get_hw_addr(pkt.targ_hw_addr,argv[4]);

get_hw_addr(pkt.rcpt_hw_addr,argv[4]);get_hw_addr(pkt.src_hw_addr,argv[2]);

get_hw_addr(pkt.sndr_hw_addr,argv[2]);

get_ip_addr(&src_in_addr,argv[1]);

get_ip_addr(&targ_in_addr,argv[3]);

27

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 28/48

memcpy(pkt.sndr_ip_addr,&src_in_addr,IP_ADDR_LEN);

memcpy(pkt.rcpt_ip_addr,&targ_in_addr,IP_ADDR_LEN);

bzero(okt.padding,18);

strcpy(sa.sa_data,DEFAULT_DEVICE);

if(sendto(sock,&pkt,sizeof(pkt),0,&sa,sizeof(sa))<0)

{

perror(“sendto”);

exit(1);

}

exit(0);

}

void die(char* str)

{

fprintf(stderr,”%s\n”,str);

exit(1);

}

Void get_ip_addr(struct in_addr* in_addr,char* str){

struct hostnet *hostp;

in_addr->s_addr=inet_addr(str);

if(in_addr->s_addr==-1)

{

if(hostp=gethostbyname(str)))

bcopy(hostp->h_addr,in_addr,hostp->h_length);else

{

fprintf(stderr,”send_arp:unknown host %s\n”,str);

exit(1);

28

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 29/48

}

}

}

void get_hw_addr(char* buf,char* str)

{

int I;

char c,val;

for(i=0;i<ETH_HW_ADDR_LEN;i++)

{

if(!(c=tolower(*str++)))

die(“Invalid hardware address”);

if(isdigit©)

val=c-‘0’;

else if(c>=’a’&&c<=’f’)

val=c-‘a’+10;

else

die(“Invalid hardware address”);if(isdigit(c))

val=c-‘0’;

else if(c>=’a’&&c<=’f’)

val=c-‘a’+10;

else

die(“Invalid hardware address”);

*buff++=val;If(*str==’:’)

str++;

}

}

29

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 30/48

 

OUTPUT

[administrator @localhost ~]#cc sniffer.c

[administrator @localhost~]# ./a.out

send_arp:sends outcustom ARP packet yuri volobuev’97

30

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 31/48

RESULT:

  The program has been executed successfully and output is verified.

Ex No:8

Date: 04-03-2011

  TCP SOCKETS(CLIENT & SERVER)

AIM:

  To write a UNIX program to exchange message between client and server using

TCP socket

31

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 32/48

ALGORITHM:

TCP FOR SERVER:

Step 1: Start the process

Step 2: Include the necessary header files

Step 3: Declare necessary variables s1,c1,clilen as integer buf[100],s as char 

Step 4: Create the socket and check the condition

If(s1,(struct sockaddr*)&ser,sizeof(ser))==-1)

If the condition is true then print bind error 

Step 5: Find cli by using sizeof() function and assign to the clilen

Step6: Check if ((c1=accept(s1,(structsockaddr*)&cli,&client))==-1) if the condition is

True print the error is accepted

Step 7: Check if ((n=read(c1,buf,sizeof(buf)))!=-1) true get the message from the client

Step 8: Store the client message in buffer 

Step 9: write the c1,buf and sizeof the buffer 

Step 10: Compile and execute the program

Step 11: Stop the process

TCP FOR CLIENT:

Step 1: Start the process

Step 2: Include the necessary header files

Step 3: Declare necessary variables s1,c1,clilen as integer buf[100],s as char 

Step 4: Create the socket and check the condition

If(s1,(struct sockaddr*)&ser,sizeof(ser))==-1)

If the condition is true then print bind error 

32

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 33/48

Step 5: Find cli by using sizeof() function and assign to the clilen

Step6: Check if (connect(s1(struct sockaddr*)&seri,&sizeof(ser))=-1) if the condition

Print the connect error 

Step 7: Get the data and write the s1,buf,sizeof(buf)

Step 8: Check if ((n=read(s1,buf,sizeof(buf)))!=-1) true get the message from the Server 

Step 9:Else read the error 

Step 10: Compile and execute the program

Step 11: Stop the process

PROGRAM: 

SERVER:

#include<stdio.h>

#include<sys/types.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<string.h>

33

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 34/48

Int main(0

{

Struct sockaddr_in ser,cli;

Int s1,c1,n,clilen;

Char buf[100],s;

s1=socket(AF_INET,SOCK_STREAM,0);

 bzero((char*)&ser,sizeof(ser));

ser.sin_family=AF_INET;

ser.sin_port=htons(5000);

ser.sin_addr.s_addr=htonl(INADDR_ANY);

if(bind(s1,(struct sockaddr*)&ser,sizeof(ser))=-1)

{

 printf(“BIND ERROR”);

exit(1);

}

listen(s1,5);

 bzero((char*)&cli,sizeof(cli));

clilen=sizeof(cli);

if(c1=accept(s1,(struct sockaddr*)&cli,&clilen))==-1)

{

 printf(“Accept error”);

exit(1);

}

If(n=read(c1,buf,sizeof(buf)))!=-1)

{

34

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 35/48

 buf[n]=’\0’;

 printf(“message from client\t\t : %s”,buf);

fputs(buf,stdout);

 printf(“\n\n”);

}

write(c1,buf,sizeof(buf));

close(c1);

close(s1);

}

CLIENT:

#include<stdio.h>

#include<sys/types.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<string.h>

int main(0

35

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 36/48

{

Struct sockaddr_in ser,cli;

Int s1,c1,n,clilen;

Char buf[100],s;

s1=socket(AF_INET,SOCK_STREAM,0);

 bzero((char*)&ser,sizeof(ser));

ser.sin_family=AF_INET;

ser.sin_port=htons(5000);

ser.sin_addr.s_addr=htonl(INADDR_ANY);

if(connect(s1,(struct sockaddr*)&ser,sizeof(ser))==-1)

{

 printf(“connect error”);

exit(1);

}

 printf(“Enter data\\t\t”);

fgets(buf,100,stdin);

write(s1,buf,sizeof(buf));

if(n=read(s1,buf,sizeof(buf)))!=-1)

{

 buf[n]=’\0’;

 printf(“message from server\t\t: %s”,buf);

 printf(“\n\n”);

}

Else

{

36

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 37/48

 printf(“read error”);

exit(1);

}

close(s1);

exit(0);

}

OUTPUT:

SERVER:

[admin@niitm]$cc tcp1.c

[admin@niitm]$./a.out

Messagae send from Client : haii

CLIENT:

37

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 38/48

[admin@niitm]$cc tcp2.c

[admin@niitm]$./a.out

Enter the data :haii

Message from the server :haii

RESULT:

  The program was executed successfully and the output was verified.

Ex No:9

Date: 17-03-2011

  UDP SOCKETS(CLIENT & SERVER)

AIM:

  To write a UNIX program to exchange message between client and server using

UDP socket 

38

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 39/48

ALGORITHM:

UDP SOCKET FOR SERVER:

Step 1: Start the process

Step 2: Include the necessary header files

Step 3: Declare necessary variables

Step 4: Create socket(AF_INET,SOCK_DGRAM,) and store in s1

Step 5: Bind the socket address usiing bind() function

Step 6: Find the client sizeof using function and store it in n

Step 7: Receive the message from the client by using recvfrom()

Step 8: Get the message

Step 9: Send to the client by using sendto()

Step 10: Compile and execute the program

Step 11: Stop the process

UDP SOCKET FOR CLIENT:

Step 1: Start the process

Step 2: Include the necessary header files

Step 3: Declare necessary variables

Step 4: Create socket(AF_INET,SOCK_DGRAM,) and store in s1

Step 5: send to the client by using

Sendto(s1,buf,100,0(struct sockaddr*)&cli,sizeof(cli));

39

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 40/48

Step 6: Receive the message from server by using

recvfrom(s1,buf,100,0,NULL,NULL)

Step 7: Print the message

Step 8: Compile and execute the program

Step 9: Stop the process

PROGRAM:

SERVER:

#include<stdio.h>

#include<string.h >

#include<sys/socket.h>

#include<netinet/in.h>

int main()

40

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 41/48

{

Struct sockaddr_in ser,cli;

int s1,n,t;

char buf[100];

s1=socket(AF_INET,SOCK_DGRAM,0);

ser.sin_family=AF_INET;

ser.sin_port=htons(5000);

ser.sin_addr.s_addr=inet _addr(“127.0.0.1”);

 bind(s1,(struct sockaddr*)&ser,sizeof(ser));

n=sizeof(cli);

recvfrom(s1,buf,100,0(structaddr*)&cli,&n);

 printf(“message : %s”,buf);

sendto(s1,buf,100,0,(struct sockaddr*)&cli,sizeof(cli));

return 1;

}

CLIENT:

#include<stdio.h>

#include<string.h >

#include<sys/socket.h>

#include<netinet/in.h>

int main()

{

41

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 42/48

Struct sockaddr_in ser,cli;

int s1,n,t;

char buf[100];

 printf(“Enetr the message”);

fgets(buf,sizeof(buf),stdin);

s1=socket(AF_INET,SOCK_DGRAM,0);

ser.sin_family=AF_INET;

ser.sin_port=htons(5000);

ser.sin_addr.s_addr=inet _addr(“127.0.0.1”);

 bind(s1,(struct sockaddr*)&ser,sizeof(ser));

n=sizeof(cli);

sendto(s1,buf,100,0,(struct sockaddr*)&ser,sizeof(ser));

recvfrom(s1,buf,100,0,NULL,NULL);

  printf(“msg : %s”,buf);

return 1;

}

OUTPUT: 

SERVER: 

[admin@niitm]$cc udp1.c

[admin@niitm]$./a.out

Message :welcome

42

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 43/48

CLIENT: 

[admin@niitm]$cc udp2.c

[admin@niitm]$./a.out

Enter the message : welcome

Msg : welcome

RESULT:

  The program was executed successfully and the output was verified

Ex No:10

Date: 25-03-2011

  URL CLASS TO DOWNLOAD WEB PAGES

AIM:

  To write a UNIX program using URL Class to download web page

ALGORITHM:

Step 1: Start the process43

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 44/48

Step 2: Include the necessary header files

Step 3: Cfreate error function by passing pointer varriable *msg

Step 4: Declare necessary variables

Step 5: check arg<3 if it is true then print the usage host name

Step 6: Create the socket and assign too the sockfd

Step 7: Check sockfd<0 print error opening socket

Step 8: Get the hostname and assign it to the server 

Step 9: Check server==NULL then print error,no such host

Step 10: By using copy function copy the host address,server addressand find the host

Length

Step 11: Check the condition if(connect(sockfd,&serv_addr,sizeof(serv_addr))<0) then

Display error in connection

Step 12: write the sockfd,buffer,strlen,(buffer) and store it in a variable n

Step 13:Check n<0 then error while writing to the socket

Step 14: Compile and execute the program

Step 15: Stop the process

PROGRAM:

#include<stdio.h>

#include<sys/types.h>

#include<sys/socket.h>

#include<netinet/in.h>

#include<netdb.h>

Void error(char *msg)

{

44

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 45/48

 perror(msg);

exit(0);

}

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

{

Int sockfd,portno,n;

struct sockaddr_in serb_addr;

struct hostent *server;

struct hostent *host;

char buffer[1028];

char tbuffer[1028];

if(argc<3)

{

fprintf(stderr,”usage %s hostname port\n”,argv[0])0;

exit(0);

}

 portno= atof(argv[0]);

sockfd=socket(AF_INET,SOCK_STREAM,0);

if(sockfd<0)

error(“error opening file”);

server=gethostbyname(argv[1]);

if(server==NULL)

{

fprintf(stderr,”error no such host”);

exit(0);

45

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 46/48

}

 bzero((char*)&serv_addr,sizeof(serv_addr));

serv_addr.sin_family=AF_INET;

 bcopy((char*)server->h_addr,(char*)&serv_addr.sin_addr.s_addr,server->length);

serv_addr.sin.port=htons(portno);

if(connect(sockfd,&serv_addr,sizeof(serv_addr))<0)

error(:”error connecting”);

sprintf(buffer,”GET/HTTP/1.1/nuser_agent:url/7.16.3\n

Host:%s\naccepted:\n\n argv[1]);

n=write(sockfd,buffer,strlen(buffer);

if(n<0)

error(“error writing to socket”);

 bzero(tbuffer,256);

while(n>0)

{

 bzero(tbuffer,256);

n=read(socketfd,tbuffer,255);

if(n<0)

 printf(“error reading from the socket”);

 printf(“%s”,tbuffer);

}

return 0;

}

46

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 47/48

OUTPUT:

[admin@niitm]$cc url1.c

[admin@niitm]$./a.out www.google.com 80

47

8/3/2019 Unix Rec Full New

http://slidepdf.com/reader/full/unix-rec-full-new 48/48

RESULT:

  The program was executed successfully and the output was verified