Program

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Experiment 3(tcp)

Server.c

#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#define PORT 8080
int main(int argc, char const* argv[])
{
int server_fd, new_socket;
ssize_t valread;
struct sockaddr_in address;
int opt = 1;
socklen_t addrlen = sizeof(address);
char buffer[1024] = {0};
char* hello = "hello from server";
server_fd = socket(AF_INET, SOCK_STREAM, 0);
setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
bind(server_fd, (struct sockaddr*)&address, sizeof(address));
listen(server_fd, 3);
new_socket = accept(server_fd, (struct sockaddr*)&address, &addrlen);
valread = read(new_socket, buffer, 1024 - 1);
printf("%s", buffer);
send(new_socket, hello, strlen(hello), 0);
printf("Hello message sent\n");
close(new_socket);
close(server_fd);
return 0;
}
Experiment 3(tcp)
Client.c

#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#define PORT 8080
int main(int argc, char const* argv[])
{
int status, valread, client_fd;
struct sockaddr_in serv_addr;
char* hello = "hello from calendar";
char buffer[1024] = {0};
client_fd = socket(AF_INET, SOCK_STREAM, 0);
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
inet_pton(AF_INET, "127.0.0.1", &serv_addr.sin_addr);
status = connect(client_fd, (struct sockaddr*)&serv_addr, sizeof(serv_addr));
if (status < 0) {
printf("Connection Failed\n");
return -1;
}
send(client_fd, hello, strlen(hello), 0);
printf("Hello message sent\n");
valread = read(client_fd, buffer, 1024);
printf("%s\n", buffer);
close(client_fd);
return 0;
}
Experiment 4(udp)
Server.c

#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/types.h>
#define PORT 8080
#define MAXLEN 1000
int main()
{
char buffer[100];
char *message="hello from server";
int listenfd,len;
struct sockaddr_in servaddr,cliaddr;
bzero(&servaddr, sizeof(servaddr));
listenfd=socket(AF_INET,SOCK_DGRAM,0);
servaddr.sin_addr.s_addr=htons(INADDR_ANY);
servaddr.sin_port=htons(PORT);
servaddr.sin_family=AF_INET;
bind(listenfd,(struct sockaddr*)&servaddr,sizeof(servaddr));
len=sizeof(cliaddr);
int n=recvfrom(listenfd,buffer,sizeof(buffer),0,(struct sockaddr*)&cliaddr,&len);
buffer[n]='\0';
puts(buffer);
sendto(listenfd,message,MAXLEN,0,(struct sockaddr*)&cliaddr,sizeof(cliaddr));

}
Experiment 4(udp)
Client.c

#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include <sys/types.h>
#include <arpa/inet.h>
#define PORT 8080
#define MAXLEN 1000
int main()
{
char buffer[100];
char *message="hello from client";
int sockfd,n;
struct sockaddr_in servaddr;
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_addr.s_addr=inet_addr("127.0.0.1");
servaddr.sin_port=htons(PORT);
servaddr.sin_family=AF_INET;
sockfd=socket(AF_INET,SOCK_DGRAM,0);
connect(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr));
sendto(sockfd,message,MAXLEN,0,(struct sockaddr*)NULL,sizeof(servaddr));
recvfrom(sockfd,buffer,sizeof(buffer),0,(struct sockaddr*)NULL,NULL);
close(sockfd);
}
Experiment 5(multichat)
Server.c

#include <stdio.h>
#include <sys/socket.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
int main()
{
int sock=0, client_conn=0,counter=0,pid;
char data_send[1024],data_received[1024];
struct sockaddr_in ServerIp;
sock = socket(AF_INET, SOCK_STREAM, 0);
memset(&ServerIp,'0',sizeof(ServerIp) );
ServerIp.sin_family = AF_INET;
ServerIp.sin_port = htons(1234);
ServerIp.sin_addr . s_addr = inet_addr("127.0.0.1");
if( bind( sock,(struct sockaddr* )&ServerIp, sizeof(ServerIp)) == -1 )
printf("\n Socket binding failed ");
if( listen(sock,20) == -1)
printf("Error\n");
else
printf("\n Server started\n");
for(;;){
label:
client_conn = accept( sock, (struct sockaddr*)NULL, NULL);
pid = fork();
if( pid < 0 )
printf("\n Process creation failed ");
else if( pid > 0 ){
counter++;
goto label;
}
else{
counter++;
if( recv(client_conn, data_received, 1024, 0 ) == -1 )
printf(" Error !! cannot get response \n");
printf(" data from client is %s\n",data_received);
sprintf(data_send," Hi client %d !! from server ",counter);
write( client_conn, data_send, sizeof(data_send) );
}
}
close( sock );
return 0;
}
Experiment 5(multichat)
Client.c

#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
int main()
{
char data_received[1024],data_send[1024];
int sock=0;
struct sockaddr_in ServerIp;
if( (sock = socket(AF_INET, SOCK_STREAM ,0 )) == -1 )
printf(" socket creation failed ");
ServerIp.sin_family = AF_INET;
ServerIp.sin_port = htons(1234);
ServerIp.sin_addr . s_addr = inet_addr("127.0.0.1");
if( (connect( sock, (struct sockaddr *)&ServerIp, sizeof(ServerIp) )) == -1 )
{
printf("\n connection to the socket failed ");
exit(0);
}
else
printf("\n connected to socket \n");
strcpy(data_send,"hello server");
if( send(sock,data_send,sizeof(data_send),0) == -1 )
printf("sending failed ");
while(1)
{
printf("\n waiting for respose !! \n");
if( recv(sock, data_received, 1024, 0 ) == -1 )
printf(" Error !! cannot get response |n");
else
printf("\n Response Received is : %s", data_received );
}
}
Experiment 7(stop and wait)

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

void sender()
{
int datapacket[]={1,2,3,4,5};
int n=sizeof(datapacket)/sizeof(datapacket[0]);
for (int i=0;i<n;i++)
{
printf("sender: sending data packet%d\n",datapacket[i]);
printf("sender: receive ack for data packet%d\n",datapacket[i]);
}
}
void receive()
{
int receivedpacket[]={1,2,4,5};
int n=sizeof(receivedpacket)/sizeof(receivedpacket);
int expectedseqnum=1;
for (int i=0;i<n;i++)
{
printf("receiver: received data packet%d\n",receivedpacket[i]);
if (receivedpacket[i]==expectedseqnum)
{
printf("receiver: sent ack for data packet%d\n",receivedpacket[i]);
expectedseqnum++;
}
else
{
printf("receiver: out of order packet received,requisting retransmission\n");
printf("retransmitting\n");
}
}
}
int main()
{
sender();
receive();
return 0;
}
Experiment 8(go back n)

#include <stdio.h>
#include <stdlib.h>
int main()
{
int windowsize,sent=0,ack,i;
printf("enter window size");
scanf("%d",&windowsize);
while(1)
{
for(i=0;i<windowsize;i++)
{
printf("frame %d has been transmitted\n",sent+1);
sent++;
if(sent==windowsize)
break;
}
printf("\n please enter the last ack received\n");
scanf("%d",&ack);
if (ack==windowsize)
break;
else
sent=ack;
}
return 0;
}
Experiment 9(selective repeate)

#include <stdio.h>
#include <stdlib.h>
#define MAX_FRAMES 100
void SelectiveRepeat(int windowsize,int totalframes)
{
int sendwindow=windowsize;
int receivewindow=windowsize;
int frames[MAX_FRAMES];
int ack[MAX_FRAMES]={0};
for (int i=0;i<totalframes;i++)
{
frames[i]=i;
}
for(int i=0;i<totalframes;)
{
for(int j=i;j<i+sendwindow&&j<totalframes;++j)
{
if(!ack[j])
{
printf("sending frame %d\n",frames[j]+1);
}
}
for(int j=i;j<i+receivewindow&&j<totalframes;++j)
{
if(!ack[j])
{
printf("received ack for frame %d\n",frames[j]+1);
ack[j]=1;
}
}
i+=sendwindow;
}
}
int main()
{
int windowsize,totalframes;
printf("enter window size");
scanf("%d",&windowsize);
printf("enter total number of frames to transmit");
scanf("%d",&totalframes);
SelectiveRepeat(windowsize,totalframes);
return 0;
}
Experiment 10(distance vector)

#include <stdio.h>
#include <stdlib.h>
struct node
{
unsigned dist[20];
unsigned from[20];
}rt[10];
int main()
{
int dmat[20][20];
int n,i,j,k,count=0;
printf("\n enter the number of nodes:");
scanf("%d",&n);
printf("\n enter the cost matrix:\n");
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
scanf("%d",&dmat[i][j]);
dmat[i][i]=0;
rt[i].dist[j]=dmat[i][j];
rt[i].from[j]=j;
}
do
{
count=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
for(k=0;k<n;k++)
if(rt[i].dist[j]>dmat[i][j]+rt[k].dist[j])
{
rt[i].dist[j]=rt[i].dist[k]+rt[k].dist[j];
rt[i].from[j]=k;
count++;
}
}while(count!=0);
for(i=0;i<n;i++)
{
printf("\n\nstate value for router %d is\n",i+1);
for(j=0;j<n;j++)
{
printf("\t\nnode %d via %d distance %d",j+1,rt[i].from[j]+1,rt[i].dist[j]);
}}
printf("\n\n");
}
Experiment 11(link state)

#include<stdio.h>
#include<limits.h>

#define MAX_NODES 10

void dijkstra(int graph[MAX_NODES][MAX_NODES], int numNodes, int source) {


int dist[MAX_NODES];
int visited[MAX_NODES] = {0};

for(int i = 0; i < numNodes; i++) {


dist[i] = INT_MAX;
}

dist[source] = 0;

for(int i = 0; i < numNodes - 1; ++i) {


int u = -1;

for(int v = 0; v < numNodes; ++v) {


if(!visited[v] && (u == -1 || dist[v] < dist[u])) {
u = v;
}
}

visited[u] = 1;

for(int v = 0; v < numNodes; ++v) {


if(!visited[v] && graph[u][v] != INT_MAX) {
int newDist = dist[u] + graph[u][v];
if(newDist < dist[v]) {
dist[v] = newDist;
}
}
}
}

printf("Shortest path from node %d:\n", source);


for(int i = 0; i < numNodes; ++i) {
printf("Node %d: Distance = %d\n", i, dist[i]);
}
}

int main() {
int numNodes;
printf("Enter the number of routers: ");
scanf("%d", &numNodes);

int graph[MAX_NODES][MAX_NODES];

printf("Enter the cost matrix:\n");


for(int i = 0; i < numNodes; i++) {
for(int j = 0; j < numNodes; ++j) {
scanf("%d", &graph[i][j]);
}
}

int source;
printf("Enter the source router: ");
scanf("%d", &source);

dijkstra(graph, numNodes, source);

return 0;
}

Experiment 13(leaky bucket)

#include<stdio.h>

void main() {
int in, out, bsize, n, bucket = 0;

printf("Enter the bucket size : ");


scanf("%d", &bsize);

printf("Enter the no of inputs: ");


scanf("%d", &n);

printf("Enter the packet outgoing rate : ");


scanf("%d", &out);

while (n != 0) {
printf("\nEnter the incoming packet size : ");
scanf("%d", &in);

printf("Incoming packet size: %d\n", in);

if (in <= (bsize - bucket)) {


bucket += in;
printf("Bucket status : %d out of %d\n", bucket, bsize);
} else {
printf("Dropped packets : %d \n", in - (bsize - bucket));
printf("Bucket status : %d out of %d\n", bucket, bsize);
bucket = bsize;
}

bucket -= out;
printf("After outgoing, bucket status: %d packets out of %d \n", bucket, bsize);
n--;
}
}

You might also like