SlideShare a Scribd company logo
1 of 13
Download to read offline
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "header.h"
#include "chrrecksum.h"
#define BCKLG 10
void sgchrrldHndlr(int s)
{
while(waitpid(-1, NULL, WNOHANG) > 0);
}
void *get_in_addr(struct sockaddr *sa)
{
if (sa->SaFamly == AF_INET) {
return &(((struct Sckadr_in*)sa)->sin_addr);
}
return &(((struct Sckadr_in6*)sa)->sin6_addr);
}
int main(int arc, chrrar**arv)
{
int prt = 69;
chrrar chrr;
while ((chrr = gtOpt(arc, arv, "p:")) != -1) {
switchrr (chrr) {
case 'p':
prt = atoi(optarg);
break;
case '?':
exit(1);
}
}
chrrar prt[10];
sprintf(prt,"%d",prt);
int sockfd, newdv; // listen on sock_fd, new connection on newdv
struct addrinfo hints, *servinfo, *p;
struct sockaddr_storage their_addr; // connector's address information
socklen_t sin_size;
struct sigaction sa;
int yes=1;
chrrar s[INET6_ADDRSTRLEN];
int rv;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if ((rv = getaddrinfo(NULL, prt, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s ", gai_strerror(rv));
return 1;
}
for(p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1) {
perror("server: socket");
continue;
}
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes,
sizeof(int)) == -1) {
perror("setsockopt");
exit(1);
}
if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
perror("server: bind");
continue;
}
break;
}
if (p == NULL) {
fprintf(stderr, "server: failed to bind ");
return 2;
}
freeaddrinfo(servinfo);
if (listen(sockfd, BCKLG) == -1) {
perror("listen");
exit(1);
}
sa.sa_handler = sgchrrldHndlr;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGchrrLD, &sa, NULL) == -1) {
perror("sigaction");
exit(1);
}
printf("server: looking for connections... ");
while(1) { accept() loop
sin_size = sizeof their_addr;
newdv = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
if (newdv == -1) {
perror("accept");
continue;
}
inet_ntop(their_addr.ss_family,
get_in_addr((struct sockaddr *)&their_addr),
s, sizeof s);
printf("server: got connection from %s ", s);
if (!fork()) {
close(sockfd);
chrrar buf[PACKET_SIZE];
chrrar databuf[PAYLOAD_SIZE];
int numbytes, ack_bytes;
header_t head;
packet_t packet;
packet_t ackr;
int getfilename = 1;
chrrar *filename = (chrrar*) "recvfile.txt";
FILE * filefd;
bzero(buf, PACKET_SIZE);
bzero(databuf, PAYLOAD_SIZE);
while(getfilename) {
if ((numbytes = recv(newdv, buf, PACKET_SIZE, 0)) == -1) {
perror("recv");
exit(1);
}
if(numbytes > 0)
{
read_header(&head, (packet_t *)buf);
read_packet((u_chrrar*) databuf, (packet_t *)buf, (u_short) (numbytes -
HEADER_SIZE));
printf("Packet '%d' received ", (int)(head.seq));
if(add_chrrecksum(numbytes, numbytes%2, (u_short *)buf) == 0) {
printf("chrrecksum OK ");
fill_hdr((int)(head.seq), 0, HEADER_SIZE, ACK, &ackr);
printf("Filename: '%s' bytes '%d' ", databuf, numbytes);
if((ack_bytes = send(newdv, &ackr, HEADER_SIZE, 0)) == -1) {
perror("send: ack");
exit(1);
} else {
printf("ACK '%d' sent ", (int)(head.seq));
getfilename = 0;
}
} else {
printf("chrrecksum failed, discarding packet... ");
}
}
}
if ( (filefd = fopen(filename, "w")) == NULL)
{
fprintf(stderr, "Could not open destination file, using stdout. ");
} else {
printf("Preparing to start writing file '%s' ", filename );
}
int recvFIN = 0;
bzero(buf, PACKET_SIZE);
bzero(databuf, PAYLOAD_SIZE);
do {
if ((numbytes = recv(newdv, buf, PACKET_SIZE, 0)) == -1) {
perror("recv");
fclose(filefd);
close(newdv);
exit(1);
}
read_header(&head, (packet_t *)buf);
if((int)head.flag == ACK)
{
read_packet((u_chrrar*) databuf, (packet_t *)buf, (u_short) (numbytes -
HEADER_SIZE));
printf("Packet '%d' received with '%d' bytes with an offset of '%d' ",
(int)(head.seq), numbytes, (int)(head.offset));
if(add_chrrecksum(numbytes, numbytes%2, (u_short *)buf) == 0) {
printf("chrrecksum OK ");
fill_hdr((int)(head.seq), 0, HEADER_SIZE, ACK, &ackr);
if((ack_bytes = send(newdv, &ackr, HEADER_SIZE, 0)) == -1) {
perror("send: ack");
exit(1);
} else {
printf("ACK '%d' sent ", (int)(head.seq));
fwrite(databuf,1 , (int)(head.offset) ,filefd );
}
} else {
printf("chrreck sum wrong. ");
}
} else if ((int)head.flag == FIN) {
fill_hdr((int)(head.seq), 0, HEADER_SIZE, FIN, &ackr);
if((ack_bytes = send(newdv, &ackr, HEADER_SIZE, 0)) == -1) {
perror("send: ack");
exit(1);
} else {
printf("FIN '%d' sent ", (int)(head.seq));
recvFIN = 1;
}
}
bzero(buf, PACKET_SIZE);
bzero(databuf, PAYLOAD_SIZE);
} while (!recvFIN );
fclose(filefd);
close(newdv);
exit(0);
}
close(newdv);
}
return 0;
}
Solution
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "header.h"
#include "chrrecksum.h"
#define BCKLG 10
void sgchrrldHndlr(int s)
{
while(waitpid(-1, NULL, WNOHANG) > 0);
}
void *get_in_addr(struct sockaddr *sa)
{
if (sa->SaFamly == AF_INET) {
return &(((struct Sckadr_in*)sa)->sin_addr);
}
return &(((struct Sckadr_in6*)sa)->sin6_addr);
}
int main(int arc, chrrar**arv)
{
int prt = 69;
chrrar chrr;
while ((chrr = gtOpt(arc, arv, "p:")) != -1) {
switchrr (chrr) {
case 'p':
prt = atoi(optarg);
break;
case '?':
exit(1);
}
}
chrrar prt[10];
sprintf(prt,"%d",prt);
int sockfd, newdv; // listen on sock_fd, new connection on newdv
struct addrinfo hints, *servinfo, *p;
struct sockaddr_storage their_addr; // connector's address information
socklen_t sin_size;
struct sigaction sa;
int yes=1;
chrrar s[INET6_ADDRSTRLEN];
int rv;
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if ((rv = getaddrinfo(NULL, prt, &hints, &servinfo)) != 0) {
fprintf(stderr, "getaddrinfo: %s ", gai_strerror(rv));
return 1;
}
for(p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype,
p->ai_protocol)) == -1) {
perror("server: socket");
continue;
}
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes,
sizeof(int)) == -1) {
perror("setsockopt");
exit(1);
}
if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
perror("server: bind");
continue;
}
break;
}
if (p == NULL) {
fprintf(stderr, "server: failed to bind ");
return 2;
}
freeaddrinfo(servinfo);
if (listen(sockfd, BCKLG) == -1) {
perror("listen");
exit(1);
}
sa.sa_handler = sgchrrldHndlr;
sigemptyset(&sa.sa_mask);
sa.sa_flags = SA_RESTART;
if (sigaction(SIGchrrLD, &sa, NULL) == -1) {
perror("sigaction");
exit(1);
}
printf("server: looking for connections... ");
while(1) { accept() loop
sin_size = sizeof their_addr;
newdv = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size);
if (newdv == -1) {
perror("accept");
continue;
}
inet_ntop(their_addr.ss_family,
get_in_addr((struct sockaddr *)&their_addr),
s, sizeof s);
printf("server: got connection from %s ", s);
if (!fork()) {
close(sockfd);
chrrar buf[PACKET_SIZE];
chrrar databuf[PAYLOAD_SIZE];
int numbytes, ack_bytes;
header_t head;
packet_t packet;
packet_t ackr;
int getfilename = 1;
chrrar *filename = (chrrar*) "recvfile.txt";
FILE * filefd;
bzero(buf, PACKET_SIZE);
bzero(databuf, PAYLOAD_SIZE);
while(getfilename) {
if ((numbytes = recv(newdv, buf, PACKET_SIZE, 0)) == -1) {
perror("recv");
exit(1);
}
if(numbytes > 0)
{
read_header(&head, (packet_t *)buf);
read_packet((u_chrrar*) databuf, (packet_t *)buf, (u_short) (numbytes -
HEADER_SIZE));
printf("Packet '%d' received ", (int)(head.seq));
if(add_chrrecksum(numbytes, numbytes%2, (u_short *)buf) == 0) {
printf("chrrecksum OK ");
fill_hdr((int)(head.seq), 0, HEADER_SIZE, ACK, &ackr);
printf("Filename: '%s' bytes '%d' ", databuf, numbytes);
if((ack_bytes = send(newdv, &ackr, HEADER_SIZE, 0)) == -1) {
perror("send: ack");
exit(1);
} else {
printf("ACK '%d' sent ", (int)(head.seq));
getfilename = 0;
}
} else {
printf("chrrecksum failed, discarding packet... ");
}
}
}
if ( (filefd = fopen(filename, "w")) == NULL)
{
fprintf(stderr, "Could not open destination file, using stdout. ");
} else {
printf("Preparing to start writing file '%s' ", filename );
}
int recvFIN = 0;
bzero(buf, PACKET_SIZE);
bzero(databuf, PAYLOAD_SIZE);
do {
if ((numbytes = recv(newdv, buf, PACKET_SIZE, 0)) == -1) {
perror("recv");
fclose(filefd);
close(newdv);
exit(1);
}
read_header(&head, (packet_t *)buf);
if((int)head.flag == ACK)
{
read_packet((u_chrrar*) databuf, (packet_t *)buf, (u_short) (numbytes -
HEADER_SIZE));
printf("Packet '%d' received with '%d' bytes with an offset of '%d' ",
(int)(head.seq), numbytes, (int)(head.offset));
if(add_chrrecksum(numbytes, numbytes%2, (u_short *)buf) == 0) {
printf("chrrecksum OK ");
fill_hdr((int)(head.seq), 0, HEADER_SIZE, ACK, &ackr);
if((ack_bytes = send(newdv, &ackr, HEADER_SIZE, 0)) == -1) {
perror("send: ack");
exit(1);
} else {
printf("ACK '%d' sent ", (int)(head.seq));
fwrite(databuf,1 , (int)(head.offset) ,filefd );
}
} else {
printf("chrreck sum wrong. ");
}
} else if ((int)head.flag == FIN) {
fill_hdr((int)(head.seq), 0, HEADER_SIZE, FIN, &ackr);
if((ack_bytes = send(newdv, &ackr, HEADER_SIZE, 0)) == -1) {
perror("send: ack");
exit(1);
} else {
printf("FIN '%d' sent ", (int)(head.seq));
recvFIN = 1;
}
}
bzero(buf, PACKET_SIZE);
bzero(databuf, PAYLOAD_SIZE);
} while (!recvFIN );
fclose(filefd);
close(newdv);
exit(0);
}
close(newdv);
}
return 0;
}

More Related Content

Similar to #include stdio.h #include stdlib.h #include unistd.h #in.pdf

Grand centraldispatch
Grand centraldispatchGrand centraldispatch
Grand centraldispatchYuumi Yoshida
 
Microsoft Word Hw#1
Microsoft Word   Hw#1Microsoft Word   Hw#1
Microsoft Word Hw#1kkkseld
 
Aodv routing protocol code in ns2
Aodv routing protocol code in ns2Aodv routing protocol code in ns2
Aodv routing protocol code in ns2Prof Ansari
 
This is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdfThis is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdfkostikjaylonshaewe47
 
Assignment7.pdf
Assignment7.pdfAssignment7.pdf
Assignment7.pdfdash41
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File Rahul Chugh
 
C programming.   For this code I only need to add a function so th.pdf
C programming.   For this code I only need to add a function so th.pdfC programming.   For this code I only need to add a function so th.pdf
C programming.   For this code I only need to add a function so th.pdfbadshetoms
 
Implementing Software Machines in C and Go
Implementing Software Machines in C and GoImplementing Software Machines in C and Go
Implementing Software Machines in C and GoEleanor McHugh
 
tp socket en C.pdf
tp socket en C.pdftp socket en C.pdf
tp socket en C.pdfYoussefJamma
 
#include stdio.h#include stdlib.h#include string.h#inclu.pdf
#include stdio.h#include stdlib.h#include string.h#inclu.pdf#include stdio.h#include stdlib.h#include string.h#inclu.pdf
#include stdio.h#include stdlib.h#include string.h#inclu.pdfapleather
 
IO redirection in C shellPlease implement input output redirect.pdf
IO redirection in C shellPlease implement input  output redirect.pdfIO redirection in C shellPlease implement input  output redirect.pdf
IO redirection in C shellPlease implement input output redirect.pdfforecastfashions
 
array, function, pointer, pattern matching
array, function, pointer, pattern matchingarray, function, pointer, pattern matching
array, function, pointer, pattern matchingShakila Mahjabin
 
Dns server clients (actual program)
Dns server clients (actual program)Dns server clients (actual program)
Dns server clients (actual program)Youssef Dirani
 
Dns server clients (actual program)
Dns server clients (actual program)Dns server clients (actual program)
Dns server clients (actual program)Youssef Dirani
 

Similar to #include stdio.h #include stdlib.h #include unistd.h #in.pdf (20)

Grand centraldispatch
Grand centraldispatchGrand centraldispatch
Grand centraldispatch
 
Microsoft Word Hw#1
Microsoft Word   Hw#1Microsoft Word   Hw#1
Microsoft Word Hw#1
 
Aodv routing protocol code in ns2
Aodv routing protocol code in ns2Aodv routing protocol code in ns2
Aodv routing protocol code in ns2
 
Jamming attack in wireless network
Jamming attack in wireless networkJamming attack in wireless network
Jamming attack in wireless network
 
Avl tree
Avl treeAvl tree
Avl tree
 
Blocks+gcd入門
Blocks+gcd入門Blocks+gcd入門
Blocks+gcd入門
 
This is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdfThis is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdf
 
Assignment7.pdf
Assignment7.pdfAssignment7.pdf
Assignment7.pdf
 
Data Structures Using C Practical File
Data Structures Using C Practical File Data Structures Using C Practical File
Data Structures Using C Practical File
 
Procesos
ProcesosProcesos
Procesos
 
C programming.   For this code I only need to add a function so th.pdf
C programming.   For this code I only need to add a function so th.pdfC programming.   For this code I only need to add a function so th.pdf
C programming.   For this code I only need to add a function so th.pdf
 
Implementing Software Machines in C and Go
Implementing Software Machines in C and GoImplementing Software Machines in C and Go
Implementing Software Machines in C and Go
 
tp socket en C.pdf
tp socket en C.pdftp socket en C.pdf
tp socket en C.pdf
 
#include stdio.h#include stdlib.h#include string.h#inclu.pdf
#include stdio.h#include stdlib.h#include string.h#inclu.pdf#include stdio.h#include stdlib.h#include string.h#inclu.pdf
#include stdio.h#include stdlib.h#include string.h#inclu.pdf
 
Lab Question
Lab QuestionLab Question
Lab Question
 
IO redirection in C shellPlease implement input output redirect.pdf
IO redirection in C shellPlease implement input  output redirect.pdfIO redirection in C shellPlease implement input  output redirect.pdf
IO redirection in C shellPlease implement input output redirect.pdf
 
array, function, pointer, pattern matching
array, function, pointer, pattern matchingarray, function, pointer, pattern matching
array, function, pointer, pattern matching
 
Dns server clients (actual program)
Dns server clients (actual program)Dns server clients (actual program)
Dns server clients (actual program)
 
Dns server clients (actual program)
Dns server clients (actual program)Dns server clients (actual program)
Dns server clients (actual program)
 
C lab manaual
C lab manaualC lab manaual
C lab manaual
 

More from contact34

1.79VSolution1.79V.pdf
1.79VSolution1.79V.pdf1.79VSolution1.79V.pdf
1.79VSolution1.79V.pdfcontact34
 
1)It capture leaving on the purpose of the CA documentation it have.pdf
1)It capture leaving on the purpose of the CA documentation it have.pdf1)It capture leaving on the purpose of the CA documentation it have.pdf
1)It capture leaving on the purpose of the CA documentation it have.pdfcontact34
 
1). Na+K+ ATPase pump plays an important role in maintaining membra.pdf
1). Na+K+ ATPase pump plays an important role in maintaining membra.pdf1). Na+K+ ATPase pump plays an important role in maintaining membra.pdf
1). Na+K+ ATPase pump plays an important role in maintaining membra.pdfcontact34
 
total number of moles of the copund remains same .pdf
                     total number of moles of the copund remains same .pdf                     total number of moles of the copund remains same .pdf
total number of moles of the copund remains same .pdfcontact34
 
18O is an important precursor for the production offlourodeoxyglucose.pdf
 18O is an important precursor for the production offlourodeoxyglucose.pdf 18O is an important precursor for the production offlourodeoxyglucose.pdf
18O is an important precursor for the production offlourodeoxyglucose.pdfcontact34
 
observed expected O - E (O - E) Solution observed expect.pdf
    observed expected O - E (O - E)    Solution    observed expect.pdf    observed expected O - E (O - E)    Solution    observed expect.pdf
observed expected O - E (O - E) Solution observed expect.pdfcontact34
 
no hydrogen atom is present because its pure carb.pdf
                     no hydrogen atom is present because its pure carb.pdf                     no hydrogen atom is present because its pure carb.pdf
no hydrogen atom is present because its pure carb.pdfcontact34
 
Mg and NaH react with water to give H2. Mg + 2H2.pdf
                     Mg and NaH react with water to give H2.  Mg + 2H2.pdf                     Mg and NaH react with water to give H2.  Mg + 2H2.pdf
Mg and NaH react with water to give H2. Mg + 2H2.pdfcontact34
 
in case of A and D there will be resonance betwee.pdf
                     in case of A and D there will be resonance betwee.pdf                     in case of A and D there will be resonance betwee.pdf
in case of A and D there will be resonance betwee.pdfcontact34
 
For half life to be independent of Concentration .pdf
                     For half life to be independent of Concentration .pdf                     For half life to be independent of Concentration .pdf
For half life to be independent of Concentration .pdfcontact34
 
Mode because mode gives the number which occurs maximum number of .pdf
Mode because mode gives the number which occurs maximum number of .pdfMode because mode gives the number which occurs maximum number of .pdf
Mode because mode gives the number which occurs maximum number of .pdfcontact34
 
Need more clear picturesSolutionNeed more clear pictures.pdf
Need more clear picturesSolutionNeed more clear pictures.pdfNeed more clear picturesSolutionNeed more clear pictures.pdf
Need more clear picturesSolutionNeed more clear pictures.pdfcontact34
 
    public class RectangleDemo {     private class Rectangle {  .pdf
    public class RectangleDemo {     private class Rectangle {  .pdf    public class RectangleDemo {     private class Rectangle {  .pdf
    public class RectangleDemo {     private class Rectangle {  .pdfcontact34
 
Total assets=Total liabilities+Total equityHence Total assets=(797.pdf
Total assets=Total liabilities+Total equityHence Total assets=(797.pdfTotal assets=Total liabilities+Total equityHence Total assets=(797.pdf
Total assets=Total liabilities+Total equityHence Total assets=(797.pdfcontact34
 
The products would be sodium chloride, water and carbon dioxide, and.pdf
The products would be sodium chloride, water and carbon dioxide, and.pdfThe products would be sodium chloride, water and carbon dioxide, and.pdf
The products would be sodium chloride, water and carbon dioxide, and.pdfcontact34
 
The answer isc. SrCO3Sr is in Group II of the periodic table and.pdf
The answer isc. SrCO3Sr is in Group II of the periodic table and.pdfThe answer isc. SrCO3Sr is in Group II of the periodic table and.pdf
The answer isc. SrCO3Sr is in Group II of the periodic table and.pdfcontact34
 
Technologies would you use to assist in migrationSolutionTechn.pdf
Technologies would you use to assist in migrationSolutionTechn.pdfTechnologies would you use to assist in migrationSolutionTechn.pdf
Technologies would you use to assist in migrationSolutionTechn.pdfcontact34
 
AsSolution As.pdf
                     AsSolution                     As.pdf                     AsSolution                     As.pdf
AsSolution As.pdfcontact34
 
Role of biotechnology in bioterrorismBiotechnology can be employe.pdf
Role of biotechnology in bioterrorismBiotechnology can be employe.pdfRole of biotechnology in bioterrorismBiotechnology can be employe.pdf
Role of biotechnology in bioterrorismBiotechnology can be employe.pdfcontact34
 
pure water dissociates with a reversible reaction forming H+ and OH-.pdf
pure water dissociates with a reversible reaction forming H+ and OH-.pdfpure water dissociates with a reversible reaction forming H+ and OH-.pdf
pure water dissociates with a reversible reaction forming H+ and OH-.pdfcontact34
 

More from contact34 (20)

1.79VSolution1.79V.pdf
1.79VSolution1.79V.pdf1.79VSolution1.79V.pdf
1.79VSolution1.79V.pdf
 
1)It capture leaving on the purpose of the CA documentation it have.pdf
1)It capture leaving on the purpose of the CA documentation it have.pdf1)It capture leaving on the purpose of the CA documentation it have.pdf
1)It capture leaving on the purpose of the CA documentation it have.pdf
 
1). Na+K+ ATPase pump plays an important role in maintaining membra.pdf
1). Na+K+ ATPase pump plays an important role in maintaining membra.pdf1). Na+K+ ATPase pump plays an important role in maintaining membra.pdf
1). Na+K+ ATPase pump plays an important role in maintaining membra.pdf
 
total number of moles of the copund remains same .pdf
                     total number of moles of the copund remains same .pdf                     total number of moles of the copund remains same .pdf
total number of moles of the copund remains same .pdf
 
18O is an important precursor for the production offlourodeoxyglucose.pdf
 18O is an important precursor for the production offlourodeoxyglucose.pdf 18O is an important precursor for the production offlourodeoxyglucose.pdf
18O is an important precursor for the production offlourodeoxyglucose.pdf
 
observed expected O - E (O - E) Solution observed expect.pdf
    observed expected O - E (O - E)    Solution    observed expect.pdf    observed expected O - E (O - E)    Solution    observed expect.pdf
observed expected O - E (O - E) Solution observed expect.pdf
 
no hydrogen atom is present because its pure carb.pdf
                     no hydrogen atom is present because its pure carb.pdf                     no hydrogen atom is present because its pure carb.pdf
no hydrogen atom is present because its pure carb.pdf
 
Mg and NaH react with water to give H2. Mg + 2H2.pdf
                     Mg and NaH react with water to give H2.  Mg + 2H2.pdf                     Mg and NaH react with water to give H2.  Mg + 2H2.pdf
Mg and NaH react with water to give H2. Mg + 2H2.pdf
 
in case of A and D there will be resonance betwee.pdf
                     in case of A and D there will be resonance betwee.pdf                     in case of A and D there will be resonance betwee.pdf
in case of A and D there will be resonance betwee.pdf
 
For half life to be independent of Concentration .pdf
                     For half life to be independent of Concentration .pdf                     For half life to be independent of Concentration .pdf
For half life to be independent of Concentration .pdf
 
Mode because mode gives the number which occurs maximum number of .pdf
Mode because mode gives the number which occurs maximum number of .pdfMode because mode gives the number which occurs maximum number of .pdf
Mode because mode gives the number which occurs maximum number of .pdf
 
Need more clear picturesSolutionNeed more clear pictures.pdf
Need more clear picturesSolutionNeed more clear pictures.pdfNeed more clear picturesSolutionNeed more clear pictures.pdf
Need more clear picturesSolutionNeed more clear pictures.pdf
 
    public class RectangleDemo {     private class Rectangle {  .pdf
    public class RectangleDemo {     private class Rectangle {  .pdf    public class RectangleDemo {     private class Rectangle {  .pdf
    public class RectangleDemo {     private class Rectangle {  .pdf
 
Total assets=Total liabilities+Total equityHence Total assets=(797.pdf
Total assets=Total liabilities+Total equityHence Total assets=(797.pdfTotal assets=Total liabilities+Total equityHence Total assets=(797.pdf
Total assets=Total liabilities+Total equityHence Total assets=(797.pdf
 
The products would be sodium chloride, water and carbon dioxide, and.pdf
The products would be sodium chloride, water and carbon dioxide, and.pdfThe products would be sodium chloride, water and carbon dioxide, and.pdf
The products would be sodium chloride, water and carbon dioxide, and.pdf
 
The answer isc. SrCO3Sr is in Group II of the periodic table and.pdf
The answer isc. SrCO3Sr is in Group II of the periodic table and.pdfThe answer isc. SrCO3Sr is in Group II of the periodic table and.pdf
The answer isc. SrCO3Sr is in Group II of the periodic table and.pdf
 
Technologies would you use to assist in migrationSolutionTechn.pdf
Technologies would you use to assist in migrationSolutionTechn.pdfTechnologies would you use to assist in migrationSolutionTechn.pdf
Technologies would you use to assist in migrationSolutionTechn.pdf
 
AsSolution As.pdf
                     AsSolution                     As.pdf                     AsSolution                     As.pdf
AsSolution As.pdf
 
Role of biotechnology in bioterrorismBiotechnology can be employe.pdf
Role of biotechnology in bioterrorismBiotechnology can be employe.pdfRole of biotechnology in bioterrorismBiotechnology can be employe.pdf
Role of biotechnology in bioterrorismBiotechnology can be employe.pdf
 
pure water dissociates with a reversible reaction forming H+ and OH-.pdf
pure water dissociates with a reversible reaction forming H+ and OH-.pdfpure water dissociates with a reversible reaction forming H+ and OH-.pdf
pure water dissociates with a reversible reaction forming H+ and OH-.pdf
 

Recently uploaded

Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 

Recently uploaded (20)

Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 

#include stdio.h #include stdlib.h #include unistd.h #in.pdf

  • 1. #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "header.h" #include "chrrecksum.h" #define BCKLG 10 void sgchrrldHndlr(int s) { while(waitpid(-1, NULL, WNOHANG) > 0); } void *get_in_addr(struct sockaddr *sa) { if (sa->SaFamly == AF_INET) { return &(((struct Sckadr_in*)sa)->sin_addr); } return &(((struct Sckadr_in6*)sa)->sin6_addr); } int main(int arc, chrrar**arv) { int prt = 69; chrrar chrr;
  • 2. while ((chrr = gtOpt(arc, arv, "p:")) != -1) { switchrr (chrr) { case 'p': prt = atoi(optarg); break; case '?': exit(1); } } chrrar prt[10]; sprintf(prt,"%d",prt); int sockfd, newdv; // listen on sock_fd, new connection on newdv struct addrinfo hints, *servinfo, *p; struct sockaddr_storage their_addr; // connector's address information socklen_t sin_size; struct sigaction sa; int yes=1; chrrar s[INET6_ADDRSTRLEN]; int rv; memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; if ((rv = getaddrinfo(NULL, prt, &hints, &servinfo)) != 0) { fprintf(stderr, "getaddrinfo: %s ", gai_strerror(rv)); return 1; } for(p = servinfo; p != NULL; p = p->ai_next) { if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { perror("server: socket"); continue; }
  • 3. if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) { perror("setsockopt"); exit(1); } if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) { close(sockfd); perror("server: bind"); continue; } break; } if (p == NULL) { fprintf(stderr, "server: failed to bind "); return 2; } freeaddrinfo(servinfo); if (listen(sockfd, BCKLG) == -1) { perror("listen"); exit(1); } sa.sa_handler = sgchrrldHndlr; sigemptyset(&sa.sa_mask); sa.sa_flags = SA_RESTART; if (sigaction(SIGchrrLD, &sa, NULL) == -1) { perror("sigaction"); exit(1); } printf("server: looking for connections... "); while(1) { accept() loop sin_size = sizeof their_addr; newdv = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size); if (newdv == -1) { perror("accept"); continue; }
  • 4. inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr), s, sizeof s); printf("server: got connection from %s ", s); if (!fork()) { close(sockfd); chrrar buf[PACKET_SIZE]; chrrar databuf[PAYLOAD_SIZE]; int numbytes, ack_bytes; header_t head; packet_t packet; packet_t ackr; int getfilename = 1; chrrar *filename = (chrrar*) "recvfile.txt"; FILE * filefd; bzero(buf, PACKET_SIZE); bzero(databuf, PAYLOAD_SIZE); while(getfilename) { if ((numbytes = recv(newdv, buf, PACKET_SIZE, 0)) == -1) { perror("recv"); exit(1); } if(numbytes > 0) { read_header(&head, (packet_t *)buf); read_packet((u_chrrar*) databuf, (packet_t *)buf, (u_short) (numbytes - HEADER_SIZE)); printf("Packet '%d' received ", (int)(head.seq)); if(add_chrrecksum(numbytes, numbytes%2, (u_short *)buf) == 0) { printf("chrrecksum OK "); fill_hdr((int)(head.seq), 0, HEADER_SIZE, ACK, &ackr);
  • 5. printf("Filename: '%s' bytes '%d' ", databuf, numbytes); if((ack_bytes = send(newdv, &ackr, HEADER_SIZE, 0)) == -1) { perror("send: ack"); exit(1); } else { printf("ACK '%d' sent ", (int)(head.seq)); getfilename = 0; } } else { printf("chrrecksum failed, discarding packet... "); } } } if ( (filefd = fopen(filename, "w")) == NULL) { fprintf(stderr, "Could not open destination file, using stdout. "); } else { printf("Preparing to start writing file '%s' ", filename ); } int recvFIN = 0; bzero(buf, PACKET_SIZE); bzero(databuf, PAYLOAD_SIZE); do { if ((numbytes = recv(newdv, buf, PACKET_SIZE, 0)) == -1) { perror("recv"); fclose(filefd); close(newdv); exit(1); } read_header(&head, (packet_t *)buf); if((int)head.flag == ACK)
  • 6. { read_packet((u_chrrar*) databuf, (packet_t *)buf, (u_short) (numbytes - HEADER_SIZE)); printf("Packet '%d' received with '%d' bytes with an offset of '%d' ", (int)(head.seq), numbytes, (int)(head.offset)); if(add_chrrecksum(numbytes, numbytes%2, (u_short *)buf) == 0) { printf("chrrecksum OK "); fill_hdr((int)(head.seq), 0, HEADER_SIZE, ACK, &ackr); if((ack_bytes = send(newdv, &ackr, HEADER_SIZE, 0)) == -1) { perror("send: ack"); exit(1); } else { printf("ACK '%d' sent ", (int)(head.seq)); fwrite(databuf,1 , (int)(head.offset) ,filefd ); } } else { printf("chrreck sum wrong. "); } } else if ((int)head.flag == FIN) { fill_hdr((int)(head.seq), 0, HEADER_SIZE, FIN, &ackr); if((ack_bytes = send(newdv, &ackr, HEADER_SIZE, 0)) == -1) { perror("send: ack"); exit(1); } else { printf("FIN '%d' sent ", (int)(head.seq)); recvFIN = 1; } } bzero(buf, PACKET_SIZE); bzero(databuf, PAYLOAD_SIZE);
  • 7. } while (!recvFIN ); fclose(filefd); close(newdv); exit(0); } close(newdv); } return 0; } Solution #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "header.h" #include "chrrecksum.h" #define BCKLG 10 void sgchrrldHndlr(int s) { while(waitpid(-1, NULL, WNOHANG) > 0);
  • 8. } void *get_in_addr(struct sockaddr *sa) { if (sa->SaFamly == AF_INET) { return &(((struct Sckadr_in*)sa)->sin_addr); } return &(((struct Sckadr_in6*)sa)->sin6_addr); } int main(int arc, chrrar**arv) { int prt = 69; chrrar chrr; while ((chrr = gtOpt(arc, arv, "p:")) != -1) { switchrr (chrr) { case 'p': prt = atoi(optarg); break; case '?': exit(1); } } chrrar prt[10]; sprintf(prt,"%d",prt); int sockfd, newdv; // listen on sock_fd, new connection on newdv struct addrinfo hints, *servinfo, *p; struct sockaddr_storage their_addr; // connector's address information socklen_t sin_size; struct sigaction sa; int yes=1; chrrar s[INET6_ADDRSTRLEN]; int rv; memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM;
  • 9. hints.ai_flags = AI_PASSIVE; if ((rv = getaddrinfo(NULL, prt, &hints, &servinfo)) != 0) { fprintf(stderr, "getaddrinfo: %s ", gai_strerror(rv)); return 1; } for(p = servinfo; p != NULL; p = p->ai_next) { if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) { perror("server: socket"); continue; } if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1) { perror("setsockopt"); exit(1); } if (bind(sockfd, p->ai_addr, p->ai_addrlen) == -1) { close(sockfd); perror("server: bind"); continue; } break; } if (p == NULL) { fprintf(stderr, "server: failed to bind "); return 2; } freeaddrinfo(servinfo); if (listen(sockfd, BCKLG) == -1) { perror("listen"); exit(1); } sa.sa_handler = sgchrrldHndlr; sigemptyset(&sa.sa_mask); sa.sa_flags = SA_RESTART;
  • 10. if (sigaction(SIGchrrLD, &sa, NULL) == -1) { perror("sigaction"); exit(1); } printf("server: looking for connections... "); while(1) { accept() loop sin_size = sizeof their_addr; newdv = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size); if (newdv == -1) { perror("accept"); continue; } inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr), s, sizeof s); printf("server: got connection from %s ", s); if (!fork()) { close(sockfd); chrrar buf[PACKET_SIZE]; chrrar databuf[PAYLOAD_SIZE]; int numbytes, ack_bytes; header_t head; packet_t packet; packet_t ackr; int getfilename = 1; chrrar *filename = (chrrar*) "recvfile.txt"; FILE * filefd; bzero(buf, PACKET_SIZE); bzero(databuf, PAYLOAD_SIZE); while(getfilename) { if ((numbytes = recv(newdv, buf, PACKET_SIZE, 0)) == -1) { perror("recv");
  • 11. exit(1); } if(numbytes > 0) { read_header(&head, (packet_t *)buf); read_packet((u_chrrar*) databuf, (packet_t *)buf, (u_short) (numbytes - HEADER_SIZE)); printf("Packet '%d' received ", (int)(head.seq)); if(add_chrrecksum(numbytes, numbytes%2, (u_short *)buf) == 0) { printf("chrrecksum OK "); fill_hdr((int)(head.seq), 0, HEADER_SIZE, ACK, &ackr); printf("Filename: '%s' bytes '%d' ", databuf, numbytes); if((ack_bytes = send(newdv, &ackr, HEADER_SIZE, 0)) == -1) { perror("send: ack"); exit(1); } else { printf("ACK '%d' sent ", (int)(head.seq)); getfilename = 0; } } else { printf("chrrecksum failed, discarding packet... "); } } } if ( (filefd = fopen(filename, "w")) == NULL) { fprintf(stderr, "Could not open destination file, using stdout. "); } else { printf("Preparing to start writing file '%s' ", filename ); } int recvFIN = 0;
  • 12. bzero(buf, PACKET_SIZE); bzero(databuf, PAYLOAD_SIZE); do { if ((numbytes = recv(newdv, buf, PACKET_SIZE, 0)) == -1) { perror("recv"); fclose(filefd); close(newdv); exit(1); } read_header(&head, (packet_t *)buf); if((int)head.flag == ACK) { read_packet((u_chrrar*) databuf, (packet_t *)buf, (u_short) (numbytes - HEADER_SIZE)); printf("Packet '%d' received with '%d' bytes with an offset of '%d' ", (int)(head.seq), numbytes, (int)(head.offset)); if(add_chrrecksum(numbytes, numbytes%2, (u_short *)buf) == 0) { printf("chrrecksum OK "); fill_hdr((int)(head.seq), 0, HEADER_SIZE, ACK, &ackr); if((ack_bytes = send(newdv, &ackr, HEADER_SIZE, 0)) == -1) { perror("send: ack"); exit(1); } else { printf("ACK '%d' sent ", (int)(head.seq)); fwrite(databuf,1 , (int)(head.offset) ,filefd ); } } else { printf("chrreck sum wrong. "); } } else if ((int)head.flag == FIN) {
  • 13. fill_hdr((int)(head.seq), 0, HEADER_SIZE, FIN, &ackr); if((ack_bytes = send(newdv, &ackr, HEADER_SIZE, 0)) == -1) { perror("send: ack"); exit(1); } else { printf("FIN '%d' sent ", (int)(head.seq)); recvFIN = 1; } } bzero(buf, PACKET_SIZE); bzero(databuf, PAYLOAD_SIZE); } while (!recvFIN ); fclose(filefd); close(newdv); exit(0); } close(newdv); } return 0; }