



























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
computer betwork for forth grades
Typology: Thesis
1 / 35
This page cannot be seen from the preview
Don't miss anything!




























struct sockaddr_in { short sin_family; // e.g. AF_INET unsigned short sin_port; // e.g. htons(3490) struct in_addr sin_addr; // see struct in_addr, below char sin_zero[8]; // zero this if you want to }; struct in_addr { unsigned long s_addr; // load with inet_aton() };
struct sockaddr_in saddr; int sockfd; unsigned short port = 80; if((sockfd=socket(AF_INET, SOCK_STREAM, 0) < 0) { // from back a couple slides printf(“Error creating socket\n”); ... } *memset(&saddr, '\0', sizeof(saddr)); // zero structure out saddr.sin_family = AF_INET; // match the socket() call saddr.sin_addr.s_addr = htonl(INADDR_ANY); // bind to any local address saddr.sin_port = htons(port); // specify port to listen on if((bind(sockfd, (struct sockaddr ) &saddr, sizeof(saddr)) < 0) { // bind! printf(“Error binding\n”); ... }
struct sockaddr_in saddr, caddr; int sockfd, clen, isock; unsigned short port = 80; if((sockfd=socket(AF_INET, SOCK_STREAM, 0) < 0) { // from back a couple slides printf(“Error creating socket\n”); ... } memset(&saddr, '\0', sizeof(saddr)); // zero structure out saddr.sin_family = AF_INET; // match the socket() call saddr.sin_addr.s_addr = htonl(INADDR_ANY); // bind to any local address saddr.sin_port = htons(port); // specify port to listen on if((bind(sockfd, (struct sockaddr *) &saddr, sizeof(saddr)) < 0) { // bind! printf(“Error binding\n”); ... } if(listen(sockfd, 5) < 0) { // listen for incoming connections printf(“Error listening\n”); ... } *clen=sizeof(caddr) if((isock=accept(sockfd, (struct sockaddr ) &caddr, &clen)) < 0) { // accept one printf(“Error accepting\n”); ... }
struct hostent { char *h_name; // official hostname char **h_aliases; // vector of alternative hostnames int h_addrtype; // address type, e.g. AF_INET int h_length; // length of address in bytes, e.g. 4 for IPv char **h_addr_list; // vector of addresses char *h_addr; // first host address, synonym for h_addr_list[0] };
struct sockaddr_in saddr; struct hostent *h; int sockfd, connfd; unsigned short port = 80; if((sockfd=socket(AF_INET, SOCK_STREAM, 0) < 0) { // from back a couple slides printf(“Error creating socket\n”); ... } if((h=gethostbyname(“www.slashdot.org”)) == NULL) { // Lookup the hostname printf(“Unknown host\n”); ... } memset(&saddr, '\0', sizeof(saddr)); // zero structure out saddr.sin_family = AF_INET; // match the socket() call *memcpy((char ) &saddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length); // copy the address saddr.sin_port = htons(port); // specify port to connect to *if((connfd=connect(sockfd, (struct sockaddr ) &saddr, sizeof(saddr)) < 0) { // connect! printf(“Cannot connect\n”); ... }