IMPLEMENTATION OF CHAT USING TCP

AIM:

ALGORITHM: Server:

Client:

PROGRAM:
SERVER: #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> void error(char *msg) { perror(msg); exit(1); } int main(int argc,char *argv[]) { int sockfd,newsockfd,portno,clilen; char buffer1[256],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) 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 binding"); bzero(buffer,256); n=read(newsockfd,buffer,255); if(n<0) error("ERROR reading from socket"); printf("HERE is the message: %s\n",buffer); printf("ENTER THE MESSAGE TO CLIENT\n"); scanf("%s",buffer1); n=write(newsockfd,buffer1,256); if(n<0) error("ERROR writing to socket"); return 0; }

CLIENT: #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<netdb.h> void error(char *msg) {

perror(msg); exit(0); { int sockfd,portno,n; struct sockaddr_in serv_addr; struct hostent *server; char buffer[256]; if(argc<3) { fprintf(stderr,"usage %s hostname port\n",argv[0]); exit(0); } portno=atoi(argv[2]); sockfd=socket(AF_INET,SOCK_STREAM,0); if(sockfd<0) error("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,&serv_addr,sizeof(serv_addr)) <0) error("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) error("ERROR reading from socket"); printf("%s\n",buffer); return 0; }

OUTPUT:
SERVER [student14@localhost student14]$ cc chats.c [student14@localhost student14]$ ./a.out 4032 CLIENT [student14@localhost student14]$ cc chatc.c [student14@localhost student14]$ ./a.out 192.168.0.60 4032 Please enter the message ASSHUSS

SERVER HERE is the message: ASSHUSS

ENTER THE MESSAGE TO CLIENT HUSSASS

CLIENT HUSSASS

RESULT:

IMPLEMENTATION OF DATE AND TIME CLIENT & SERVER USING TCP SOCKETS AIM:

ALGORITHM: Server:

Client:

PROGRAM:
SERVER: #include <stdio.h> #include <sys/types.h> #include<time.h> #include <sys/socket.h> #include <netinet/in.h> void error(char *msg) { perror(msg); exit(1); }int main(int argc, char *argv[]) { int sockfd, newsockfd, portno, clilen; char buffer1[256],buffer[256]; time_t ticks; 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) 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); ticks =time(NULL); snprintf(buffer,256,”%.24s\r\n”,ctime(&ticks)); write(newsockfd,buffer,256); close(newsockfd); return 0; } CLIENT: #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> void error(char *msg) { perror(msg); exit(0); }

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,"usage %s hostname port\n", argv[0]); exit(0); } portno = atoi(argv[2]); sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) error("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 servaddr *) &serv_addr,sizeof(serv_addr)) < 0) error("ERROR connecting"); while((n=read(sockfd,buffer,256))>0){ buffer[n]=0;

if(fputs(buffer,stdout)==EOF) error(“fputs error”); } if (n < 0) error("ERROR reading from socket"); printf("%s\n",buffer); return 0; }

OUTPUT:
SERVER:

[student14@localhost student14]$ cc days.c [student14@localhost student14]$ ./a.out 4032

CLIENT [student14@localhost student14]$ cc dayc.c [student14@localhost student14]$ ./a.out 192.168.0.160 4032

Thu Jan27 12:05:54 2011 Thu Jan27 12:05:54 2011

RESULT:

IMPLEMENTATION OF ECHOCLIENT & ECHOSERVER USING TCP SOCKETS

AIM:

ALGORITHM: Server:

Client:

PROGRAM:
SERVER: #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> void error(char *msg) { perror(msg); exit(1); } 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) 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 binding"); 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,buffer,256); if(n<0) error("ERROR writing to socket"); return 0; }

CLIENT: #include<stdio.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<netdb.h> void error(char *msg) { perror(msg);

exit(0); { int sockfd,portno,n; struct sockaddr_in serv_addr; struct hostent *server; char buffer[256]; if(argc<3) { fprintf(stderr,"usage %s hostname port\n",argv[0]); exit(0); } portno=atoi(argv[2]); sockfd=socket(AF_INET,SOCK_STREAM,0); if(sockfd<0) error("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,&serv_addr,sizeof(serv_addr)) <0) error("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) error("ERROR reading from socket"); printf("%s\n",buffer); return 0; }

OUTPUT:
SERVER [student14@localhost student14]$ cc chats.c [student14@localhost student14]$ ./a.out 4032

CLIENT [student14@localhost student14]$ cc chatc.c [student14@localhost student14]$ ./a.out 192.168.0.60 4032 Please enter the message: WELCOME SERVER HERE is the message: WELCOME

RESULT:

DATACOMMUNICATION USING UDP

AIM:

ALGORITHM: Server:

Client:

PROGRAM:
SERVER: #include <arpa/inet.h> #include <netinet/in.h> #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <unistd.h> #define BUFLEN 512 #define NPACK 10 #define PORT 4032 void diep(char *s) { perror(s); exit(1); } int main(void) { struct sockaddr_in si_me, si_other; int s, i, slen=sizeof(si_other); char buf[BUFLEN]; if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1) diep("socket"); memset((char *) &si_me, 0, sizeof(si_me)); si_me.sin_family = AF_INET; si_me.sin_port = htons(PORT); si_me.sin_addr.s_addr = htonl(INADDR_ANY); if (bind(s, &si_me, sizeof(si_me))==-1)

diep("bind"); for (i=0; i<NPACK; i++) { if (recvfrom(s, buf, BUFLEN, 0, &si_other, &slen)==-1) diep("recvfrom()"); printf("Received packet from %s:%d\nData: %s\n\n", inet_ntoa(si_other.sin_addr), ntohs(si_other.sin_port), buf); } close(s); return 0; }

CLIENT: #define SRV_IP "192.168.0.60" #include <arpa/inet.h> #include <netinet/in.h> #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <unistd.h> #define BUFLEN 512 #define NPACK 10 #define PORT 4032 void diep(char *s) { perror(s); exit(1); } int main(void)

{ struct sockaddr_in si_other; int s, i, slen=sizeof(si_other); char buf[BUFLEN]; if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1) diep("socket"); memset((char *) &si_other, 0, sizeof(si_other)); si_other.sin_family = AF_INET; si_other.sin_port = htons(PORT); if (inet_aton(SRV_IP, &si_other.sin_addr)==0) { fprintf(stderr, "inet_aton() failed\n"); exit(1); } for (i=0; i<NPACK; i++) { printf("Sending packet %d\n", i); sprintf(buf, "This is packet %d\n", i); if (sendto(s, buf, BUFLEN, 0, &si_other, slen)==-1) diep("sendto()"); } close(s); return 0; }

OUTPUT:
SERVER [student14@localhost student14]$ cc udps.c udps.c: In function `main': udps.c:26: warning: passing arg 2 of `bind' from incompatible pointer type udps.c:30: warning: passing arg 5 of `recvfrom' from incompatible pointer type [student14@localhost student14]$ ./a.out 4032 CLIENT [student14@localhost student14]$ cc udpc.c udpc.c: In function `main': udpc.c:35: warning: passing arg 5 of `sendto' from incompatible pointer type [student14@localhost student14]$ ./a.out 4032 Sending packet 0 Sending packet 1 Sending packet 2 Sending packet 3 Sending packet 4 Sending packet 5 Sending packet 6 Sending packet 7 Sending packet 8 Sending packet 9 SERVER Received packet from 192.168.0.60:32871 Data: This is packet 0

Received packet from 192.168.0.60:32871 Data: This is packet 1

Received packet from 192.168.0.60:32871 Data: This is packet 2

Received packet from 192.168.0.60:32871 Data: This is packet 3

Received packet from 192.168.0.60:32871 Data: This is packet 4

Received packet from 192.168.0.60:32871 Data: This is packet 5

Received packet from 192.168.0.60:32871 Data: This is packet 6

Received packet from 192.168.0.60:32871 Data: This is packet 7

Received packet from 192.168.0.60:32871 Data: This is packet 8

Received packet from 192.168.0.60:32871 Data: This is packet 9

RESULT:

REMOTE COMMAND EXECUTION

AIM:

ALGORITHM: Server:

Client:

PROGRAM:
SERVER: import java.net.*; import java.io.*; public class rces { public static void main(String args[])throws IOException { ServerSocket s1=null; try { s1=new ServerSocket(98); } catch(IOException u1) { System.err.println("Could not found port 98"); System.exit(1); } Socket c=null; try { c=s1.accept(); System.out.println("connection from"+c); } catch(IOException e) { System.out.println("accept failed"); System.exit(1);

} PrintWriter out=new PrintWriter(c.getOutputStream(),true); String L; BufferedReader sin=new BufferedReader(new InputStreamReader(System.in)); System.out.println("I am ready type now"); while((L=sin.readLine())!=null) { out.println(L); } out.close(); sin.close(); c.close(); s1.close(); } } CLIENT: import java.net.*; import java.io.*; public class rce { public static void main(String args[])throws IOException { Socket s=null; BufferedReader b=null; try { s=new Socket(InetAddress.getLocalHost(),98); b=new BufferedReader(new InputStreamReader(s.getInputStream())); }

catch(UnknownHostException u) { System.err.println("I dont know host"); System.exit(0); } String inp; inp=b.readLine(); Runtime r=Runtime.getRuntime(); r.exec(inp); b.close(); s.close(); } }

OUTPUT:
SERVER C:\Documents and Settings\student>cd\ C:\>d: D:\>set path="D:\JDK1.6bin" D:\>cd netlab D:\netlab>javac rces.java D:\netlab>java rces

CLIENT C:\Documents and Settings\student>cd\ C:\>d: D:\>set path="D:\JDK1.5\bin"

D:\>cd netlab D:\netlab>javac rce.java D:\netlab>java rce SERVER connection fromSocket[addr=/192.168.0.33,port=1045,localport=98] I am ready type now notepad

RESULT:

REMOTE METHOD INVOCATION

AIM:

ALGORITHM:

Server:

Client:

PROGRAM:
SERVER:

import java.rmi.*; import java.rmi.server.*; public class Rs extends UnicastRemoteObject implements multiply { public Rs() throws RemoteException { } public static void main(String args[])throws Exception { System.out.println("server starts"); Rs s=new Rs(); Naming.rebind("Rs",s); } public int multiply(int a,int b)throws RemoteException { return(a*b); } }

CLIENT: import java.io.*; import java.rmi.*; import java.net.*; public class rc {

public static void main(String args[])throws Exception { InputStreamReader i=new InputStreamReader(System.in); BufferedReader b1=new BufferedReader(i); InetAddress ia=InetAddress.getLocalHost(); String s=ia.toString(); String ip=s.substring(s.indexOf("/")+1); String ur1="rmi://"+ip+"/Rs"; System.out.println("remote method invocation-bill calculation"); multiply m=(multiply)Naming.lookup(ur1); System.out.println("Enter the number of item purchased"); String temp=b1.readLine(); int a=Integer.parseInt(temp); System.out.println("Enter the unit price"); String temp1=b1.readLine(); int b=Integer.parseInt(temp1); System.out.println("The total amt is Rs"+m.multiply(a,b)); } }

INTERFACE: import java.rmi.*; public interface multiply extends Remote { int multiply(int a,int b)throws RemoteException; }

OUTPUT:
SERVER C:\Documents and Settings\student>cd\ C:\>d: D:\>cd netlab D:\netlab>set path="D:\JDK1.6\bin" D:\netlab>path PATH="D:\JDK1.6\bin" D:\netlab>javac Rs.java

CLIENT C:\Documents and Settings\student>cd\ C:\>D: D:\>cd netlab D:\netlab>set path="D:\JDK1.6\bin" D:\netlab>javac rc.java

INTERFACE C:\Documents and Settings\student>cd\ C:\>d: D:\>cd netlab D:\ netlab >set path=" D:\ netlab >set path="D:\JDK1.6\bin" D:\ netlab >javac multiply.java

SERVER D:\ netlab >rmic Rs D:\ netlab >start rmiregistry

D:\ netlab >java Rs server starts CLIENT remote method invocation-bill calculation Enter the number of item purchased 3 Enter the unit price 45 The total amt is Rs135

RESULT:

BIT STUFFING

AIM:

ALGORITHM:

PROGRAM:
#include<stdio.h> #include<string.h> main() { char ip[20],op[30],i,j=0,count=0; printf("Enter input bits:\n"); gets(ip); for(i=0;i<strlen(ip);i++) { op[j++]=ip[i]; if(ip[i]=='1') count++; else count=0; if(count==5) { count=0; op[j++]='0'; } } op[j]='\0'; printf("Bit stuffed string is:%s",op); }

OUTPUT:
[student14@localhost student14]$ cc bs.c [student14@localhost student14]$ ./a.out Enter input bits: 111111111111111 Bit stuffed string is:111110111110111110 [student14@localhost student14]$

RESULT:

SIMULATION OF SLIDING WINDOW PROTOCOL

AIM:

ALGORITHM:

PROGRAM:
PROGRAM:

#include<stdio.h> main() { int ack,option,choice; char data[100]; do { printf(“\n 1.sender\n2.receiver\n3.exit\n”); printf(“enter the choice:”); scanf(“%d”,&choice); switch(choice) { case 1: printf(“enter the option \n1.data to send\n2.status\n”); scanf(“%d”,&option); switch(option) { case1: printf(“enter the data to be send:”); scanf (“%s”,data); printf(“need ack frame sendind…”); printf(“\n1.sender\n2.receiver\n3.exit\n”); printf(“enter the choice:”); scanf(“%d”,&choice); if(choice==2)

{ printf(“the data received is %s”,data); printf(“\n if you receive it properly press1 for ack it otherwise press 0:”); scanf(“%d”,&ack); printf(“\n now you are in sender side\n”); printf(“\n 1.data to send\n2.status\n3.enter your choice:”); scanf(“%d”,&option); if(option==2) { if(ack==1) printf(“\n succeeded…ack received”); else printf(“\n sorry… try again later”); } else printf(“\n you are again trying to send the data without getting the ack!!!”); } else exit(0); break; case 2: printf(“check for the status”); break; default: printf(“enter a valid option”); } break; case 2: printf(“send the data first”);

break; case 3: exit(0); break; default: printf(“enter a valid choice”); } }while(choice<4); }

OUTPUT:
1.sender 2.receiver 3.exit Enter the choice:1 Enter the option 1.data to send 2.status 1 Enter the data to be send:555 Need ack frame sending… 1.sender 2.reciver 3.exit Enter the choice:2 The data received is 555 If you receive it properly press1 for ack it otherwise press 0:1 Now you are in sender side 1.data to send 2.status Enter your choice:2 Succeeded…..ack received 1.sender 2.reciver 3.exit Enter the choice:3

RESULT:

DOMAIN NAME SYSTEM

AIM:

ALGORITHM:

PROGRAM:
#include<stdio.h> #include<errno.h> #include<netdb.h> #include<sys/types.h> #include<sys/socket.h> #include<netinet/in.h> #include<arpa/inet.h> int main(int argc,char *argv[]) { int i; struct hostent *he; struct in_addtr **addr_list; if(argc!=2) { fprintf(stderr,"usage:ghbn hostname\n"); return 1; } if((he=gethostbyname(argv[1]))==NULL) { herror("gethostbyname"); return 2; } printf("Official name is: %s \n",he->h_name); printf(" IP Addresses : "); addr_list=(struct in_addr **)he->h_addr_list; for(i=0;addr_list[i]!=NULL;i++) {

printf("%s",inet_ntoa(*addr_list[i])); } printf("\n"); return 0; }

OUTPUT:
[student15@localhost student15]$ cc dns.c [student15@localhost student15]$ ./a.out www.google.com Official name is: www-notmumbai.l.google.com IP Addresses : 64.233.189.104 [student15@localhost student15]$

RESULT:

Sign up to vote on this title
UsefulNot useful