0% found this document useful (0 votes)
51 views53 pages

Unix LAB MANUAL

The document describes a C program to implement a chat application using TCP/IP sockets. The chat server creates a TCP socket, binds an address and port, listens for connections from clients and sends/receives messages. The chat client connects to the server, sends/receives messages, and closes the socket. The algorithms show the server accepting connections in a loop and both client and server sending/receiving messages in a loop until termination.

Uploaded by

Rahul Anand
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views53 pages

Unix LAB MANUAL

The document describes a C program to implement a chat application using TCP/IP sockets. The chat server creates a TCP socket, binds an address and port, listens for connections from clients and sends/receives messages. The chat client connects to the server, sends/receives messages, and closes the socket. The algorithms show the server accepting connections in a loop and both client and server sending/receiving messages in a loop until termination.

Uploaded by

Rahul Anand
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 53

Ex No: 01 Socket Programming Using TCP Sockets

Date:

AIM:
To write a C Program to implement a simple socket program using TCP
sockets.

ALGORITHM:

TCP server:
Step 1: Create a TCP socket using SOCK_STREAM.
sd = socket(AF_INET, SOCK_STREAM, 0)
Step 2: Specify the family, port and address for the server.
Step 3: Bind the address and port to ensure the association of bidirectional
information.
Step 4: listen to the client using l = listen(sid, 5).
Step 5: Accept the connection from the client using
a = accept(sid, (struct sockadd *) &cliaddr, &clilen)
Step 6: Receive the message from the client using
r = recv (sid, buffer, 1023)
Step 7: Print the message in the buffer
Step 8: Close the socket.
Step 9: Stop the process.

TCP client:
Step 1: Create a TCP socket using SOCK_STREAM
cd = socket(AF_INF, SOCK_STREAM, 0)
Step 2: Get the host name by using
h = gethostbyname( argv[1])

SAN INTERNATIONAL INFO SCHOOL Page | 1


Step 3: Connect to the server using
c = connect (sid, (struct sockaddr *) &servaddr, sizeof(servaddr)).
Step 4: Send the message to the server using
s = send(sid, buffer, strlen(buffer))
Step 5: Close the socket.
Step 6: Stop the process.

TCP
Server Program

/* tcpserver*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include<errno.h>

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


{
int sockfd, newsockfd, portno, clilen;
char buffer[256];
struct sockaddr_in serv_addr, cli_addr;
int n;

if (argc < 2)
{
fprintf(stderr,"ERROR, no port provided\n");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);

if (sockfd < 0)
error("ERROR opening socket");

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


portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);

if (bind(sockfd,struct sockaddr*)
&serv_addr,sizeof(serv_addr)) < 0)

SAN INTERNATIONAL INFO SCHOOL Page | 2


error("ERROR on binding");

listen(sockfd,5);
clilen = sizeof(cli_addr);
newsockfd = accept(sockfd,(struct sockaddr *)
&cli_addr,&clilen);

if (newsockfd < 0) error("ERROR on accept");

bzero(buffer,256);
n = read(newsockfd,buffer,255);

if (n < 0) error("ERROR reading from socket");


printf("Here is the message: %s\n",buffer);

n = write(newsockfd,"I got your message",18);

if (n < 0) error("ERROR writing to socket");


return 0;
}

Client Program

/*tcpclient*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include<errno.h>

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


{
int sockfd, portno, n;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];

if (argc < 3)
{
fprintf(stderr,"Enter Hostname and Portnumber");
exit(1);
}

portno = atoi(argv[2]);
SAN INTERNATIONAL INFO SCHOOL Page | 3
sockfd = socket(AF_INET, SOCK_STREAM, 0);

if (sockfd < 0)
printf("ERROR opening socket");

server = gethostbyname(argv[1]);

if (server == NULL)
{
fprintf(stderr,"ERROR, no such host\n");
exit(0);
}

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->h_length);
serv_addr.sin_port = htons(portno);

if (connect(sockfd,(struct sockaddr*)
&serv_addr,sizeof(serv_addr)) < 0)
printf("ERROR connecting");

printf("Please enter the message: ");


bzero(buffer,256);
fgets(buffer,255,stdin);
n = write(sockfd,buffer,strlen(buffer));

if (n < 0)
error("ERROR writing to socket");
bzero(buffer,256);
n = read(sockfd,buffer,255);

if (n < 0)
printf("ERROR reading from socket");

printf("%s\n",buffer);
return 0;
}

SAN INTERNATIONAL INFO SCHOOL Page | 4


Output
Server

[prabha@localhost ~]$ gcc tcpserver.c


[prabha@localhost ~]$ ./a.out 2500

Here is the message: welcome to LOYOLA

[prabha@localhost ~]$

Client
[prabha@localhost ~]$ gcc tcpclient.c
[prabha@localhost ~]$ ./a.out localhost 2500

Please enter the message: welcome to LOYOLA

I got your message

[prabha@localhost ~]$

Result:
Thus the C program to implement a simple socket programming using
TCP sockets has been completed successfully.

SAN INTERNATIONAL INFO SCHOOL Page | 5


Ex No: 02 Socket Programming Using Udp Sockets
Date:

AIM:
To write a C program to implement a simple socket program using UDP
sockets.

ALGORITHM:

UDP Server:
Step 1: Create a UDP socket using SOCK_DGRAM.
sd = socket(AF_INET, SOCK_DGRAM,0)
Step 2: Specify the family, port and address for both server and client.
Step 3: Bind the address and port of the server with the client using
b = bind(sd, (struct sockaddr *) & servaddr, sizeof (servaddr))
Step 4: Calculate the size of client address.
Step 5: Send the information from the server to client .
Step 6: Close the socket.
Step 7: Stop the process.
UDP Client:
Step 1:Create a UDP socket using SOCK_DREAM.
sd = socket(AF_INET, SOCK_DGRAM,0)
Step 2: Specify the family, port and address for both server and client.
Step 3: Bind the address and port of the client with the server using
b = bind(sd, (struct sockaddr *) & cliaddr, sizeof (cliaddr))
Step 4: Receive the information from the server.
Step 5: Close the socket.
Step 6: Stop the process.

UDP Server program

SAN INTERNATIONAL INFO SCHOOL Page | 6


/*udpserver*/
#include<string.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>

int main()
{
char x[15],y[15];
int n,sd,size,sersock;
struct sockaddr_in cliaddr,servaddr;

cliaddr.sin_family=AF_INET;
cliaddr.sin_port=htons(8330);
cliaddr.sin_addr.s_addr=INADDR_ANY;

servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(8331);
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);

bzero(&(cliaddr.sin_zero),8);
bzero(&(servaddr.sin_zero),8);

sd=socket(AF_INET,SOCK_DGRAM,0);

bind(sd,(struct sockaddr *)&servaddr,sizeof(servaddr));


size=sizeof(cliaddr);

printf("\nWaiting to receive the data... \n");

n=recvfrom(sd,&x,15,0,(struct sockaddr *)&cliaddr,&size);


printf("\n%s",x);

printf("\nData received successfully... \n");


close(sd);
}

Client Program
/*udpclient*/
#include<sys/socket.h>
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>

SAN INTERNATIONAL INFO SCHOOL Page | 7


#include<netinet/in.h>
int main()
{
char x[15],y[15];
int n,sd,size,sersock;
struct sockaddr_in cliaddr,servaddr;

cliaddr.sin_family=AF_INET;
cliaddr.sin_port=htons(8330);
cliaddr.sin_addr.s_addr=INADDR_ANY;

servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(8331);
servaddr.sin_addr.s_addr=INADDR_ANY;

bzero(&(cliaddr.sin_zero),8);
bzero(&(servaddr.sin_zero),8);

sd=socket(AF_INET,SOCK_DGRAM,0);

bind(sd,(struct sockaddr *)&cliaddr,sizeof(cliaddr));

size=sizeof(servaddr);

printf("\nEnter the data : \n");


scanf("%s",x);

sendto(sd,x,sizeof(x),0,(struct sockaddr *)
&servaddr,size);
printf("\nData sent successfully... \n");

close(sd);
}

SAN INTERNATIONAL INFO SCHOOL Page | 8


UDP Output
Server
[prabha@localhost ~]$ gcc udpserver.c
[prabha@localhost ~]$ ./a.out

Waiting to receiver the data…

Hi

Data received successfully…


[prabha@localhost ~]$

Client
[prabha@localhost ~]$ gcc udpclient.c
[prabha@localhost ~]$ ./a.out
hi

Enter the data:


Hi Server

Data sent successfully…


[prabha@localhost ~]$

Result:
Thus the C Program to implement a simple socket program using
sockets has been completed successfully.

SAN INTERNATIONAL INFO SCHOOL Page | 9


Ex No: 03 Chat Application using sockets
Date:

AIM:
To write a C Program to implement chat application using TCP/IP
Sockets.

ALGORITHM:

Chat server:
Step 1: Create a TCP/IP socket.
Step 2: Initialize socket address structure with protocol family, port number
and IP address.
Step 3: Bind the socket.
Step 4: Convert the socket into server socket by invoking listen function.
Step 5: Accept the connection from the client using accept function.
Step 6: Send and receive the message to and from client.
Step 7: Close the socket.
Step 8: Stop the process.
Chat Client:
Step 1: Create a TCP/IP socket.
Step 2: Initialize socket address structure with protocol family, port number
and IP address.
Step 3: Invoke the convert function to connect to the server.
Step 4: Send and receive the message to and from server.
Step 5: Close the socket.
Step 6: Stop the process.

SAN INTERNATIONAL INFO SCHOOL Page | 10


Chat Application
Server program

#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<netdb.h>
#include<string.h>
#define SERV_TCP_PORT 1433
#define MAX 1024

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


{
int sockfd,newsockfd,clientlen;
char buffer[MAX];

struct sockaddr_in serv_addr,cli_addr;

sockfd = socket(AF_INET,SOCK_STREAM,0);

serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=INADDR_ANY;
serv_addr.sin_port=htons(SERV_TCP_PORT);

bind(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
listen(sockfd,5);
clientlen=sizeof(cli_addr);
newsockfd=accept(sockfd,(struct sockaddr*)
&cli_addr,&clientlen);
do
{
bzero(buffer,MAX);
read(newsockfd,buffer,MAX);
printf(“\nClient message: %s”,buffer);
bzero(buffer,MAX);
printf(“\nServer message :”);
fgets(buffer,MAX,stdin);
write(newsockfd,buffer,MAX);
printf(“\n”);
}while(strcmp(buffer,”bye\n”)!=0);
close(sockfd);
return(0);
}

SAN INTERNATIONAL INFO SCHOOL Page | 11


Client Program

#include<stdio.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<netdb.h>
#include<string.h>
#define SERV_TCP_PORT 1433
#define MAX 1024

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


{
int sockfd;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[MAX];

sockfd=socket(AF_INET,SOCK_STREAM,0);

serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=inet_addr(“127.0.0.1”);
serv_addr.sin_port=htons(SERV_TCP_PORT);

connect(sockfd,(struct sockaddr*)
&serv_addr,sizeof(serv_addr));

do
{
bzero(buffer,MAX);
printf(“\nClient message :”);
fgets(buffer,MAX,stdin);
write(sockfd,buffer,MAX);
bzero(buffer,MAX);
read(sockfd,buffer,MAX);
printf(“\nServer message : %s”,buffer);
printf(“\n”);
}while(strcmp(buffer,”bye\n”)!=0);

close(sockfd);

return(0);
}

SAN INTERNATIONAL INFO SCHOOL Page | 12


Chatting Output
Server
[prabha@localhost ~]$ gcc chatserver.c
[prabha@localhost ~]$ ./a.out

Client message : Hi
Server message : Hi da

Client message : How are you?


Server message : I am fine da

Client message : bye


Server message : bye

[prabha@localhost ~]$

Client
[prabha@localhost ~]$ gcc chatclient.c
[prabha@localhost ~]$ ./a.out

Client message : Hi
Server message : Hi da

Client message : How are you?


Server message : I am fine da

Client message : bye


Server message : bye

[prabha@localhost ~]$

Result:
Thus the c program to implement chat application using TCP/IP sockets
has been completed successfully.

SAN INTERNATIONAL INFO SCHOOL Page | 13


Ex No: 04 Simple Sliding window Protocol
Date:

AIM:
To write a C program to simulate a simple sliding window protocol.

ALGORITHM:
Server:
Step 1: Start the process.
Step 2: Open the file which contains the frames to be sent.
Step 3: Get the starting and ending frame numbers to be sent.
Step 4: Read the frames from the file and send that frames to client side.
Step 5: Stop the process.

Client:
Step 1: Start the process.
Step 2: Enter the number frames going to be received.
Step 3: Receive the frames one by one.
Step 4: After receiving all the frames, check the frames received order.
Step 5: If it is received in correct order, print as frames were received in correct
sequence, otherwise print frames were not received in correct sequence.
Step 6: Stop the process.

Sliding Window
SAN INTERNATIONAL INFO SCHOOL Page | 14
Server program

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>

struct mymsgbuf
{
long mtype;
char mtext[25];
};

FILE *fp;

int main()
{
Struct mymsgbuf buf;
int si,ei,sz;
int msgid;
int i=0,s;
int a[100];
char d;

if((fp=fopen(“send”,”r”))= = NULL)
printf(“\nFile opened”);

printf(“\nEnter starting and ending index of


frame array: “);
scanf(“%d%d”,&si,&ei);
sz=ei-si;

if((msgid=msgget(80,IPC_CREAT|0666))= = -1)
{
printf(“\nError inMSGGET”);
exit(0);
}
while(!feof(fp))
{
d=getc(fp);
a[i]=d;
i++;
}

SAN INTERNATIONAL INFO SCHOOL Page | 15


S=i;
buf.mtype=1;
for(i=si;i<=ei;i++)
{
buf.mtext[i]=a[i];
}
for(i=si;i<=ei;i++)
printf(“\t%c”,buf.mtext[i]);
for(i=0;i<=sz;i++)
{
if((msgsnd(msgid,&buf,sizeof(buf),0))= = -1)
{
printf(“\nError in MSGSND”);
exit(0);
}
}
printf(“\nFrames send”);
return 0;
}

Client program

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/types.h>
#include<sys/ipc.h>
#include<sys/msg.h>

struct mymsgbuf
{
long mtype;
char mtext[25];
};

FILE *fp;

int main()
{
Struct mymsgbuf buf;
int msgid;
int i=0,s;
int count=0,frmsz;
int a[100];

SAN INTERNATIONAL INFO SCHOOL Page | 16


char d;

if((msgid=msgget(89,IPC_CREAT|0666))= = -1)
{
printf(“\nError inMSGGET”);
exit(0);
}
printf(“\nEnter the frame size:”);
scanf(“%d”,&frmsz);
if((fp=fopen(“check”,”r”))= = NULL)
printf(“\nFile not opened”);
else
printf(“\nFile opened”);

while(!feof(fp))
{
d=getc(fp);
a[i]=d;
i++;
}
S=i;
for(i =0;i<frmsz;i++)
printf(“\t %c”,a[i]);

for(i=0;i<frmsz;i++)
{
if((msgrcv(msgid,&buf,sizeof(buf),0,1))= = -1)
{
printf(“\nError in MSGRCV”);
exit(0);
}
Printf(“\nFrame received is :%c”,buf.mtext[i]);
}
for(i=0;i<frmsz;i++)
{
if(a[i]= =buf.mtext[i])
count++;
}
if(count= =0)
{
printf(“\nFrames were not received in
correct sequence”);
printf(“%d”,count);
exit(0);
}
if(count==frmsz)

SAN INTERNATIONAL INFO SCHOOL Page | 17


{
Printf(“\nFrames were received in correct sequence”);
else
{
Printf(“\nFrames were not received in
correct sequence”);
}
}

Sliding Window Output


SAN INTERNATIONAL INFO SCHOOL Page | 18
[prabha@localhost ~]$ cat check
Networking
[prabha@localhost ~]$

[prabha@localhost ~]$ cat send


Networking

Server
[prabha@localhost ~]$ gcc slidserver.c
[prabha@localhost ~]$ ./a.out

File opened
Enter starting and ending index of frame array :0 6
N e t w o r k

Frames send

[prabha@localhost ~]$

Client
[prabha@localhost ~]$ gcc slidclient.c
[prabha@localhost ~]$ ./a.out

Enter the frame size: 7

File opened n e t w o r k

Frame received is : n
Frame received is : e
Frame received is : t
Frame received is : w
Frame received is : o
Frame received is : r
Frame received is : k

Frames were received in correct sequence


[prabha@localhost ~]$

Result:
Thus the C program to simulate a simple sliding window protocol has
been completed successfully.

Ex No: 05 Selective Repeat and Go Back – N


SAN INTERNATIONAL INFO SCHOOL Page | 19
Date:

AIM:
To write a c program to simulate selective repeat and go back-N sliding
window protocol.
ALGORITHM:
Step 1: Start the process.
Step 2: Enter the number of frames.
Step 3: If your option is selective repeat
Enter the frames one by one
Step 4: Send the frames one by one and receive acknowledgements for the sent
frames.
Step 5: If you received NACK, retransmit the frame for which you received
NACK, otherwise no need to retransmit.
Step 6: If your option is go-back-N
Send the frames one by one & receive acknowledgement for the sent
frames.
Step 7: If you received NACK, retransmit the frames from that frame for which
received NACK.
Step 8: Stop the Process.

Sliding Window

SAN INTERNATIONAL INFO SCHOOL Page | 20


Program

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

int main()
{
int data[50],ack[10];
int m,n,a,i,d,index=0;

p:

printf(“\n SLIDING WINDOW PROTOCOL \n 1. Selective Repeat \n


2. Go Back-N \n 3. Exit);
printf(“\n \n Enter your option :”);
scanf(“%d”,&a)

if(a==3)
exit(0);
else
{
printf(“\n Enter the size of the data :”);
scanf(“%d”,&n);
printf(“\n Enter the data one by one : “);
for(i=0;i<n;i++)
scanf(“%d”,&data[i]);
switch(a)
{
case 1:
{
printf(“\n Data is ready to send\n”);
for(i=0;i<n;i++)
ack[i]=0;

do
{
index=0;
for(i=0;i<n;i++)
{
if(ack[i]==0)
{
printf(“\n%d is sent”,data[i]); printf(“\nEnter the
ack[1-ACK: 0-NACK] :”);
scanf(“%d”,&ack[i]);
}

SAN INTERNATIONAL INFO SCHOOL Page | 21


}

for(i=0;i<n;i++)
if(ack[i]==0)
{
printf(“\n Received negative ack for frame
%d”,i+1);
index=1;
}
}while(index!=0);
printf(“\n Do you want to continue[1- Yes:0-No]:”);
scanf(“%d”,&d);
if(d==1)
goto p;
if(d==0)
exit(0);
break;
}
case 2:
{
for(i=0;i<n;i++)
{
printf(“\n %d is sent”,data[i]);
printf(“\nEnter the ack [1-ACK : 0-NACK] :”);
scanf(“%d”,&ack[i]);
printf(“%d”,ack[i]);
if(ack[i]==1)
printf(“\n Received ack”);
else
{
printf(“\n Negarive ack for %d”,data[i]);
if(i==0)
i=-1;
else
i=i-1;
}
}
printf(“\nDo you want to continue [1-Yes:0-No]: “);
scanf(“%d”,&d);
if(d==1)
goto p;
if(d==0)
exit(0);
break;
}

SAN INTERNATIONAL INFO SCHOOL Page | 22


case 3:
{
exit(0);
break;
}
}
}
return 0;
}

Sliding Window Output

SAN INTERNATIONAL INFO SCHOOL Page | 23


[prabha@localhost ~]$ gcc slid.c

[prabha@localhost ~]$ ./a.out

SLIDING WINDOW PROTOCOL

1. Selective Repeat
2. Go Back-N
3. Exit

Enter your option :1

Enter the size of the data :3

Enter the data one by one :123

Data is ready to send

1 is sent
Enter the ack [1-ACK : 0-NACK} :1

2 is sent
Enter the ack [1-ACK : 0-NACK) :0

3 is sent
Enter the ack [1-ACK : 0-NACK} :1

Received negative ack for frame 2


2 is sent
Enter the ack [1-ACK : 0-NACK} :1

Do you want to continue [1-Yes : 0-No] :1

SLIDING WINDOW PROTOCOL

1. Selective Repeat
2. Go Back-N
3. Exit

Enter your option :2

Enter the size of the data :3

Enter the data one by one :123

1 is sent

SAN INTERNATIONAL INFO SCHOOL Page | 24


Enter the ack [1-ACK : 0-NACK} :1

1
Received ack
2 is sent
Enter the ack [1-ACK : 0-NACK} :0

0
Negative ack for 2
2 is sent
Enter the ack [1-ACK : 0-NACK} :1

1
Received ack
3 is sent
Enter the ack [1-ACK : 0-NACK} :1

1
Received ack

3 is sent
Enter the ack [1-ACK : 0-NACK} :1

1
Received ack
Do you want to continue [1-Yes : 0-No] :1

SLIDING WINDOW PROTOCOL

1. Selective Repeat
2. Go Back-N
3. Exit

Enter your option :3

Result:

Thus the c program to simulate selective repeat and Go Back-N sliding


window protocols has been completed successfully.

Ex No: 06 Slow Start Window Management


Date: Technique
SAN INTERNATIONAL INFO SCHOOL Page | 25
AIM:
To write a c program to simulate a slow start window management
technique.

ALGORITHM:

Server:
Step 1: Start the process.
Step 2: Create a TCP socket.
Step 3: Assign family, port and address to the socket.
Step 4: Bind the socket.
Step 5: Listen to the client and accept the connection from the client.
Step 6: Initialize cwnd to 1
i) Read the message from client.
ii) Print the message
iii) Send the acknowledgement to the client.
iv) Increment cwnd as cwnd*=2(exponentially).
Step 7: Close the socket.
Step 8: Stop the process.

Client:
Step 1: Start the process.
Step 2: Create a TCP socket.

Step 3: Assign family, port, and address to the client.


Step 4: Connect with the server.
Step 5: Initialize cwnd to 1

SAN INTERNATIONAL INFO SCHOOL Page | 26


i) Write the message to the server.
ii) Read the ACK from the server
iii) Print the ACK
Step 6: Increment the cwnd as cwnd*=2(exponentially).
Step 7: Close the socket.
Step 8: stop the process.

Slow Start Window Management Technique


Server program:

#include<stdio.h>

SAN INTERNATIONAL INFO SCHOOL Page | 27


#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#define MAX 10

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


{
int sid,newsid,clen;
char buffer[1024];
int cwnd,count=0,dummy;
struct sockaddr_in saddr,caddr;
int n;

if(argc<2)
{
fprintf(stderr,“Error:Port number is not given\n”);
exit(1);
}

sid=socket(AF_INET,SOCK_STREAM,0);
bzero((char*)&addr,sizeof(saddr));
saddr.sin_family=AF_INET;
saddr.sin_port=htons(atoi(argv[1]));
saddr.sin_addr.s_addr=INADDR_ANY;

if(bind(sid,(struct sockaddr*)&saddr,sizeof(saddr))<0)
{
perror(“socket_bind”);
}

listen(sid,5);
clen=sizeof(struct sockaddr_in);
newsid=accept(sid,(struct sockaddr*)&caddr,&clen);

if(newsid<0)
{
perror(“socket_accept”);
}
bzero(buffer,1024);
for(cwnd=1;cwnd<=MAX;cwnd*=2)
{
printf(“\n\nCurrent Window size:%d\n”,cwnd);

SAN INTERNATIONAL INFO SCHOOL Page | 28


for(dummy=1;dummy<=cwnd;dummy++)
{
n=read(newsid, buffer, 1023);
printf(“Message from client:%s”, buffer);
n=write(newsid, buffer, strlen(buffer));
if(cwnd>MAX)
{
printf(“\n”);
printf(“Congestion may occur”);
}
}
printf(“\n”);
printf(“\nAck for %d message(s) sent to client”,cwnd);
printf(“\n”);
}
return 0;
}

Client program:

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<string.h>
#include<errno.h>
#include<netdb.h>
#define MAX 10

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


{
int cid,sid;
struct sockaddr_in saddr;
char buffer[1024];
int i,n,length,count=0,cwnd,dummy;
struct hosten* h;

if(argc<3)
{
fprintf(stderr, “Enter Hostname and Portnumber”);
exit(1);
}
cid=socket(AF_INET, SOCK_STREAM, 0);

if(cid<0)

SAN INTERNATIONAL INFO SCHOOL Page | 29


{
perror(“Socket”);
}
h=gethostbyname(argv[1]);

if(h==NULL)
{
printf(“Host is not found”);
exit(1);
}
saddr.sin_family=AF_INET;
saddr.sin_port=htons(atoi(argv[2]));
bcopy((char*)h->h_addr,(char*)&saddr.sin_addr.s_addr,
h->h_length);

if(connect(cid,(struct sockaddr*)&saddr,sizeof(saddr))<0)
{
perror(“Socket_connect”);
}
bzero(buffer,1024);
for(cwnd=1;cwnd<=MAX;cwnd*=2)
{
printf(“\nCurrent Window Size:%d\n”,cwnd);
for(dummy=1; dummy<=cwnd; dummy++)
{
printf(“\nEnter your message to server:”);
fgets(buffer,1023,stdin);
n=write(cid,buffer,strlen(buffer));
n=read(cid, buffer, strlen(buffer));
}
count++;
printf(“Ack %d from server\n”, count);
}
return o;
}

Output:
Server
[prabha@localhost ~]$ gcc slows.c

SAN INTERNATIONAL INFO SCHOOL Page | 30


[prabha@localhost ~]$ ./a.out 2551

Currrent Window Size:1


Message from Client:m1

Ack for 1 message(s) sent to client

Current Window Size:2


Message from Client:m1
Message from Client:m2

Ack for 2 messages(s) sent to client

Current Window Size:4


Message from Client:m1
Message from Client:m2
Message from Client:m3
Message from Client:m4

Ack for 4 messages(s) sent to client

Current Window Size:8


Message from Client:m1
Message from Client:m2
Message from Client:m3
Message from Client:m4
Message from Client:m5
Message from Client:m6
Message from Client:m7
Message from Client:m8

Ack for 8 messages(s) sent to client

[prabha@localhost ~]$

Client

[prabha@localhost ~]$ gcc slowc.c


[prabha@localhost ~]$ ./a.out localhost 2551

Current Window Size:1

Enter your message to Server :m1


Ack 1 from server

Current Window Size:2

SAN INTERNATIONAL INFO SCHOOL Page | 31


Enter your message to Server :m1
Enter your message to Server :m2
Ack 2 from server

Current Window Size:4

Enter your message to Server :m1


Enter your message to Server :m2
Enter your message to Server :m3
Enter your message to Server :m4
Ack 3 from server

Current Window Size:8

Enter your message to Server :m1


Enter your message to Server :m2
Enter your message to Server :m3
Enter your message to Server :m4
Enter your message to Server :m5
Enter your message to Server :m6
Enter your message to Server :m7
Enter your message to Server :m8
Ack 4 from server

[prabha@localhost ~]$

Result:
Thus the c program to simulate a slow start window management
technique has been completed successfully.

Ex No: 07 Simulation Of Routing Protocols-OSPF


Date:

SAN INTERNATIONAL INFO SCHOOL Page | 32


AIM
To write a c program to simulate the ‘open shortest path first’ routing
protocol to find the shortest path between two nodes.

ALGORITHM:
Step 1: Start the process.
Step 2: Enter the number of nodes.
Step 3: Enter the cost between the nodes.
Step 4: enter the source and destination nodes to find the shortest path.
Step 5: If the given source and destination nodes are same, print the
Source and destination nodes are same, try again.
Step 6: If no path exists between source and destination node, print no
Path exists.
Step 7: If a path exists between source and destination nodes, print the as
The shortest path with cost.
Step 8: If more than one path exists between source and destination node,
Find the shortest path by adding the cost, and print the shortest path.
Step 9: Stop the process.

OSPF
Program:

#include<stdio.h>
#include<stdlib.h>

SAN INTERNATIONAL INFO SCHOOL Page | 33


void getdata();
void findpath();
void display();
void output();
int cost1[10][10],cost[10][10],key[10][10],ch=1,num,src,
des,i,j,k,min;
main()
{
getdata();
findpath();
printf(“\nMENU\n”);
printf(“\n 1.Find Shortest path”);
printf(“\n 2.Exit”);
while(ch!=2)
{
printf(“\n\n Enter your choice”);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
printf(“\n Enter the source node and
destination node”);
scanf(“%d %d”,&src,&des);
display(src,des);
break;
case 2:
exit(0);
}
}
}
void getdata()
{
printf(“\n Enter the number of nodes”);
scanf(“%d”,&num);
for(i=1;i<=num;i++)
{
for(j=1;j<=num;j++)
{
if(i==j)
cost1[i][j]=0;
else
{
printf(“\n Enter the cost between
nodes[&d %d]”,i,j);
scanf(“%d”,&cost1[i][j]);
}

SAN INTERNATIONAL INFO SCHOOL Page | 34


cost[i][j]=cost1[i][j];
key[i][j]=0;
}
}
}
void findpath()
{
int d1,d2,d3;
for(k=1;k<=num;k++)
{
for(i=1;i<=num;i++)
{
for(j=1;j<=num;j++)
{
d1=cost[i][k];
d2=cost[k][j];
d3=cost[i][j];
if(k!=0 &&d1!=0 &&d2!=0 &&
(d3==0 ||(d1+d2)<d3))
{
cost[i][j]=d1+d2;
key[i][j]=k;
}
}
}
}
}
void display(int i,int j)
{
min=0;
if(i==j)
{
printf(“The source and destination
are same,try again”);
}
else if(cost[i][j]==0)
printf(“\n No path exists”);
else
{
printf(“The path is %d”,i);
output(i,j);
printf(“The cost is %d”,min);
}
}
void output(int i,int j)
{

SAN INTERNATIONAL INFO SCHOOL Page | 35


if(key[i][j]==0)
{
printf(“%d”,j);
min+=cost1[i][j];
}
else
{
output(i,key[i][j]);
output(key[i][j],j);
}
}

OSPF Output:
[prabha@localhost ~]$ gcc ospf.c 2 3
[prabha@localhost ~]$ ./a.out 2
Enter the number of nodes :4 2
2
2 SCHOOL
SAN INTERNATIONAL INFO Page | 36
4
2

OSPF
Enter the cost between nodes[1 2] :1
Enter the cost between nodes[1 3] :0
Enter the cost between nodes[1 4] :2
Enter the cost between nodes[2 1] :0
Enter the cost between nodes[2 3] :0
Enter the cost between nodes[2 4] :2
Enter the cost between nodes[3 1] :0
Enter the cost between nodes[3 2] :0
Enter the cost between nodes[3 4] :6
Enter the cost between nodes[4 1] :0
Enter the cost between nodes[4 2] :3
Enter the cost between nodes[4 3] :3

MENU:
1.Find shortest path
2.Exit

Enter your choice :1


Enter the source and destination node:1 1
The source and destination are same,try again

Enter your choice :1


Enter the source and destination node :4 1
No paths exists

Enter your choice :1


Enter the source and destination node :1 3
The path is 143
The cost is 5

Enter your choice :2


[prabha@localhost ~]$

Result:
Thus the c program to simulate the OSPF routing protocol has been
completed successfully.

Ex No: 08 Simulation of Routing protocols –


Date: Distance vector Routing

AIM:
SAN INTERNATIONAL INFO SCHOOL Page | 37
To write a c program to simulate the distance vector routing protocol.

ALGORITHM:
Step 1: Start the process.
Step 2: Enter the no of nodes.
Step 3: Enter the distance between each node
Step 4: Enter the node to display the routing table
Step 5: Routing table contains route value from source node to each node
Step 6: Calculate the distance for each adjacent node.
Step 7: Display the resulting distance vector routing table
Step 8: Stop the process.

Distance Vector Routing


Program

#include<stdio.h>
#include<string.h>

int main()
{
SAN INTERNATIONAL INFO SCHOOL Page | 38
int i,j,k,n,a[10][10],b[10][10],source,s,d;
char ch;
printf(“Enter the number of nodes: “);
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i==j)
a[i][j]=0;
else
{
printf(“\n Distance between the host %d  %d : “,I,j);
scanf(“%d”,&a[i][j]);
}
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf(“\t%d”,a[i][j]);
}
printf(“\n”);
}
do
{
printf(“\n Enter the node to display the routing table :”);
scanf(“%d”,&source);
for(j=1;j<=n;j++)
{
if(source!=j)
{
if(a[source][j] != 0)
printf(“\n Adjacent path from %d to %d is :%d”,source,j,a[source][j]);
else
printf(“\n There is no path from %d to %d”,source,j);
}
}
printf(“\n\n Do you want to continue [y/n] “);
scanf(“%s”,&ch);
}while(ch != ‘n’);

for(k=1;k<=n;k++)
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)

SAN INTERNATIONAL INFO SCHOOL Page | 39


if(a[i][j]>a[i][k]+a[k][j])
a[i][j] = a[i][k]+a[k][j];
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf(“%d\t”,a[i][j]);
}
printf(“\n”);
}
}

Distance Vector Routing Output


[prabha@localhost ~]$ gcc distance.c
[prabha@localhost ~]$ ./a.out

Enter the number of nodes : 4

SAN INTERNATIONAL INFO SCHOOL Page | 40


Distance between the host 1  2 : 2

Distance between the host 1  3 : 1

Distance between the host 1  4 : 0

Distance between the host 2  1 : 0

Distance between the host 2  3 : 1

Distance between the host 2  4 : 0

Distance between the host 3  1 : 0

Distance between the host 3  2 : 3

Distance between the host 3  4 : 2

Distance between the host 4  1 : 0

Distance between the host 4  2 : 2

Distance between the host 4  3 : 3

0 2 1 0
0 0 1 0
0 3 0 2
0 2 3 0

Enter the node to display the routing table : 4

There is no path from 4 to 1


Adjacent path from 4 to 2 is : 2
Adjacent path from 4 to 3 is : 3

Do you want to continue [y/n] : y

Enter the node to display the routing table : 3

There is no path from 3 to 1


Adjacent path from 3 to 2 is : 3
Adjacent path from 3 to 4 is : 2

Do you want to continue [y/n] : n

SAN INTERNATIONAL INFO SCHOOL Page | 41


0 2 1 0
0 0 1 0
0 2 0 0
0 2 1 0

[prabha@localhost ~]$

Result:
Thus the C program to simulate the distance vector routing protocol
has been completed successfully.

Ex No: 09 File Transfer Protocol – FTP


Date:

AIM:

SAN INTERNATIONAL INFO SCHOOL Page | 42


To write a C program to transfer a file from source to the destination
using FTP.

ALGORITHM:
FTP Server:
Step 1: Start the process.
Step 2: Create a TCP socket.
Step 3: Assign family, port and address to the socket.
Step 4: Bind the socket.
Step 5: Listen to the client and accept the connection from the client.
Step 6: Get the source file name.
Step 7: Get the destination file name and transfer the source to destination and
display the content.
Step 8: Close the socket.
Step 9: Stop the process.
FTP Client:
Step 1: Start the process.
Step 2: Create a TCP socket.
Step 3: Assign family, port and address to the client.
Step 4: Connect with the server.
Step 5: Receive the file name from the server.
Step 6: Display the content of the requested file.
Step 7: Close the socket.
Step 8: Stop the process.
FTP
Server Program

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<fcntl.h>

SAN INTERNATIONAL INFO SCHOOL Page | 43


#include<stdlib.h>
#include<unistd.h>
#define SERV_TCP_PORT 9000
#define MAX 80
int main(int argc,char *argv[])
{
FILE *fp;
int sockfd,newsockfd,clength;
struct sockaddr_in serv_addr,cli_addr;
char s[MAX],t[MAX],str[MAX];
strcpy(t,”exit”);
sockfd=socket(AF_INET,SOCK_STREAM,0);
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=INADDR_ANY;
serv_addr.sin_port=htons(SERV_TCP_PORT);
bind(sockfd,(struct sockaddr*)&serv_addr,sizeof(serv_addr));
listen(sockfd,5);
for(;;)
{
clength=sizeof(cli_addr);
newsockfd=accept(sockfd,(struct sockaddr*)
&cli_addr,&clength);
close(sockfd);
read(newsockfd,&str,MAX);
printf(“File Name Requested :%s\n”,str);
fp=fopen(str,”r”);
while(fgets(s,79,fp)!=NULL)
{
printf(“%s”,s);
write(newsockfd,s,MAX); }
write(newsockfd,s,MAX);
fclose(fp);
exit(0); }
printf(“\nThe Requested file is tranfered…\n”);
return 0;
}

Client Program
#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
#include<fcntl.h>
#include<stdlib.h>
#include<unistd.h>

SAN INTERNATIONAL INFO SCHOOL Page | 44


#define SERV_TCP_PORT 9000
#define MAX 80
int main(int argc,char *argv[])
{
FILE *fp;
int sockfd;
char s[MAX],name[MAX],sendline[MAX],recvline[MAX+1];
struct sockaddr_in serv_addr;
strcpy(s,”exit”);
sockfd=socket(AF_INET,SOCK_STREAM,0);
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=inet_addr(“127.0.0.1”);
serv_addr.sin_port=htons(SERV_TCP_PORT);

connect(sockfd,(struct sockaddr*)
&serv_addr,sizeof(serv_addr));
for(;;)
{
printf(“\nEnter the File Name: “);
scanf(“%s”,sendline);
printf(“\nEnter the Destination File Name: “);
scanf(“%s”,name);
fd=fopen(name,”w”);
write(sockfd,sendline,MAX);
while(1)
{
read(sockfd,recvline,MAX);
printf(“%s”,recvline);
fputs(recvline,fp);
exit(0);
}
fclose(fp);
}close(sockfd);
return 0;
}

Output
[prabha@localhost ~]$ cat sample.c
Welcome to Loyola Institute of Technology

[prabha@localhost ~]$

Server

SAN INTERNATIONAL INFO SCHOOL Page | 45


[prabha@localhost ~]$ gcc ftpc.c
[prabha@localhost ~]$ ./a.out

Enter the File Name : sample.c

Enter the Destination File Name : out.c


Welcome to Loyola Institute of Technology

[prabha@localhost ~]$

Client

[prabha@localhost ~]$ gcc ftps.c


[prabha@localhost ~]$ .\a.out

File Name Requested: sample.c


Welcome to Loyola Institute of Technology

[prabha@localhost ~]$

[prabha@localhost ~]$ cat out.c


Welcome to Loyola Institute of Technology
[prabha@localhost ~]$

Result:
Thus the C program to develop a FTP application has been completed
successfully.

Ex No: 10 Multi User Chat Application


Date:

AIM:
To write a C program to develop a Multi-user chat Application.

SAN INTERNATIONAL INFO SCHOOL Page | 46


ALGORITHM:
Server:
Step 1: Start the process.
Step 2: Create a TCP socket.
Step 3: Assign family, port and address to the socket.
Step 4: Bind the socket.
Step 5: Listen to the clients and accept the connections.
Step 6: Get the ID of the client which needs communication.
Step 7: Proceed the communication.
Step 8: Close the socket.
Step 9: Stop the process.

Client 1:
Step 1: start the process.
Step 2: Create a TCP socket.
Step 3: Assign family, port and address to the socket.
Step 4: Connect with the server.
Step 5: Proceed the communication.
Step 6: Close the socket.
Step 6: Stop the process.

Client 2:
Step 1: Start the process.
Step 2: Create a TCP socket.
Step 3: Assign family, port and address to the socket.
Step 4: Connect with the server.
Step 5: Proceed the communication.
Step 6: Close the socket

SAN INTERNATIONAL INFO SCHOOL Page | 47


Development of Application – Multi User Chat
Server Program

#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<string.h>
main()
{
int i,sd,sd2,nsd,client,sport,len;
char sendmsg[20],rcvmsg[20];
struct sockaddr_in servaddr,cliaddr;

printf(“\nEnter the port number :”);


scanf(“%d”,&sport);

sd=socket(AF_INET,SOCK_STREAM,0);

if(sd<0)
printf(“\nCant’t created\n”);
else
printf(“\nSocket is created\n”);

servadd.sin_family=AF_INET;
servadd.sin_addr.s_addr.s=hto|(INADDR_ANY);
servaddr.sin_port=htons(sport);
sd2=bind(sd,(struct sockaddr*)&seraddr,sizeof(seraddr));

if(sd2<0)
printf(“\nCan’t bind\n”);
else
printf(“\nBinded\n”);

listen(sd,5);

do
{
Printf(“\nEnter the client number to
communication :”);
scanf(“%d”,&i);
if(i==0)
exit(0);
printf(“\nClint %d is connected\n”,i);

SAN INTERNATIONAL INFO SCHOOL Page | 48


clilen=sizeof(cliaddr);
nsd=accept(sd,(struct sockaddr*)&cliaddr,&clilen);

if(nsd<0)
printf(\nCan’t accept\n”);

else
printf(“\nAccepted\n”);

printf(“\nStart to communication
\n\nServer\t\tClient”);
do
{
recv(nsd,rcvmsg,20,0);
printf(“\t\t%s\n\n”,rcvmsg);
fgets(sendmsg,20,stdin);
len=strlen(sendmsg);
sendmsg[len-1]=’\0’;
send(nsd,sendmsg,20,0);
wait(20);
}while(strcmp(sendmsg,”bye”)!=0);
}while(i!=0);
}

Client 1Program

#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
main()
{
int csd,cport,len;
char sendmsg[20],revmsg[20];
struct sockaddr_in servaddr;
printf(“\nEnter the port :”);
scanf(“%d”,&cport);
csd=socket(AF_INET,SOCK_STREAM,0);

if(csd<0)
printf(“\nCan’t Create\n”);
else
printf(“\nSocket is created\n”);

servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=hton|(INADDR_ANY);
servaddr.sin_port=htons(cport);

SAN INTERNATIONAL INFO SCHOOL Page | 49


if(connect(csd,(struct sockaddr*)
&servaddr,sizeof(servaddr))<0)
Printf(“\nCan’t Connect\n”);
else
printf(“\nConnect\n\nClient\t\tServer”);
do
{
fgets(sendmsg,20,stdin);
len=strlen(sendmsg);
sendmsg[len-1]=’\0’;
send(csd,sendmsg,20,0);
wait(20);
recv(csd,revmsg,20,0);
printf(“\t\t%s\n\n”,revmsg);
}while(strcmp(revmsg,”bye”)!=0);
}

Client 2 Program

#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
main()
{
int csd,cport,len;
char sendmsg[20],revmsg[20];
struct sockaddr_in servaddr;
printf(“\nEnter the port :”);
scanf(“%d”,&cport);
csd=socket(AF_INET,SOCK_STREAM,0);

if(csd<0)
printf(“\nCan’t Create\n”);
else
printf(“\nSocket is created\n”);

servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=hton|(INADDR_ANY);
servaddr.sin_port=htons(cport);

if(connect(csd,(struct sockaddr*)
&servaddr,sizeof(servaddr))<0)
Printf(“\nCan’t Connect\n”);

SAN INTERNATIONAL INFO SCHOOL Page | 50


else
printf(“\nConnect\n\nClient\t\tServer”);
do
{
fgets(sendmsg,20,stdin);
len=strlen(sendmsg);
sendmsg[len-1]=’\0’;
send(csd,sendmsg,20,0);
wait(20);
recv(csd,revmsg,20,0);
printf(“\t\t%s\n\n”,revmsg);
}while(strcmp(revmsg,”bye”)!=0);
}

Output

Server
[prabha@localhost ~]$ gcc mucs.c
[prabha@localhost ~]$ ./a.out

Enter the port number: 6444


Socket is created
Binded

SAN INTERNATIONAL INFO SCHOOL Page | 51


Enter the client number to communicate: 1
Client 1 is connected
Accepted

Start to communication

Server Client
Hi server
Hi client1
Bye
Why?
Urgent
Ok
Bye
Bye

Enter the client number to communicate: 2

Client 2 is connected

Accepted

Start to communication

Server Client
Hi server
Hi client2
How r u?
Fine
Where r u?
Bye
Enter the client number to communicate :0

[prabha@localhost ~]$

Client 1

[prabha@localhost ~]$ gcc mucc1.c


[prabha@localhost ~]$ ./a.out

Enter the port number: 6444

Socket is created

Connected

SAN INTERNATIONAL INFO SCHOOL Page | 52


Client Server
Hi server
Hi client1
Bye
Why?
Urgent
Ok
Bye
Bye

[prabha@localhost ~]$

Client 2

[prabha@localhost ~]$ gcc mucc2.c


[prabha@localhost ~]$ ./a.out

Enter the port number: 6444


Socket is created
Connected

Client Server
Hi server
Hi client2
How r u?
Fine
Where r u?
Bye
[prabha@localhost ~]$

Result:
Thus the C program to develop a Multi -user Chat Application has been
completed successfully.

SAN INTERNATIONAL INFO SCHOOL Page | 53

You might also like