0% found this document useful (0 votes)
1K views10 pages

Stop and Wait Protocol

The document contains code for an echo client-server program. The echo client code connects to an echo server, sends a message, and prints the server's response. The echo server code binds to a port, accepts connections, reads the client's message, writes it back to the client, and closes the connection. It uses sockets for network communication and supports multiple concurrent clients.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views10 pages

Stop and Wait Protocol

The document contains code for an echo client-server program. The echo client code connects to an echo server, sends a message, and prints the server's response. The echo server code binds to a port, accepts connections, reads the client's message, writes it back to the client, and closes the connection. It uses sockets for network communication and supports multiple concurrent clients.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

/* Stop and Wait Protocol ( Flow Control ) */

#include<stdio.h>
#include<bios.h>
#include<conio.h>
#define COM1 0
#define SETTINGS 0x82
#define DTR 0x100
void main()
{
int ch,status,i=0,true=1;
char msg[100],ack,rec;
clrscr();
bioscom(0,SETTINGS,COM1);
printf("\n 1.Transmit\n 2.Receive");
printf("\n\n Enter the operation code : ");
scanf("%d",&ch);
switch(ch)
{
case 1 : printf("\n Enter the Message \n ");
scanf("%s",&msg);
while(msg[i]!='\0')
{
bioscom(1,msg[i],COM1);
printf("\n%c",msg[i]);
do
{
while(1)
{
status=bioscom(3,0,COM1);
if(status & DTR)
{
ack=bioscom(2,0,COM1) & 0x7F;
if(ack=='0')
{
bioscom(1,msg[i],COM1);
printf("\n%c - retransmitted ",msg[i]);
}
break;
}
}
}while(ack!='1');
i++;
}
break;
case 2 : printf("\n Receiver \t Press Escape to quit\n\n");
do

{
status=bioscom(3,0,COM1);
if(status & DTR)
{
rec=bioscom(2,0,COM1);
printf("\n%c -- Enter ACK(1 for +ve, 0 for -ve) :",rec);
ack=getche();
bioscom(1,ack,COM1);
}
if(kbhit())
if(getch()=='\x1b')
true=0;
}while(true);
break;
default : printf("\n Invalid Selection");
}
}

#include<stdio.h>
#include<conio.h>
void main()
{
char sender[50],receiver[50];
int i,winsize;
clrscr();
printf("\n ENTER THE WINDOWS SIZE : ");
scanf("%d",&winsize);
printf("\n SENDER WINDOW IS EXPANDED TO STORE MESSAGE OR WINDOW
\n");
printf("\n ENTER THE DATA TO BE SENT: ");
fflush(stdin);
gets(sender);
for(i=0;i<winsize;i++)
receiver[i]=sender[i];
receiver[i]=NULL;
printf("\n MESSAGE SEND BY THE SENDER:\n");
puts(sender);
printf("\n WINDOW SIZE OF RECEIVER IS EXPANDED\n");
printf("\n ACKNOWLEDGEMENT FROM RECEIVER \n");
for(i=0;i<winsize;i++);
printf("\n ACK:%d",i);
printf("\n MESSAGE RECEIVED BY RECEIVER IS : ");
puts(receiver);
printf("\n WINDOW SIZE OF RECEIVER IS SHRINKED \n");
getch();
}
Output
OUTPUT:
ENTER THE WINDOWS SIZE : 10
SENDER WINDOW IS EXPANDED TO STORE MESSAGE OR WINDOW
ENTER THE DATA TO BE SENT: ForgetCode.com
MESSAGE SEND BY THE SENDER:
ForgetCode.com
WINDOW SIZE OF RECEIVER IS EXPANDED
ACKNOWLEDGEMENT FROM RECEIVER
ACK:5
MESSAGE RECEIVED BY RECEIVER IS : ForgetCode

WINDOW SIZE OF RECEIVER IS SHRINKED


Write a code simulating ARP /RARP protocols.
arpserver.c
#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 name:");
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("\nARP table at serverside is=\n%s",shmptr);
shmdt(shmptr);
}
arpclient.c
#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 ARPtable is:");
printf("%s",shmptr);
printf("\n1.ARP\n2.RARP\n3.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);
}
}

Echo client-server
/*
* echoclient.c - A simple connection-based client
* usage: echoclient <host> <port>
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#define BUFSIZE 1024
/*
* error - wrapper for perror
*/
void error(char *msg) {
perror(msg);
exit(0);
}
int main(int argc, char **argv) {
int sockfd, portno, n;
struct sockaddr_in serveraddr;
struct hostent *server;
char *hostname;
char buf[BUFSIZE];
/* check command line arguments */
if (argc != 3) {
fprintf(stderr,"usage: %s <hostname> <port>\n", argv[0]);
exit(0);
}
hostname = argv[1];
portno = atoi(argv[2]);
/* socket: create the socket */
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
/* gethostbyname: get the server's DNS entry */

server = gethostbyname(hostname);
if (server == NULL) {
fprintf(stderr,"ERROR, no such host as %s\n", hostname);
exit(0);
}
/* build the server's Internet address */
bzero((char *) &serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET;
bcopy((char *)server->h_addr,
(char *)&serveraddr.sin_addr.s_addr, server->h_length);
serveraddr.sin_port = htons(portno);
/* connect: create a connection with the server */
if (connect(sockfd, &serveraddr, sizeof(serveraddr)) < 0)
error("ERROR connecting");
/* get message line from the user */
printf("Please enter msg: ");
bzero(buf, BUFSIZE);
fgets(buf, BUFSIZE, stdin);
/* write: send the message line to the server */
n = write(sockfd, buf, strlen(buf));
if (n < 0)
error("ERROR writing to socket");
/* read: print the server's reply */
bzero(buf, BUFSIZE);
n = read(sockfd, buf, BUFSIZE);
if (n < 0)
error("ERROR reading from socket");
printf("Echo from server: %s", buf);
close(sockfd);
return 0;
}

/*
* echoserver.c - A simple connection-based echo server
* usage: echoserver <port>
*/
#include <stdio.h>

#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define BUFSIZE 1024
#if 0
/*
* Structs exported from netinet/in.h (for easy reference)
*/
/* Internet address */
struct in_addr {
unsigned int s_addr;
};
/* Internet style socket address */
struct sockaddr_in {
unsigned short int sin_family; /* Address family */
unsigned short int sin_port; /* Port number */
struct in_addr sin_addr;
/* IP address */
unsigned char sin_zero[...]; /* Pad to size of 'struct sockaddr' */
};
/*
* Struct exported from netdb.h
*/
/* Domain name service (DNS) host entry */
struct hostent {
char *h_name;
/* official name of host */
char **h_aliases; /* alias list */
int h_addrtype; /* host address type */
int h_length;
/* length of address */
char **h_addr_list; /* list of addresses */
}
#endif
/*
* error - wrapper for perror
*/

void error(char *msg) {


perror(msg);
exit(1);
}
int main(int argc, char **argv) {
int listenfd; /* listening socket */
int connfd; /* connection socket */
int portno; /* port to listen on */
int clientlen; /* byte size of client's address */
struct sockaddr_in serveraddr; /* server's addr */
struct sockaddr_in clientaddr; /* client addr */
struct hostent *hostp; /* client host info */
char buf[BUFSIZE]; /* message buffer */
char *hostaddrp; /* dotted decimal host addr string */
int optval; /* flag value for setsockopt */
int n; /* message byte size */
/* check command line args */
if (argc != 2) {
fprintf(stderr, "usage: %s <port>\n", argv[0]);
exit(1);
}
portno = atoi(argv[1]);
/* socket: create a socket */
listenfd = socket(AF_INET, SOCK_STREAM, 0);
if (listenfd < 0)
error("ERROR opening socket");
/* setsockopt: Handy debugging trick that lets
* us rerun the server immediately after we kill it;
* otherwise we have to wait about 20 secs.
* Eliminates "ERROR on binding: Address already in use" error.
*/
optval = 1;
setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR,
(const void *)&optval , sizeof(int));
/* build the server's internet address */
bzero((char *) &serveraddr, sizeof(serveraddr));
serveraddr.sin_family = AF_INET; /* we are using the Internet */
serveraddr.sin_addr.s_addr = htonl(INADDR_ANY); /* accept reqs to any IP addr */
serveraddr.sin_port = htons((unsigned short)portno); /* port to listen on */
/* bind: associate the listening socket with a port */

if (bind(listenfd, (struct sockaddr *) &serveraddr,


sizeof(serveraddr)) < 0)
error("ERROR on binding");
/* listen: make it a listening socket ready to accept connection requests */
if (listen(listenfd, 5) < 0) /* allow 5 requests to queue up */
error("ERROR on listen");
/* main loop: wait for a connection request, echo input line,
then close connection. */
clientlen = sizeof(clientaddr);
while (1) {
/* accept: wait for a connection request */
connfd = accept(listenfd, (struct sockaddr *) &clientaddr, &clientlen);
if (connfd < 0)
error("ERROR on accept");
/* gethostbyaddr: determine who sent the message */
hostp = gethostbyaddr((const char *)&clientaddr.sin_addr.s_addr,
sizeof(clientaddr.sin_addr.s_addr), AF_INET);
if (hostp == NULL)
error("ERROR on gethostbyaddr");
hostaddrp = inet_ntoa(clientaddr.sin_addr);
if (hostaddrp == NULL)
error("ERROR on inet_ntoa\n");
printf("server established connection with %s (%s)\n",
hostp->h_name, hostaddrp);
/* read: read input string from the client */
bzero(buf, BUFSIZE);
n = read(connfd, buf, BUFSIZE);
if (n < 0)
error("ERROR reading from socket");
printf("server received %d bytes: %s", n, buf);
/* write: echo the input string back to the client */
n = write(connfd, buf, strlen(buf));
if (n < 0)
error("ERROR writing to socket");
close(connfd);
}
}

You might also like