CN Lab File
CN Lab File
#include<stdio.h>
#include<stdlib.h> #include<math.h>
int k,time,win=2,i2=0,frame=0,a[20],b[20],i,j,s,r,ack,c,d;
main()
{
int i1=0,j1=0,c1;
for(i=0;i<frame;i++)
{
a[i]=rand();
}
k=1;
while(i1<frame)
{
if((frame-i1)<win) j1=frame-i1;
break;
case 2:
printf("\n\n\t Go back n\t\n\n");
printf("\nRetransmit the frames from %d to %d\n",i1,i1+j1);
send(i1,i1+j1);
break;
}
}
i1=i1+win;
k++;
}
}
int send(c,d)
{
int t1;
for(i=c;i<d;i++)
{ b[i]=a[i]; printf("frame
sent\n",i);
}
s=checsum(&a[c]);
return(s);
}
int receive(c,d,c2) int c2;
{
r=checsum(&b[c]);
if(c2==r)
{
return(0);
}
else return(1);
}
int checsum(int *c)
{
int sum=0;
for(i=0;i<win;i++)
sum=sum^(*c);
return sum;
}
Output:
50
sent
sent
back N
#include<iostream.h>
#include<stdio.j>
#include<sys/socket.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<netdb.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<time.h> int
main()
listenfd=socket(AF_INET,SOCK_STREAM,0);
bzero(&serv,sizeof(serv));
serv.sin_family=AF_INET;
serv.sin_addr.s_addr=htonl(INADDR_ANY);
serv.sin_port=htons(port);
bind(listenfd,(struct sockaddr*)&serv,sizeof(serv));
listen(listenfd,15); while(1)
connfd=accept(listenfd,(struct sockaddr*)NULL,NULL);
cout<<"\nConx.Open!]n";
else if(child==0)
close(listenfd); strcpy(buff,"abc");
while(strcmp(buff,"bye")!=0)
buff[n]='\0';
cout<<"\nClient:"<<buff;
cout<<"Server:"; gets(buff);
write(connfd,buff,strlen(buff));
}
cout<<"\n\n Conx closed!";
close(connfd); exit(0);
close(connfd);
return 0;
Client Program:-
#include<stdio.h>
#include<sys/socket.h>
#include<iostream.h>
#include<netinet/in.h>
#include<stdlib.h>
#include<unistd.h>
#include<string.h>
#include<netbd.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<errno.h>
main(int argc.char*argv)
{
int sockfd,n; char buff[100];
if(argc!=3)
exit(0);
if((sockfd=socket(AF_INET,SOCK_STREAM,0))<0)
exit(0);
bzero(&serv,sizeof(serv)); serv.sin_family=AF_INET;
serv.sin_port=htons(atoi9argv[2]));
if(inet_pton(AF_INET,argv[1],&serv.sin_addr)<0)
if(connect(sockfd,(struct sockaddr*)&serv,sizeof(serv))<0)
(0);
}
printf("\n connected..\n"); strcpy(buff,"abc");
while(strcmp(buff,"bye")!=0)
write(sockfd,buff,strlen(buff));
cout<<"\n Server:";
n=read(sockfd,buff,100);
buff[n]='\0'; cout<<buff;
if(n<0)
exit (0);
cout<<"\n"; return 0;
#include<stdio.h>
#include<sys/types.h>
#include<sys/shm.h> #include<string.h>
main()
{ int shmid, a, i;
Char *ptr, *shmptr;
shmid=shmget(3000,10,IPC_CREAT | 0666);
shmptr=shmat(shmid,NULL,0);
ptr=shmptr;
for(i=0;i<3;i++)
{
puts("enter the mac");
scanf("%s",ptr); a=strlen(ptr);
printf("string length:%d",a);
ptr[a]= ' ' ; puts("enter ip");
ptr=ptr+a+1; scanf("%s",ptr);
ptr[a]='\n' ;
ptr= ptr+a+1;
} ptr[strlen(ptr)]= '\0';
printf("\n ARP table at serverside is=\n%s", shmptr);
shmdt(shmptr);
}
//ARP CLIENT
#include<stdio.h>
#include<string.h>
#include<sys/types.h>
#include<sys/shm.h> main()
{
int shmid,a;
char *ptr, *shmptr; char ptr2[51], ip[12],
mac[26]; shmid=shmget(3000,10,0666);
shmptr=shmat(shmid,NULL,0); puts("the arp
table is"); printf("%s",shmptr);
printf("\n1.ARP\n 2.RARP\n 3.EXIT\n");
scanf("%d",&a);
switch(a)
{
case 1: puts("enter ip address");
scanf("%s",ip); ptr=strstr(shmptr,
ip); ptr-=8;
sscanf(ptr,"%s%*s",ptr2);
printf("mac addr is %s",ptr2);
break; case 2: puts("enter mac
addr"); scanf("%s",mac);
ptr=strstr(shmptr, mac);
sscanf(ptr,"%*s%s",ptr2);
printf("%s",ptr2); break; case 3:
exit(1);
}
}
1.ARP
2.RARP 3.EXIT enter your
choice: 1 enter ip address:
1.2.3.4 mac addr is a.b.c.d
#include<sys/socket.h>
#include<netinet/in.h>
#include<arpa/inet.h> main()
{
char host[17],*ptr; long int
addr;
struct in_addr ad; /* netinet/in.h*/
puts("\n\nEnter the host ID"); gets(host);
SCNetworkReachabilityRef reachability =
SCNetworkReachabilityCreateWithName(NULL,
host_name);
SCNetworkReachabilityFlags flags;
success = SCNetworkReachabilityGetFlags(reachability, &flags); bool isAvailable = success &&
(flags & kSCNetworkFlagsReachable) && !(flags &
kSCNetworkFlagsConnectionRequired); if (isAvailable) {
NSLog(@"Host is reachable: %d", flags);
}else{
NSLog(@"Host is unreachable");
}
5. Create a socket for HTTP for web page upload and download.
#include<stdio.h>
#include<stdlib.h>
#include<netdb.h>
#include<netinet/in.h>
#include<arpa/inet.h>
#include<sys/types.h>
#include<sys/socket.h> #define size
100
int main(int argc, char *argv[])
{
struct sockaddr_in sock; struct
hostent *hp,port; char
*req,*hostname,*cp; FILE
*local; char buff[size]; int
con,i,l,nrv,sd;
if(argc!=3)
{
printf(“\nUsage: %s<server>filename”,argv[0]); exit(1);
}
if(cp=strchr(argv[1],’/’))
{ *cp=’\0’; l=strlen(argv[1]);
hostname=malloc(l+1);
strcpy(hostname,argv[1]);
*cp=’/’; l=strlen(cp);
req=malloc(l+1);
strcpy(req,cp);
}
else
{
hostname=argv[1]; req=”/”;
}
printf(“\nHost=%s\nReq= %s”,hostname,req); sd=socket(AF_INET,SOCK_STREAM,0);
if(sd<0)
{
perror(“\nCannot open socket”); exit(1);
}
bzero(&sock,sizeof(sock)); sock.sin_family=AF_INET;
con=inet_pton(AF_INET, argv[1], &sock);
sock.sin_port=htons(80); con=connect(sd,(struct sockaddr
*)&sock, sizeof(sock)); if(con<0)
{
perror(“\nConnection failed”);
exit(1);
}
sprintf(buff,”Get HTTP:%s//1.1\r\nHost: %s\r\nConnection: class\r\n\r”, req, hostname);
printf(“Buff=%s\n”, buff); l=strlen(buff); local=fopen(argv[2],”w”); write(sd,buff,1);
do
{
nrv=read(sd,buff,size);
if(nrv>0)
{
for(i=0;i<nrv;i++)
putc(buff[i],local);
}
else break;
}
while(1); close(sd);
fclose(local);
return 0;
}
6. Write a program to implement RPC (Remote Procedure Call)
REMOTE PROCEDURE CALL
Step 3: In server side. register list of performance object in the RMI registry
Step 4: By starting the RMI registry we can run our server program to keep the program run
always.
Step 5: If the client want to involve to the procedure by means that by biding the registered
object in the RMI registry if(client) call the procedure and produce the result..
extends Remote
great
{
public static void main(String args[]) throws IOException
n=0,n1=0;
try
greater f=(greater)Naming.lookup("greaterser");
try
System.out.println("\n\n\t ******************************");
n1=Integer.parseInt(br.readLine());
catch(IOException e)
result=f.getresult(n,n1); if(result.equals("Equal"))
else
catch(Exception e)
}}}
implements greater
if(first<second)
return(Integer.toString(second)); else
if(second<first) return(Integer.toString(first));
else return("EQUAL");
{ try
{
greaterser f=new greaterser(); Naming.rebind("greaterser",f);
catch(Exception e)
}}}
OUTPUT:
RESULT:
7. Implementation of Subnetting .
\\ and these bits to get the Network Address. Note, the prefix could be used \\
instead of subnet mask - they represent the same thing.
AIM:
ALGORITHM:
1. Include necessary header files to support functions for Socket definitions,Socket types,Internet
addresses,I/O functions,UNIX system calls.
2. Declare variables for Socket ID,Port number,Socket addresses,Character buffer etc.
3. Create socket for server and bind socket with addresses.
4. Specify number of allowed connections.
5. Wait for connection.
6. Accept connection if any.
7. Retrieve information from Connected Socket.
8. Echo retrieved information to Connected Socket.
9. Close Connected Socket.
1. Include necessary header files to support functions for Socket definitions,Socket types,Internet
addresses,I/O functions,UNIX system calls.
2. Declare variables for Socket ID,Port number,Socket addresses,Character buffer etc.
3. Create socket for client.
4. Connect client socket to the server socket addresses.
5. Give message to be echoed.
6. Retrieve echoed information from server and display it.
SERVER PROGRAM:
java.net.*; import
java.lang.String.*; public
class Secho
Socket s=ss.accept();
String str;
System.out.println("\nSERVER SIDE!...");
while(true)
str=in.readLine();
out.writeBytes(str+"\n");
System.out.println(str+"\n");
CLIENT PROGRAM:
java.net.*; import
java.lang.String.*; public
class Cecho
{
DataInputStream in=new DataInputStream (System.in);
String str;
while((str=in.readLine())!=null)
out.writeBytes(str+"\n");
if(str.equals("exit"))
out.writeBytes("\nClient Terminated");
break;
else
System.out.print(str+"\n");
}}
OUTPUT: SERVER:
CLIENT:
RESULT:
b. Chat
CHAT APPLICATION
AIM:
ALGORITHM:
SERVER PROGRAM:
chatserver
DataInputStream din=null;
DataOutputStream dout=null;
Socket c=null;
ServerSocket m=null;
try
DataInputStream(c.getInputStream()); dout=new
DataOutputStream(c.getOutputStream());
}
catch(Exception e)
while(c!=null)
while(true)
String m1=din.readLine();
m2=stdin.readLine(); dout.writeBytes(""+m2);
dout.writeBytes("\n");
din.close(); dout.close();
c.close();
m.close();
CLIENT PROGRAM:
import java.io.*; import
chatclient
Socket c=null;
DataInputStream uin=null;
dout=null;
try
DataInputStream(System.in); din=new
DataInputStream(c.getInputStream()); dout=new
DataOutputStream(c.getOutputStream()); }
catch(Exception e)
} if(c!=null)
String inp;
while((inp=uin.readLine())!=null)
{
dout.writeBytes(""+inp); dout.writeBytes("\n");
System.out.println("Enter ur message:");
}}
din.close(); dout.close();
c.close();
}}
OUTPUT: SERVER:
CLIENT:
RESULT:
c. File Transfer
int main(void)
{
int sockfd = 0;
int bytesReceived = 0;
char recvBuff[256];
memset(recvBuff, '0', sizeof(recvBuff));
struct sockaddr_in serv_addr;
/* Create a socket first */
if((sockfd = socket(AF_INET, SOCK_STREAM, 0))< 0)
{
printf("\n Error : Could not create socket \n");
return 1;
}
/* Attempt a connection */
if(connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr))<0)
{
printf("\n Error : Connect Failed \n");
return 1;
}
return 0; }
Server:
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h> #include <errno.h>
#include <string.h>
#include <sys/types.h>
int main(void)
{
int listenfd = 0;
int connfd = 0;
struct sockaddr_in serv_addr;
char sendBuff[1025];
int numrv;
while(1) {
connfd = accept(listenfd, (struct sockaddr*)NULL ,NULL);
AIM:
ALGORITHM:
1. Include necessary header files to support functions for Socket definitions,Socket types,Internet
addresses,I/O functions,UNIX system calls.
2. Declare variables for Socket ID,Port number,Socket addresses,Character buffer etc.
3. Create socket for server .
4. Bind socket with addresses.
5. Specify number of allowed connections.
6. Retrieve information from Connected Socket.
7. Echo retrieved information to Connected Socket.
8. Close Connected Socket.
1. Include necessary header files to support functions for Socket definitions,Socket types,Internet
addresses,I/O functions,UNIX system calls.
2. Declare variables for Socket ID,Port number,Socket addresses,Character buffer etc.
3. Create socket for client.
4. Connect client socket to the server socket addresses.
5. Give message to be echoed.
6. Retrieve echoed information from server and display it.
7. Close the Socket.
SERVER PROGRAM
import java.net.*;
import java.io.*;
class p1sender
InetAddress.getByName(toName), 666));
} } }
CLIENT PROGRAM
import java.net.*;
throws Exception {
}}
OUTPUT:
SERVER
CLIENT
RESULT:
e. SNMP
f. File Transfer
10. Perform a case study about the different routing algorithms to
select the network path with its optimum and economical during
data transfer. i. Link State routing ii. Flooding iii. Distance vector