0% found this document useful (0 votes)
118 views23 pages

Lab Manual

The document describes a simulation of the sliding window protocol for reliable data transfer over TCP. It includes code for a server and client implementation that transmit frames of data with sequence numbers and implement retransmission of frames if errors are detected. The client can request retransmission of specific frames by sequence number. The output shows an example run where the server transmits a string over multiple frames, and the client simulates errors requiring retransmission of some frames.

Uploaded by

Geethu Mohan
Copyright
© Attribution Non-Commercial (BY-NC)
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)
118 views23 pages

Lab Manual

The document describes a simulation of the sliding window protocol for reliable data transfer over TCP. It includes code for a server and client implementation that transmit frames of data with sequence numbers and implement retransmission of frames if errors are detected. The client can request retransmission of specific frames by sequence number. The output shows an example run where the server transmits a string over multiple frames, and the client simulates errors requiring retransmission of some frames.

Uploaded by

Geethu Mohan
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 23

1.

TCP File server


PROGRAM: CLIENT:
import java.io.*; import java.net.*; import java.util.*; public class ft2client { public static void main(String srgs[])throws IOException { Socket s=null; BufferedReader get=null; PrintWriter put=null; try { s=new Socket("127.0.0.1",8081); get=new BufferedReader(new InputStreamReader(s.getInputStream())); put=new PrintWriter(s.getOutputStream(),true); } catch(Exception e) { System.exit(0); } String u,f; System.out.println("Enter the file name to transfer from server:"); DataInputStream dis=new DataInputStream(System.in); f=dis.readLine(); put.println(f); File f1=new File(f); FileOutputStream fs=new FileOutputStream(f1); while((u=get.readLine())!=null) { byte jj[]=u.getBytes(); fs.write(jj); } fs.close(); System.out.println("File received"); s.close(); } }

SERVER: import java.io.*; import java.net.*; import java.util.*; public class ft2server { public static void main(String args[])throws IOException { ServerSocket ss=null; try { ss=new ServerSocket(8081); } catch(IOException e) { System.out.println("couldn't listen"); System.exit(0); } Socket cs=null; try { cs=ss.accept(); System.out.println("Connection established"+cs); } catch(Exception e) { System.out.println("Accept failed"); System.exit(1); } PrintWriter put=new PrintWriter(cs.getOutputStream(),true); BufferedReader st=new BufferedReader(new InputStreamReader(cs.getInputStream())); String s=st.readLine(); System.out.println("The requested file is : "+s); File f=new File(s); if(f.exists()) { BufferedReader d=new BufferedReader(new FileReader(s)); String line; while((line=d.readLine())!=null) { put.write(line); put.flush(); } d.close();

System.out.println("File transfered"); cs.close(); ss.close(); } } }

OUTPUT: C:\jdk1.3\bin>javac ft2client.java Note: ft2client.java uses or overrides a deprecated API. Note: Recompile with -deprecation for details. C:\jdk1.3\bin>javac ft2server.java C:\jdk1.3\bin>java ft2server 2nd C:\jdk1.3\bin>java ft2client Enter the file name to transfer from server : u1.txt File received 1st C:\jdk1.3\bin>java ft2server Connection established Socket[addr=localhost/127.0.0.1,port=3022,localport=8081] The requested file is : u1.txt File transfered

2.UDP Socket to transfer File

File Transfer Client:


#include<sys/socket.h> #include<arpa/inet.h> #include<stdio.h> #include<unistd.h> #include<sys/types.h> #include<string.h> main() { int sfd,cfd; char buf[1024]=""; struct sockaddr_in server,client; sfd=socket(AF_INET,SOCK_DGRAM,0); bzero(&server,sizeof(server)); server.sin_family=AF_INET; server.sin_port=htons(1011); inet_aton("172.16.29.67",&server.sin_addr); int b=connect(sfd,(struct sockaddr *)&server,sizeof(server)); printf("CONNECT VALUE:%d\n",b); int l=sizeof(client); for(;;) { printf("ENTER THE FILE NAME\n"); scanf("%s",buf); sendto(sfd,buf,strlen(buf),0,(struct sockaddr *)&server,sizeof(server)); recvfrom(sfd,buf,1024,0,NULL,NULL); printf("RECEIVED FROM SERVER:%s\n",buf); } close(sfd); }

File Transfer Server:


#include<sys/socket.h> #include<arpa/inet.h> #include<stdio.h> #include<unistd.h> #include<fcntl.h> #include<sys/types.h> #include<string.h> main() { int sfd,cfd; char buf[1024]=""; struct sockaddr_in server,client; sfd=socket(AF_INET,SOCK_DGRAM,0); bzero(&server,sizeof(server)); server.sin_family=AF_INET; server.sin_port=htons(1011); inet_aton("172.16.29.67",&server.sin_addr); int b=bind(sfd,(struct sockaddr *)&server,sizeof(server)); printf("BIND VALUE:%d\n",b); int l=sizeof(client); for(;;) { recvfrom(sfd,buf,1024,0,(struct sockaddr *)&client,&l); int fd=open(buf,O_RDONLY); read(fd,buf,1024); sendto(sfd,buf,strlen(buf),0,(struct sockaddr *)&client,sizeof(client)); printf("MESSAGE FROM CLIENT:%s\n",buf); } close(sfd); }

OUTPUT:
SERVER:
$cc udps.c -o udps $./udps BIND VALUE:0 accept value 4 MESSAGE FROM CLIENT: #include<stdio.h> main() { printf("hello"); }

CLIENT:
#cc udps.c -o udps #./udps ENTER THE MESSAGE hello.c RECEIVED FROM SERVER #include<stdio.h> main() { printf("hello"); }

3.TCP Socket 4.UDP Socket

5.TCP Chat server

6.UDP Chat server


SERVER #include<stdio.h> #include<netinet/in.h> #include<sys/types.h> #include<sys/socket.h> #include<netdb.h> #include<string.h> #include<stdlib.h> #define MAX 80 #define PORT 43454 #define SA struct sockaddr void func(int sockfd) { char buff[MAX]; int n,clen; struct sockaddr_in cli; clen=sizeof(cli); for(;;) { bzero(buff,MAX); recvfrom(sockfd,buff,sizeof(buff),0,(SA *)&cli,&clen); printf("From client %s To client",buff); bzero(buff,MAX); n=0; while((buff[n++]=getchar())!='\n'); sendto(sockfd,buff,sizeof(buff),0,(SA *)&cli,clen); if(strncmp("exit",buff,4)==0) { printf("Server Exit...\n"); break;

} } } int main() { int sockfd; struct sockaddr_in servaddr; sockfd=socket(AF_INET,SOCK_DGRAM,0); if(sockfd==-1) { printf("socket creation failed...\n"); exit(0); } else printf("Socket successfully created..\n"); bzero(&servaddr,sizeof(servaddr)); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=htonl(INADDR_ANY); servaddr.sin_port=htons(PORT); if((bind(sockfd,(SA *)&servaddr,sizeof(servaddr)))!=0) { printf("socket bind failed...\n"); exit(0); } else printf("Socket successfully binded..\n"); func(sockfd); close(sockfd); } CLIENT #include<sys/socket.h> #include<netdb.h> #include<string.h> #include<stdlib.h> #include<stdio.h> #define MAX 80 #define PORT 43454

#define SA struct sockaddr int main() { char buff[MAX]; int sockfd,len,n; struct sockaddr_in servaddr; sockfd=socket(AF_INET,SOCK_DGRAM,0); if(sockfd==-1) { printf("socket creation failed...\n"); exit(0); } else printf("Socket successfully created..\n"); bzero(&servaddr,sizeof(len)); servaddr.sin_family=AF_INET; servaddr.sin_addr.s_addr=inet_addr("127.0.0.1"); servaddr.sin_port=htons(PORT); len=sizeof(servaddr); for(;;) { printf("\nEnter string : "); n=0; while((buff[n++]=getchar())!='\n'); sendto(sockfd,buff,sizeof(buff),0,(SA *)&servaddr,len); bzero(buff,sizeof(buff)); recvfrom(sockfd,buff,sizeof(buff),0,(SA *)&servaddr,&len); printf("From Server : %s\n",buff); if(strncmp("exit",buff,4)==0) { printf("Client Exit...\n"); break; } } close(sockfd); }

OUTPUT SERVER SIDE $ cc udpchatserver.c $ ./a.out Socket successfully created.. Socket successfully binded.. From client hai To client hello From client exit To client exit Server Exit... $ CLIENT SIDE $ cc udpchatclient.c $ ./a.out Socket successfully created.. Enter string : hai From Server : hello Enter string : exit From Server : exit Client Exit... $

7.Simulation of sliding window protocol with frame sequence


/* Simulation of Sliding Window Protocol */
// SlideServer.c : A Simple Slide Server Program Using Sliding Window Protocol #include<stdio.h> #include<sys/socket.h> #include<sys/types.h> #include<netinet/in.h> #include<string.h> #include<stdlib.h>

#include<arpa/inet.h> #define SIZE 4 int main() { int sfd,lfd,len,i,j,status; char str[20],frame[20],temp[20],ack[20]; struct sockaddr_in saddr,caddr; sfd=socket(AF_INET,SOCK_STREAM,0); if(sfd<0) perror("Error"); bzero(&saddr,sizeof(saddr)); saddr.sin_family=AF_INET; saddr.sin_addr.s_addr=htonl(INADDR_ANY); saddr.sin_port=htons(5465); if(bind(sfd,(struct sockaddr*)&saddr,sizeof(saddr))<0) perror("Bind Error"); listen(sfd,5); len=sizeof(&caddr); lfd=accept(sfd,(struct sockaddr*)&caddr,&len); printf(" Enter the text : \n"); scanf("%s",str); i=0; while(i<strlen(str)) { memset(frame,0,20); strncpy(frame,str+i,SIZE); printf(" Transmitting Frames. "); len=strlen(frame); for(j=0;j<len;j++) { printf("%d",i+j); sprintf(temp,"%d",i+j); strcat(frame,temp); } printf("\n"); write(lfd,frame,sizeof(frame)); read(lfd,ack,20); sscanf(ack,"%d",&status); if(status==-1) printf(" Transmission is successful. \n"); else { printf(" Received error in %d \n\n",status);

printf("\n\n Retransmitting Frame. "); for(j=0;;) { frame[j]=str[j+status]; printf("%d",j+status); j++; if((j+status)%4==0) break; } printf("\n"); frame[j]='\0'; len=strlen(frame); for(j=0;j<len;j++) { sprintf(temp,"%d",j+status); strcat(frame,temp); } write(lfd,frame,sizeof(frame)); } i+=SIZE; } write(lfd,"exit",sizeof("exit")); printf("Exiting\n"); sleep(2); close(lfd); close(sfd); }

// SlideClient.c : A Simple Slide Client Program Using Sliding Window Protocol #include<stdio.h> #include<string.h> #include<stdlib.h> #include<sys/socket.h> #include<sys/types.h> #include<netinet/in.h> int main() { int sfd,lfd,len,choice; char str[20],str1[20],err[20]; struct sockaddr_in saddr,caddr; sfd=socket(AF_INET,SOCK_STREAM,0); if(sfd<0) perror("FdError"); bzero(&saddr,sizeof(saddr)); saddr.sin_family=AF_INET; saddr.sin_addr.s_addr=INADDR_ANY; saddr.sin_port=htons(5465); connect(sfd,(struct sockaddr*)&saddr,sizeof(saddr)); for(;;) { read(sfd,str,20); if(!strcmp(str,"exit")) { printf("Exiting\n"); break; } printf("\n\nReceived%s\n\n1.Do u want to report an error(1-Yes 0No)",str); scanf("%d",&choice); if(!choice) write(sfd,"-1",sizeof("-1")); else { printf("Enter the sequence no of the frame where error has occured\n"); scanf("%s",err); write(sfd,err,sizeof(err)); read(sfd,str,20); printf("\n\nReceived the re-transmitted frames%s\n\n",str); }

} }

OUT PUT :
SlideServer.c : cc SlideServer.c ./a.out Enter the text jerusalem Transmitting Frames=0123 Transmission is successful Transmitting Frames=4567 Received error in 5 Retransmitting Frame=678 Transmitting Frames=8 Received error in 2 Retransmitting Frame=34 Exiting ClientServer.c : cc ClientServer.c ./a.out Received=jeru0123 1.Do u want to report an error(1-Yes 0-No)0

Received=sale4567 1.Do u want to report an error(1-Yes 0-No)1 Enter the sequence no of the frame where error has occured 5 Received the re-transmitted frames=ale567 Received=m8 1.Do u want to report an error(1-Yes 0-No)1 Enter the sequence no of the frame where error has occured

2 Received the re-transmitted frames=ru23 Exiting

10.RPC USING JAVA

Filename: rint.java import java.io.*; import java.rmi.*; public interface rint extends Remote { public int add(int a,int b)throws RemoteException; publicintsub(intx,inty)throwsRemoteException; public int mul(int p,int q)throws RemoteException; } File Name: rclass.java importjava.io.*; import java.rmi.*; import java.net.*; import java.rmi.server.*; public class rclass extends UnicastRemoteObject implements rint { publicrclass()throwsRemoteException{} publicintadd(inta,intb)throwsRemoteException { intc=a+b; returnc; } publicintsub(intx,inty)throwsRemoteException { intd=x-y; returnd; } publicintmul(intp,intq)throwsRemoteException {

inte=p*q; returne; } } File Name: sclass.java Importjava.io.*; import java.rmi.*; import java.net.*; public class sclass { public static void main(String args[ ]) { try { rclass r=new rclass(); Naming.rebind("sum",r); } catch(Exceptione){} } } Filename: cclass.java import java.io.*; import java.rmi.*; public class cclass { public static void main(String args[]) { try { int b,b1; Strings; s="rmi://localhost/sum"; rintx=(rint)Naming.lookup(s); b=Integer.parseInt(args[0]); b1=Integer.parseInt(args[1]);

intz=x.add(b,b1); intz1=x.sub(b,b1); int z2=x.mul(b,b1); System.out.println("Sum"+z); System.out.println("Difference"+z1); System.out.println("Product"+z2); } catch(Exceptione){ } } }

OUTPUT Sum 4 Difference 0 Production 4

11.TCP Chat application 12.UDP Chat application(connect using command line arguments) 13.Echo server(TCP) 14.Echo server(UDP)

ECHO SERVER USING UDP


SERVER:

#include<netinet/in.h> #include <sys/types.h>

#include <sys/socket.h> #include<stdio.h> #include <arpa/inet.h> #include <string.h> #include<fcntl.h> main() { int sfd,l; char buf[1024]="",buf1[1024]=""; struct sockaddr_in ser; sfd=socket(AF_INET,SOCK_DGRAM,0); bzero(&ser,sizeof(ser)); ser.sin_family=AF_INET; ser.sin_port=htons(1300); inet_aton("localhost",&ser.sin_addr); printf("Enter the message:"); scanf("%s",buf); sendto(sfd,buf,strlen(buf),0,(struct sockaddr *)&ser,sizeof(ser)); recvfrom(sfd,buf1,1024,0,NULL,NULL); close(sfd); }

CLIENT:
#include<netinet/in.h>

#include <sys/types.h> #include <sys/socket.h> #include<stdio.h> #include <arpa/inet.h> #include <string.h> #include<fcntl.h> main() { int sfd,l; char buf[1024]=""; struct sockaddr_in server,client; sfd=socket(AF_INET,SOCK_DGRAM,0); bzero(&server,sizeof(server)); server.sin_family=AF_INET; server.sin_port=htons(1300); inet_aton("localhost",&server.sin_addr); printf("bind=%d\n" ,bind(sfd,(struct sockaddr *)&server,sizeof(server))); l=sizeof(client); for(;;) { recvfrom(sfd,buf,1024,0,(struct sockaddr *)&client,&l); sendto(sfd,buf,strlen(buf),0,(struct sockaddr *)&client,l); printf("MESSAGE FROM CLIENT:%s\n",buf); } }

OUTPUT:
SERVER:
cc udpechoserver.c -o echo ./echo bind=0 MESSAGE FROM CLIENT:hello

CLIENT:
cc udpechoclient.c -o udpecho ./udpecho Enter the message: hello

15.RPC for mul and div 16.RPC for fact RMI pgm to calculate factorial of a number

PROGRAM CLIENT PROGRAM:

import java.io.*; import java.rmi.*; public class client { public static void main(String args[])throws Exception { try { String s="rmi://"+args[0]+"/abc"; serverint f=(serverint)Naming.lookup(s); DataInputStream m=new DataInputStream(System.in); int n1=Integer.parseInt(m.readLine()); System.out.println("the factorial is"+f.fact(n1)); } catch(Exception e) { System.out.println(e); } } } INTERFACE PROGRAM: import java.rmi.*; public interface serverint extends Remote { int fact(int n)throws Exception;

} IMPLEMENTATION PROGRAM: import java.rmi.*; import java.rmi.server.*; public class serverimpl extends UnicastRemoteObject implements serverint { public serverimpl()throws Exception { } public int fact(int n) { int i,c=1; for(i=1;i<=n;i++) { c=i*c; } return c; } } SERVER PROGRAM: import java.net.*; import java.rmi.*; public class server { public static void main(String args[]) { try { serverimpl m=new serverimpl(); Naming.rebind("abc",m); } catch(Exception e) { System.out.println("Exception"+e); } } } OUTPUT: SERVER WINDOW: C:\vino20>javac serverint.java C:\vino20>javac serverimpl.java

C:\vino20>javac server.java C:\vino20>rmic serverimpl C:\vino20>start rmiregistry C:\vino20>java server CLIENT WINDOW: C:\vino20>javac client.java Note: client.java uses or overrides a deprecated API. Note: Recompile with -Xlint:deprecation for details. C:\vino20>java client localhost 3 the factorial is 6

17.Domain Name system

18.HTTP Application

19.Multi user chat

You might also like