ΗΜΥ 316 - Ε α η ια ή Ά η η 5 Υ ο οίη η Π ω ο ό ο α η αι α α ο ή sockets Ά η η 1 (30%): Sockets π α α α α α π πα π α α ω sockets Unix/Linux. Γ α α α π π α π α Server α Client π π π α έ Α π, α α απ π ω. socket () bind () listen () accept () connect () read () write () close () Ά η η 2 (70%): Π ω ό ο ο αύ η αι α α ο ή π α α α π π ω πα α α α (Stop and wait protocol) α π α α C π α sockets. Θα α α π α απ α (Transmitter) α πα α π (Receiver) π α π ω ω α π α α ( α α α α α α )έ α, απ α α α α α α απ π α α απ ( α α α α ) πα α π α α έ πα α π α π ω α α (π π α απ π π π α α ) απ α (Socket π Client non blocking mode) α α α (channel) port 4610 (server port = 4610) α α α α πα α π (Socket π Server - non blocking mode) port 4613 (Receiver port = 4613). απ α α α α α α ω απ π α α απ α, α α α α έ π α απ α α β α α ω έ π α α α α α sequence number (0 1) α α α αέ Γ α πα α α α α απ π α α format pkt0=0,ch or pkt1=1,ch π ch α α α α έ
α π α α απ frame α ω α ( α α π CRC). Η α π π α π α 1,β π αέ πα α π α π α π ω α π απ απ α α α απα α Acknowledgment (ACK). α α ακ Ο ώ ι α αι η α αφο ά ι α π Email:akyria09@ucy.ac.cy charalampos.menelaou@ucy.ac.cy π, 25 Φ α βί1ζ π αέ κ α(subject) email π π α π ECEγ1ζ_βί1ζ έ α α α π π α π α α έzip α α Assignment5_name_surname έ
Server #include <sys/types.h> #include <sys/time.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <unistd.h> #include <errno.h> #include <signal.h> #include <stdio.h> static void serve(int); #define PORT 6060 main( int argc, char **argv ) int lsd; /* Listening socket */ struct sockaddr_in sin; /* Binding struct */ int sin_size=sizeof(sin); int sd; /* Socket to accept new connexion */ /* Create listening socket */ if ( (lsd=socket(af_inet, SOCK_STREAM, 0)) < 0 ) fprintf(stderr, "%s: cannot create listening socket: ", argv[0]); sin.sin_family = AF_INET; sin.sin_port = htons(port); sin.sin_addr.s_addr = htonl(inaddr_any); if ( bind(lsd, &sin, sin_size) < 0 ) fprintf(stderr, "%s: cannot bind listening socket: ", argv[0]); /* Initiate a listen queue */ if ( listen(lsd, 5) < 0 ) fprintf(stderr, "%s: cannot listen on socket: ", argv[0]);
/* Take care of the SIGPIPE signal - ignore it */ signal(sigpipe, SIG_IGN); while ( 1 ) if ( (sd=accept(lsd, &sin, &sin_size)) < 0 ) exit(errno); serve(sd); shutdown(sd, 2); close(sd); void serve(int sd) time_t local_time; char *time_string; time(&local_time); time_string = ctime(&local_time); write(sd, time_string, strlen(time_string)); return;
Client #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #define BUFSIZE 1024 #define SERVER_PORT 6060 main( int argc, char **argv ) int sd; /* Socket descriptor */ struct sockaddr_in server; /* Server to connect */ struct hostent *server_host; /* Host info */ char buf[bufsize]; int nbytes; /* Create socket */ if ( (sd=socket(af_inet, SOCK_STREAM, 0)) < 0 ) fprintf(stderr, "%s: cannot create socket: ", argv[0]); /* Get info on host */ if ( (server_host=gethostbyname(argv[1])) == NULL ) fprintf(stderr, "%s: unknown host %s\n", argv[0], argv[1]); /* Set up struct sockaddr_in */ server.sin_family = AF_INET; server.sin_port = SERVER_PORT; bcopy((char*)server_host->h_addr, (char*)&server.sin_addr, server_host->h_length); /* Connect */ if ( connect(sd, &server, sizeof(server)) < 0 ) fprintf(stderr, "%s: cannot connect to server: ", argv[0]); /* Get date */ if ( (nbytes=read(sd, buf, BUFSIZE-1)) <= 0 ) fprintf(stderr, "%s: read failed: ", argv[0]);
buf[nbytes] = 0; printf("date on host %s is: %s\n", argv[1], buf); close(sd); exit(0);
#include <sys/types.h> #include <sys/time.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <string.h> #include <unistd.h> #include <errno.h> #include <signal.h> #include <stdio.h> #include <time.h> #include <stdlib.h> Channel #define BUFSIZE 1024 #define PORT 4610 #define SERVER_PORT 4613 double pl,pd,u; main( int argc, char **argv ) int new_sd,sd2,sd3; srand(time(null)); char Rxbuf_from_Rx[BUFSIZE],Rxbuf_from_Tx[BUFSIZE]; int parent,child,nbytes,n1bytes; int channel_delay=4000000; struct sockaddr_in server; struct hostent *server_host; /* Create socket */ if ( (sd2=socket(af_inet, SOCK_STREAM, 0)) < 0 ) fprintf(stderr, "%s: cannot create socket: ", argv[0]); /* Get info on host */ if ( (server_host=gethostbyname(argv[1])) == NULL ) fprintf(stderr, "%s: unknown host %s\n", argv[0], argv[1]); /* Set up struct sockaddr_in */ server.sin_family = AF_INET; server.sin_port = htons(server_port); bcopy((char*)server_host->h_addr, (char*)&server.sin_addr, server_host->h_length); connect(sd2,(struct sockaddr *)&server, sizeof(server)); struct sockaddr_in in_sa; int in_sa_size = sizeof(in_sa); in_sa.sin_family = AF_INET; in_sa.sin_port = htons(port); in_sa.sin_addr.s_addr = htonl(inaddr_any); if ((new_sd =socket(af_inet,sock_stream,0)) < 0) fprintf(stderr,"%s: cannot create listening socket: ",argv[0]); if(bind (new_sd,(struct sockaddr *)&in_sa,in_sa_size)<0) fprintf(stderr,"%s: cannot bind listening socket: ",argv[0]);
// Initialize a listen queque if(listen(new_sd,5)<0) fprintf(stderr, "%s: cannot listen on socket: ",argv[0]); socklen_t len = sizeof(in_sa); if((sd3=accept(new_sd,(struct sockaddr *)&in_sa,&len))<0) signal(sigpipe,sig_ign); printf("\ngive Channel Probability for Loss:"); scanf("%lf",&pl); printf("\ngive Channel Probability for Delay:"); scanf("%lf",&pd); if(fork()==0 ) child=getpid(); while((n1bytes=read(sd2,rxbuf_from_rx,bufsize))>0) Rxbuf_from_Rx[n1bytes] = '\0'; printf("channel Recieved Ack %c from Receiver \n\n", Rxbuf_from_Rx[0]); write(sd3,rxbuf_from_rx,n1bytes); else parent=getppid(); while((nbytes=read(sd3,rxbuf_from_tx,bufsize))>0) Rxbuf_from_Tx[2] = '\0'; printf("channel Recieved from Transmitter: %s\n", Rxbuf_from_Tx); u =1+rand()%100; u=u/100; if (pl>=u) printf("packet %c lost\n", Rxbuf_from_Tx[1]); else if ((pl<u)&&(pd>=u)) printf("packet %c delay for %d microseconds\n", Rxbuf_from_Tx[1], channel_delay); usleep(channel_delay); write(sd2, Rxbuf_from_Tx,nbytes); else if ((pl<u)&&(pd<u)) printf("packet %c send successfully\n", Rxbuf_from_Tx[1]); write(sd2, Rxbuf_from_Tx,nbytes); else printf("unexpected error\n");