Sie sind auf Seite 1von 35

Exp. No.

______ Date: __________

Program for Byte Stuffing or Character Stuffing

#include<stdio.h>
#include<string.h>
#define FLAG_BYTE "$"
#define ESCAPE_BYTE "#"
void byte_stuff();
char input_buf[100];
char output_buf[100];
main()
{
int ans;
do{
input_buf[0]='\0';
output_buf[0]='\0';
printf("\nFLAG_BYTE:$,ESC_BYTE=#\n");
printf("\nEnter th data from Network Layer:");
scanf("%s",input_buf);
byte_stuff();
printf("\nData to the physical Layer:%s",output_buf);
printf("\nDo you want to continue?(Y: 1/N: 0):");
scanf("%d",&ans);
}while(ans!=0);
return 0;
}
void byte_stuff(void)
{
int i=0,j=1;
output_buf[0]='$';
for(;input_buf[i]!='\0';i++,j++)
{ if(input_buf[i]!='$' && input_buf[i]!='#')
output_buf[j]=input_buf[i];
else
{
output_buf[j++]='#';
output_buf[j]=input_buf[i];
}
}
output_buf[j]='$';
output_buf[j++]='\0';
}

Output:

JNTUK-UCEV COMPUTER NETWORK - Lab


Exp. No. ______ Date: __________

FLAG_BYTE:$,ESC_BYTE=#
Enter the data from Network Layer:cat
Data to the physical Layer:$cat
Do you want to continue?(Y: 1/N: 0):1
FLAG_BYTE:$,ESC_BYTE=#
Enter th data from Network Layer:apple
Data to the physical Layer:$apple
Do you want to continue?(Y: 1/N: 0):0

JNTUK-UCEV COMPUTER NETWORK - Lab


Exp. No. ______ Date: __________

Program for Bit Stuffing

#include<stdio.h>
#include<string.h>
#define DELIM_BIT_PATTERN "01111110"
#define SNDR_INPUT 0
#define SNDR_OUTPUT 1
#define REC_INPUT 2
#define REC_OUTPUT 3
char data[4][100];
int valid_data(void);
void sender_bit_stuff(void);
void receiver_process_data(void);
int main()
{
int ans;
do{
printf("\nEnter Data from Netwrok Layer in Binary Form:");
scanf("%s",data[SNDR_INPUT]);
if(!valid_data())
continue;
sender_bit_stuff();
printf("\nSenders Physical Layer Data:%s\n",data[SNDR_OUTPUT]);
strcpy(data[REC_INPUT],data[SNDR_OUTPUT]);
receiver_process_data();
printf("\nReceiver's Network Layer Data: %s\n",data[REC_OUTPUT]);
printf("\n\nDo you want to continue?(y: 1/n: 0)");
scanf("%d",&ans);
}while(ans!=0);
}

int valid_data()
{
char *p=data[SNDR_INPUT];
if(*p=='\0')
{
printf("\n***Enter Some DAta***\n");
return 0;
}

JNTUK-UCEV COMPUTER NETWORK - Lab


Exp. No. ______ Date: __________

while(*p!='\0')
{
if(*p!='1' && *p!='0')
{
printf("** this is not binary data. please Enter 0's and
1's\n");
}
p++;
}
return 1;
}
void sender_bit_stuff(void)
{
char *src=data[SNDR_INPUT];
char *dst=data[SNDR_OUTPUT];
int count=0;
strcpy(dst,DELIM_BIT_PATTERN);
dst+=strlen(DELIM_BIT_PATTERN);
while(*src!='\0')
{
if(count==5)
{
*dst='0';
dst+=1;
count=0;
}
if(*src=='1')
count++;
else
count=0;
*dst++=*src++;
}
if(*src=='\0' && count==5)
{
*dst='0';
dst+=1;
}
strcpy(dst,DELIM_BIT_PATTERN);
dst+=strlen(DELIM_BIT_PATTERN);
*dst='\0';
}

JNTUK-UCEV COMPUTER NETWORK - Lab


Exp. No. ______ Date: __________

void receiver_process_data(void)
{
char *src=data[REC_INPUT];
char *dst=data[REC_OUTPUT];
char *end;
int count=0;
src+=strlen(DELIM_BIT_PATTERN);
end=data[REC_INPUT]+strlen(data[REC_INPUT])-
strlen(DELIM_BIT_PATTERN);
while(src<=end)
{
if(count==5)
src+=1;
count=0;
if(*src=='1')
count++;
else
count=0;
*dst++=*src++;
}
*(dst-1)='\0';

return;
}

Output:

Enter Data from Netwrok Layer in Binary Form:01111111111110

Senders Physical Layer Data:01111110011111011111011001111110

Receiver's Network Layer Data: 0111110111110110

Do you want to continue?(y: 1/n: 0)0

JNTUK-UCEV COMPUTER NETWORK - Lab


Exp. No. ______ Date: __________

Program For Dijkstras algorithm to compute the Shortest path


through a graph.

#include<stdio.h>
int p[100],count=0,n,start,j,fix[100],dist[100],x,min=0,newdist,w[100]
[100],from[100],i;
int main()
{
printf("enter size of matrix:\n");
scanf("%d",&n);
printf("enter elements:\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&w[i][j]);
fix[i]=0;
dist[i]=100;
from[i]=0;
p[i]=0;
}
}
printf("enter starting vertex:\n");
scanf("%d",&start);
dijkstra(start);
return 0;
}
dijkstra(int start)
{
int count=0;
dist[start]=0;
do
{
x=find_min();
fix[x]=1;
count++;
update_dist(x);
find_path(x);
}while(count<=n);
}
int find_min()
{
for(i=0;i<n;i++)
{
if(fix[i]==0)
{
min=1;
break;

JNTUK-UCEV COMPUTER NETWORK - Lab


Exp. No. ______ Date: __________

}
}
for(i=min;i<n;i++)
{
if((fix[i]==0)&&(dist[min]>dist[i]))
min=i;
}
return min;
}
update_dist(int x)
{
for(i=0;i<n;i++)
{
if((w[x][i]>0)&&(fix[i]==0))
{
newdist=dist[x]+w[x][i];
if(newdist<dist[i])
{
dist[i]=newdist;
from[i]=x;
}
}
}
}
find_path(int x)
{
if(p[x]==0)
{
printf("cost of the path from %d to %d is %d\n",start,x,dist[x]);
p[x]=1;
}
if(start!=x)
find_path(from[x]);
}

JNTUK-UCEV COMPUTER NETWORK - Lab


Exp. No. ______ Date: __________

Output:
enter size of matrix:
5
enter elements:
00100
10010
10001
00001
00100
enter starting vertex:
3
cost of the path from 3 to 3 is 0
cost of the path from 3 to 4 is 1
cost of the path from 3 to 2 is 2
cost of the path from 3 to 1 is 100
cost of the path from 3 to 0 is 3

JNTUK-UCEV COMPUTER NETWORK - Lab


Exp. No. ______ Date: __________

Program for Distance Vector Algorithm by using Bellman Fords


Algorithm.

#include<stdio.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("enter the number of nodes:");
scanf("%d",&n);
printf("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][k]+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++)
for(j=0;j<n;j++)
{
printf("\n state value for router %d is\n",i+1);
for(j=0;j<n;j++)
{
printf("\n node %d via %d Distance %d", j+1,rt[i].from[j]+1,rt[i].dist[j]);
}
}
printf("\n");
}

JNTUK-UCEV COMPUTER NETWORK - Lab


Exp. No. ______ Date: __________

Output:

enter the number of nodes:3


enter the root matrix:
024
205
450
state value for router 1 is
node 1 via 1 distance 0
node 2 via 2 distance 2
node 3 via 3 distance 4
state value for router 2 is
node 1 via 1 distance 2
node 2 via 2 distance 0
node 3 via 3 distance 5
state value for router 3 is
node 1 via 1 distance 4
node 2 via 2 distance 5
node 3 via 3 distance 0

JNTUK-UCEV COMPUTER NETWORK - Lab


Exp. No. ______ Date: __________

Program to create pipe and to use fork system call.

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
int pfd[2];
char buf[30];
if(pipe(pfd)==-1)
{
perror("pipe failed");
exit(1);
}
if(!fork())
{
printf("CHILD: writing to the pipe\n");
write(pfd[1],"test",5);
printf("CHILD: exiting\n");
exit(0);
}
else
{
printf("PARENT: reading from pipe\n");
read(pfd[0],buf,5);
printf("PARENT: read\"%s\"\n",buf);
wait(NULL);
}
}

Output:

[14VV5A0562@localhost 14VV5A0562]$ cc file2.c


[14VV5A0562@localhost 14VV5A0562]$ ./a.out
CHILD: writing to the pipe
CHILD: exiting
PARENT: reading from pipe
PARENT: read"test"

JNTUK-UCEV COMPUTER NETWORK - Lab


Exp. No. ______ Date: __________

Program for creation of a two way pipe between two processes

#include<stdio.h>
#include<stdlib.h>
main()
{
int p1[2],p2[2],n,pid;
char buf1[25],buf2[25];
pipe(p1);
pipe(p2);
printf("\n readfds=%d %d\n",p1[0],p2[0]);
printf("\n writefds=%d %d\n",p1[1],p2[1]);
pid=fork();
if(pid==0)
{
close(p1[0]);
printf("\n CHILD PROCESS SENDING DATA\n");
write(p1[1],"where is GEC",25);
close(p2[1]);
read(p2[0],buf1,25);
printf(" reply from parent:%s\n",buf1);
sleep(2);
}
else
{
close(p1[1]);
printf("\n parent process receiving data\n");
n=read(p1[0],buf2,sizeof(buf2));
printf("\n data received from child through pipe:%s\n",buf2);
sleep(3);
close(p2[0]);
write(p2[1]," in gudlavalleru",25);
printf("\n reply send\n");
}
}

Output:
readfds=3 5
writefds=4 6
CHILD PROCESS SENDING DATA
parent process receiving data
data received from child through pipe: where is JNTUV
reply send
reply from parent: in Vizianagaram

JNTUK-UCEV COMPUTER NETWORK - Lab


Exp. No. ______ Date: __________

Program to implement TCP client and server application to echo


the given input sentence.

Client terminal:

#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#define SA struct sockaddr
void str_cli(FILE *fp,int sockfd);
main(int argc,char **argv)
{
int sockfd;
struct sockaddr_in servaddr;
if(argc!=3)
perror("\n Usage:host name & port");
sockfd=socket(AF_INET,SOCK_STREAM,0);
if(sockfd<0)
perror("error in opening socket");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(atoi(argv[2]));
inet_pton(AF_INET,argv[1],&servaddr.sin_addr);
connect(sockfd,(SA*)&servaddr,sizeof(servaddr));
str_cli(stdin,sockfd);
exit(0);
}
void str_cli(FILE *fp,int sockfd)
{
char send[100],recv[100];
int n;
printf("\n enter message");
while(fgets(send,100,fp)!=NULL)
{
write(sockfd,send,100);
if(n=read(sockfd,recv,100)<0)
perror("error in reading");
printf("%s\n",recv);
}
}

Server terminal:

JNTUK-UCEV COMPUTER NETWORK - Lab


Exp. No. ______ Date: __________

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
#include<errno.h>
#define SA struct sockaddr
void strserv(int sock);
main(int argc,char **argv)
{
int listenfd,connfd,pid,bfd,clilen;
struct sockaddr_in servaddr,cliaddr;
listenfd=socket(AF_INET,SOCK_STREAM,0);
if(listenfd<0)
perror("error in opening socket");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=INADDR_ANY;
servaddr.sin_port=htons(atoi(argv[1]));
bfd=bind(listenfd,(SA*)&servaddr,sizeof(servaddr));
if(bfd<0)
perror("binding error");
listen(listenfd,5);
for(;;)
{
clilen=sizeof(cliaddr);
connfd=accept(listenfd,(SA*)&cliaddr,&clilen);
if(connfd<0)
perror("accept errorr");
if((pid=fork())<0)
perror("fork error");
if((pid=fork())==0)
{
close(listenfd);
strserv(connfd);
close(connfd);
exit(0);
}

else
close(connfd);
}

JNTUK-UCEV COMPUTER NETWORK - Lab


Exp. No. ______ Date: __________

}
void strserv(int sock)
{
char buf[100];
int n,l;
while((n=read(sock,buf,100))>0)
{
if(n<0)
perror("error in reading");
printf("processsing request\n");
printf("The echo message is %s\n",buf);
printf("request completed\n");
l=strlen(buf);
n=write(sock,buf,l+1);
}
if(n<0)
perror("error in writing");
exit(0);
}

Output:

Server terminal:

[14VV5A0562@localhost 14VV5A0562]$ cc tcpsrv.c


[14VV5A0562@localhost 14VV5A0562]$ ./a.out 3452
processing request
The echo message is hi
request completed

Client terminal:

[14VV5A0562@localhost 14VV5A0562]$ cc tcpcli.c


[14VV5A0562@localhost 14VV5A0562]$ ./a.out 10.0.4.250 3452
enter messagehi
hi

Program to implement TCP client and server application to


reverse the given input sentence.

Client terminal:

JNTUK-UCEV COMPUTER NETWORK - Lab


Exp. No. ______ Date: __________

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#define SA struct sockaddr
void str_clirev(FILE *fp,int sockfd);
main(int argc,char **argv)
{
int sockfd;
struct sockaddr_in serv_addr;
if(argc!=2)
perror("\n usage:hostname &port");
sockfd=socket(AF_INET, SOCK_STREAM,0);
if(sockfd<0)
perror("open socket");
bzero(&serv_addr,sizeof(serv_addr));
serv_addr.sin_family=AF_INET;
serv_addr.sin_port=htons(atoi(argv[2]));
inet_pton(AF_INET,argv[1],&serv_addr.sin_addr);
connect(sockfd,(SA*)&serv_addr,sizeof(serv_addr));
str_clirev(stdin,sockfd);
exit(0);
}
void str_clirev(FILE *fp,int sockfd)
{
char sdata[100],rdata[100];
int n;
printf("enter the text \n");
while(fgets(sdata,100,fp)!=NULL)
{
sdata[strlen(sdata)-1]='\0';
write(sockfd,sdata,strlen(sdata));
if(n=read(sockfd,rdata,100)<0)
perror("error in reading");
rdata[n+strlen(sdata)]='\0';
printf("the reverse of the string is %s \n",rdata);
}
}

Server terminal:

#include<stdio.h>
#include<sys/types.h>

JNTUK-UCEV COMPUTER NETWORK - Lab


Exp. No. ______ Date: __________

#include<sys/socket.h>
#include<netinet/in.h>
#define SA struct sockaddr
void str_serv(int sock);
main(int argc,char **argv)
{
int listenfd,connfd,bfd,cli_len;
struct sockaddr_in serv_addr,cli_addr;
pid_t pid;
listenfd=socket(AF_INET,SOCK_STREAM,0);
if(listenfd<0)
perror("error in open socket");
bzero(&serv_addr,sizeof(serv_addr));
serv_addr.sin_family=AF_INET;
serv_addr.sin_addr.s_addr=INADDR_ANY;
serv_addr.sin_port=htons(atoi(argv[1]));
bfd=bind(listenfd,(SA*)&serv_addr,sizeof(serv_addr));
if(bfd<0)
perror("binding error");
listen(listenfd,5);
for(;;)
{
cli_len=sizeof(cli_addr);
connfd=accept(listenfd,(SA*)&cli_addr,&cli_len);
if(connfd<0)
perror("accept error");
if((pid=fork())<0)
perror("fork error");
if((pid=fork())==0)
{
close(listenfd);
str_serv(connfd);
exit(0);
}
else
close(connfd);
}
}
void str_serv(int sock)
{
char data[100],revdata[100];
int n,i,j,l;
while((n=read(sock,data,100))>0)

JNTUK-UCEV COMPUTER NETWORK - Lab


Exp. No. ______ Date: __________

{
printf("read data %d \n",n);
data[n]='\0';
l=strlen(data);
for(i=l-1,j=0;i>=0;i--,j++)
revdata[j]=data[i];
revdata[j]='\0';
printf("the reverse data is %s \n",revdata);
write(sock,revdata,l);
printf("\n");
}
}

Output:

Server terminal:

[14VV5A0562@localhost 14VV5A0562]$ cc rserv.c


[14VV5A0562@localhost 14VV5A0562]$ ./a.out 1234
read data 19
the reverse data is gnimmargorp krowten

Client terminal:

[14VV5A0562@localhost 14VV5A0562]$ cc rcli.c


[14VV5A0562@localhost 14VV5A0562]$ ./a.out 10.0.4.250 1234
usage:hostname &port: Success
enter the text
network programming
the reverse of the string is gnimmargorp krowten

Program to implement TCP client and server application to


transfer a file.

Client terminal:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>

JNTUK-UCEV COMPUTER NETWORK - Lab


Exp. No. ______ Date: __________

void dostuff(int); /* function prototype */


void error(char *msg)
{
perror(msg);
exit(1);
}

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


{
int sockfd, newsockfd, portno, clilen, pid;
struct sockaddr_in serv_addr, cli_addr;
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);
while (1)
{
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
error("ERROR on accept");
pid = fork();
if (pid < 0)
error("ERROR on fork");
if (pid == 0)
{ close(sockfd);
dostuff(newsockfd);
exit(0);
}
else
close(newsockfd);

JNTUK-UCEV COMPUTER NETWORK - Lab


Exp. No. ______ Date: __________

}
return 0;
}
void dostuff (int sock)
{
int n,bytesread;
char buffer[256];
FILE *stream;
bzero(buffer,256);
stream = fopen("sample.txt","r");
if(stream)
{
printf("Sending file [sample.txt]\n");
while(1)
{
bytesread = fread(buffer, 1, sizeof(buffer), stream);
if(bytesread <= 0)
break;
printf("Read %i bytes from file, sending them to network...\n",
bytesread);
n = write(sock,buffer,strlen(buffer));
printf("Wrote %i bytes to file", n);
if (n < 0)
error("ERROR writing to socket");
}
fclose(stream);
}
else
printf("Error, couldn't open file [sample.txt] to send!\n");
}
Server terminal:

#include<stdio.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<netdb.h>
#include<string.h>
void error(char *msg)
{ perror(msg);
exit(0);
}

JNTUK-UCEV COMPUTER NETWORK - Lab


Exp. No. ______ Date: __________

main(int argc, char **argv)


{
int sockfd, portno, n, bytesread;
struct sockaddr_in serv_addr;
struct hostent *server;
char buffer[256];
FILE *stream;
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 sockaddr *)&serv_addr,sizeof(serv_addr))<0);
error("ERROR connecting");
stream=fopen("sample.txt","w");
bzero(buffer,256);
if(stream)
{
printf("Recieving file [sample.txt]\n");
while(1)
{
bytesread = read(sockfd,buffer,255);
if(bytesread < 0)
error("ERROR reading from socket");
if(bytesread == 0)
break;
printf("Recieved %i bytes from file, writing them to file...\n",
bytesread);

JNTUK-UCEV COMPUTER NETWORK - Lab


Exp. No. ______ Date: __________

if(fwrite(buffer, 1, bytesread, stream)!= bytesread)


{
error("error");
break;
}
}
fclose(stream);
}
else
printf("Error, couldn't open file [sample.txt] to send!\n");
return 0;
}

Output:

Server terminal:

[14VV5A0562@localhost 14VV5A0562]$ cc recv.c


[14VV5A0562@localhost 14VV5A0562]$ ./a.out 10.0.4.250 2842
ERROR connecting: Success

Client terminal:

[14VV5A0562@localhost 14VV5A0562]$ cc sender.c


[14VV5A0562@localhost 14VV5A0562]$ ./a.out 2842
Sending file [sample.txt]
Read 31 bytes from file, sending them to network...
Wrote 31 bytes to file
Program to implement UDP client and server application to
transfer a file.

Client Program:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<unistd.h>
#define SERV_PORT 6349
main(int argc,char **argv)
{
char filename[80];
int sockfd;

JNTUK-UCEV COMPUTER NETWORK - Lab


Exp. No. ______ Date: __________

struct sockaddr_in servaddr;


sockfd=socket(AF_INET,SOCK_DGRAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(SERV_PORT);
inet_pton(AF_INET,argv[1],&servaddr.sin_addr);
printf("enter the file name");
scanf("%s",filename);
sendto(sockfd,filename,strlen(filename),0,
(structsockaddr*)&servaddr,sizeof(servaddr))
}

Server Program:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<sys/socket.h>
#include<sys/types.h>
#include<netinet/in.h>
#define SERV_PORT 6349
main(int argc,char **argv)
{
char filename[80],recvline[80];
FILE *fp;
struct sockaddr_in servaddr,cliaddr;
int clilen,sockfd;
sockfd=socket(AF_INET,SOCK_DGRAM,0);
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(SERV_PORT);
bind(sockfd,(struct sockaddr*)&servaddr,sizeof(servaddr));
clilen=sizeof(cliaddr);
recvfrom(sockfd,filename,80,0,(struct sockaddr*)&cliaddr,&clilen);
printf("\n date in the file is \n ");
fp=fopen(filename,"r");
while(fgets(recvline,80,fp)!=NULL) { printf("\n %s\n ",recvline);
}
fclose(fp);
}

Output:

JNTUK-UCEV COMPUTER NETWORK - Lab


Exp. No. ______ Date: __________

Client Program:

enter the file name: npfile


NETWORK PROGRAMMING LAB MANUAL

Server Program:

date in the file is: hai this is np lab something interesting

JNTUK-UCEV COMPUTER NETWORK - Lab


Exp. No. ______ Date: __________

Program to implement TCP concurrent server to convert a given


text into uppercase using multiplexing system call "select".

Client terminal:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <fcntl.h>
#define MAXBUFFER 1024
void sendstring(int , char *);
int main( int argc, char **argv )
{
int sd,fd;
char c;
struct sockaddr_in serveraddress;
char text[100];
int i=0;
sd = socket( AF_INET, SOCK_STREAM, 0 );
if( sd < 0 )
{
perror( "socket" );
exit( 1 );
}
if (argv[1] == NULL ) {
printf ("PL specfiy the server's IP Address \n");
exit(0);
}
if (argv[2] == NULL ) {
printf ("PL specify the server's Port No \n");
exit(0);
}
memset( &serveraddress, 0, sizeof(serveraddress) );
serveraddress.sin_family = AF_INET;
serveraddress.sin_port = htons(atoi(argv[2]));

JNTUK-UCEV COMPUTER NETWORK - Lab


Exp. No. ______ Date: __________

serveraddress.sin_addr.s_addr = inet_addr(argv[1]);
if (connect(sd,(struct sockaddr*)&serveraddress,sizeof(serveraddress))<0)
{
printf("Cannot Connect to server");
exit(1);
}
printf("enter sentence to end enter #");
while(1)
{
c=getchar();
if(c=='#')
break;
text[i++]=c;
}
text[i]='\0';
sendstring(sd,text);
close(sd);
return 0;
}
void sendstring(int sd, char *fname)
{
int n , byteswritten=0 , written ;
char buffer[MAXBUFFER];
strcpy(buffer , fname);
n=strlen(buffer);
while (byteswritten<n)
{
written=write(sd,buffer+byteswritten,(n-byteswritten));
byteswritten+=written;
}
printf("String : %s sent to server \n",buffer);
}

Server terminal:

#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>

JNTUK-UCEV COMPUTER NETWORK - Lab


Exp. No. ______ Date: __________

#include <arpa/inet.h>
#include <fcntl.h>
#define MAXLINE 100
int main(int argc, char **argv)
{
int k, i, maxi, maxfd, listenfd, connfd, sockfd;
int nready, client[FD_SETSIZE];
ssize_t n;
fd_set rset, allset;
char line[MAXLINE],buf[100];
socklen_t clilen;
struct sockaddr_in cliaddr, servaddr;
listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd < 0 )
{
perror("socket" );
exit(1);
}
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_addr.s_addr = htonl(INADDR_ANY);
servaddr.sin_port = htons(atoi(argv[1]));
bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr));
listen(listenfd,5);
maxfd = listenfd;
maxi = -1;
for (i = 0; i < FD_SETSIZE; i++)
client[i] = -1;
FD_ZERO(&allset);
FD_SET(listenfd, &allset);
for ( ; ; )
{
printf("Server:I am waiting-----Start of Main Loop\n");
rset = allset;
nready = select(maxfd+1, &rset, NULL, NULL, NULL);
if (FD_ISSET(listenfd, &rset)) {
clilen = sizeof(cliaddr);
connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &clilen);
#ifdef NOTDEF
printf("new client: %s, port %d\n",
inet_ntop(AF_INET, &cliaddr.sin_addr, buf, NULL),
ntohs(cliaddr.sin_port));
#endif

JNTUK-UCEV COMPUTER NETWORK - Lab


Exp. No. ______ Date: __________

for (i = 0; i < FD_SETSIZE; i++)


if (client[i] < 0)
{
client[i] = connfd;
break;
}
if (i == FD_SETSIZE)
{
printf("too many clients");
exit(0);
}
FD_SET(connfd, &allset);
if (connfd > maxfd)
maxfd = connfd;
if (i > maxi)
maxi = i;
if (--nready <= 0)
continue;
}
for (i = 0; i <= maxi; i++)
{
if ( (sockfd = client[i]) < 0)
continue;
if (FD_ISSET(sockfd, &rset)) {
if ( (n = read(sockfd, line, MAXLINE)) == 0)
{
close(sockfd);
FD_CLR(sockfd, &allset);
client[i] = -1;
}
else
{
printf("\n EXECUTION at server\n");
for(k=0;line[k]!='\0';k++)
printf("%c",toupper(line[k]));
write(sockfd, line, n);
}

if (--nready <= 0)
break;
}
}

JNTUK-UCEV COMPUTER NETWORK - Lab


Exp. No. ______ Date: __________

}
}

Output:

Server terminal:

[14VV5A0562@localhost 14VV5A0562]$ cc selectserver.c


[14VV5A0562@localhost 14VV5A0562]$ ./a.out 6738
Server:I am waiting-----Start of Main Loop
Server:I am waiting-----Start of Main Loop

EXECUTION at server
NETWORK PROGRAMMINGServer:I am waiting-----Start of Main Loop
Server:I am waiting-----Start of Main Loop

Client terminal:

[14VV5A0562@localhost 14VV5A0562]$ cc selectclient.c


[14VV5A0562@localhost 14VV5A0562]$ ./a.out 10.0.4.200 6738
enter sentence to end enter #network programming#
String : network programming sent to server

JNTUK-UCEV COMPUTER NETWORK - Lab


Exp. No. ______ Date: __________

Program to implement UDP client and server application to echo


the given input sentence.

Client terminal:

#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#define SA struct sockaddr
#define MAXSIZE 1024
main(int argc,char **argv)
{
int sockfd;
struct sockaddr_in servaddr;
if(argc!=3)
perror("\n Usage:host name & port");
sockfd=socket(AF_INET,SOCK_DGRAM,0);
if(sockfd<0)
perror("error in opening socket");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(atoi(argv[2]));
inet_pton(AF_INET,argv[1],&servaddr.sin_addr);
dg_cli(stdin,sockfd,(SA*) &servaddr,sizeof(servaddr));
exit(0);
}
dg_cli(FILE *fp,int sockfd,const SA *pservaddr,socklen_t servlen)
{
socklen_t len;
len=servlen;
char sdata[MAXSIZE],rdata[MAXSIZE];
int n;
printf("\n enter message");
while(fgets(sdata,MAXSIZE,fp)!=NULL)
{
sendto(sockfd,sdata,strlen(sdata),0,pservaddr,len);
if((n=recvfrom(sockfd,rdata,MAXSIZE,0,NULL,NULL))<=0)
return;
rdata[strlen(rdata)]='\0';
printf("%s",rdata);
}}
Server terminal:

JNTUK-UCEV COMPUTER NETWORK - Lab


Exp. No. ______ Date: __________

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
#define SA struct sockaddr
#define MAXSIZE 1024
main(int argc,char **argv)
{
struct sockaddr_in servaddr,cliaddr;
int sockfd,bd;
sockfd=socket(AF_INET,SOCK_DGRAM,0);
if(sockfd<0)
perror("error in opening socket");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(atoi(argv[1]));
bd=bind(sockfd,(SA*)&servaddr,sizeof(servaddr));
if(bd<0)
perror("bind error");
dg_echo(sockfd,(SA *)&cliaddr,sizeof(cliaddr));

}
dg_echo(int sockfd,SA *pcliaddr,socklen_t clilen)
{
int n;
socklen_t len;
len=clilen;
char msg[MAXSIZE];
for(;;)
{
n=recvfrom(sockfd,msg,MAXSIZE,0,pcliaddr,&len);
msg[n]='\0';
sendto(sockfd,msg,n+1,0,pcliaddr,len);
printf("\n");
}
}

Output:

Server terminal:

JNTUK-UCEV COMPUTER NETWORK - Lab


Exp. No. ______ Date: __________

[14VV5A0562@localhost 14VV5A0562]$ cc udpsrvecho.c


[14VV5A0562@localhost 14VV5A0562]$ ./a.out 5958

Client terminal:

[14VV5A0562@localhost 14VV5A0562]$ cc udpcliecho.c


[14VV5A0562@localhost 14VV5A0562]$ ./a.out 10.0.4.250 5958
enter messagehello
hello
network
network
programming
programming

JNTUK-UCEV COMPUTER NETWORK - Lab


Exp. No. ______ Date: __________

Program to implement UDP client and server application to


reverse the given input sentence.

Client terminal:

#include<stdio.h>
#include<sys/types.h>
#include<netinet/in.h>
#include<sys/socket.h>
#define SA struct sockaddr
#define MAXSIZE 1024
main(int argc,char **argv)
{
int sockfd;
struct sockaddr_in servaddr;
if(argc!=3)
perror("\n Usage:host name & port");
sockfd=socket(AF_INET,SOCK_DGRAM,0);
if(sockfd<0)
perror("error in opening socket");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_port=htons(atoi(argv[2]));
inet_pton(AF_INET,argv[1],&servaddr.sin_addr);
str_cli(stdin,sockfd,(SA*) &servaddr,sizeof(servaddr));
exit(0);
}
str_cli(FILE *fp,int sockfd,const SA *pservaddr,socklen_t servlen)
{
socklen_t len;
len=servlen;
char sdata[MAXSIZE],rdata[MAXSIZE];
int n;
printf("\n enter message");
while(fgets(sdata,MAXSIZE,fp)!=NULL)
{
sdata[strlen(sdata)-1]='\0';
sendto(sockfd,sdata,strlen(sdata),0,pservaddr,len);
if((n=recvfrom(sockfd,rdata,MAXSIZE,0,NULL,NULL))<=0)
return;
rdata[n]='\0';
fputs(rdata,stdout);
printf("\n");

JNTUK-UCEV COMPUTER NETWORK - Lab


Exp. No. ______ Date: __________

printf("enter another text message");

}
}

Server terminal:

#include<stdio.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<netinet/in.h>
#include<string.h>
#define SA struct sockaddr
#define MAXSIZE 1024
main(int argc,char **argv)
{
struct sockaddr_in servaddr,cliaddr;
int sockfd,bd;
sockfd=socket(AF_INET,SOCK_DGRAM,0);
if(sockfd<0)
perror("error in opening socket");
bzero(&servaddr,sizeof(servaddr));
servaddr.sin_family=AF_INET;
servaddr.sin_addr.s_addr=htonl(INADDR_ANY);
servaddr.sin_port=htons(atoi(argv[1]));
bd=bind(sockfd,(SA*)&servaddr,sizeof(servaddr));
if(bd<0)
perror("bind error");
dg_reverse(sockfd,(SA *)&cliaddr,sizeof(cliaddr));
}
dg_reverse(int sockfd,SA *pcliaddr,socklen_t clilen)
{
int n,i,j,l;
socklen_t len;
len=clilen;
char data[MAXSIZE],revdata[MAXSIZE];

while((n=recvfrom(sockfd,data,MAXSIZE,0,pcliaddr,&len))>0)

{
printf("read data..%d\n",n);

JNTUK-UCEV COMPUTER NETWORK - Lab


Exp. No. ______ Date: __________

data[n]='\0';
l=strlen(data);
for(i=l-1,j=0;i>=0;i--,j++)
revdata[j]=data[i];
revdata[j]='\0';
printf("The reverse data is %s\n",revdata);
sendto(sockfd,revdata,n,0,pcliaddr,len);
printf("\n");
}
}

Output:

Server terminal:

[14VV5A0562@localhost 14VV5A0562]$ cc udpsrv.c


[14VV5A0562@localhost 14VV5A0562]$ ./a.out 1234
read data..7
The reverse data is krowten

read data..11
The reverse data is gnimmargorp

Client terminal:

[14VV5A0562@localhost 14VV5A0562]$ cc udpclirev.c


[14VV5A0562@localhost 14VV5A0562]$ ./a.out 10.0.4.250 1234

enter messagenetwork
krowten
enter another text messageprogramming
gnimmargorp
enter another text message

JNTUK-UCEV COMPUTER NETWORK - Lab