The Awesome
Linux Network Programming
I have just finished some code for my school work. Dispatch code is a server, which determines jobs of workers. Dispatch divides calculation into smaller parts and serves those parts to workers (clients). As you can guess, workers do jobs, calculates prime numbers and return those values to server. There is no error correction of any kind in these codes, even server doesn’t know if a client take a job but unable return the answer. This is only a simple example for unix socket programming and distributed computing. Feel free to test, examine, change and publish.
?Download dispatch.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | #include <iostream> #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <string.h> #include <list> using namespace std; int main(int argc, char *argv[]) { long last_part=1000000; int sockfd, newsockfd, portno, n; socklen_t client_lenth; portno = 1300; char buffer[256]; struct sockaddr_in server_address, client_address; sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { cerr << "ERROR opening socket"; return -1; } bzero((char *) &server_address, sizeof(server_address)); server_address.sin_family = AF_INET; server_address.sin_addr.s_addr = INADDR_ANY; server_address.sin_port = htons(portno); if (bind(sockfd, (struct sockaddr *) &server_address, sizeof(server_address)) < 0){ cerr << "ERROR on binding"; return -1; } listen(sockfd,10); client_lenth = sizeof(client_address); long data = 0; while(1){ newsockfd = accept(sockfd, (struct sockaddr *) &client_address, &client_lenth); if (newsockfd > -1){ n = read(newsockfd,&data,sizeof(data)); if (n > -1){ if(data == 0){ //n = write(newsockfd,"%d",last_part); n = write(newsockfd, &last_part, sizeof(last_part)); if (n > -1){ last_part += 1000000; } } else if(data == -1) { n = read(newsockfd,&data,sizeof(data)); if(n > -1){ long number = data; for(long j=0; j<number; j++){ n = read(newsockfd,&data,sizeof(data)); if(n > -1){ printf("%d\t-->\t%d\n",j,data); } else { break; } } } } } } close(newsockfd); } return 0; } |
?Download worker.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | #include <iostream> #include <stdio.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <string.h> #include <netdb.h> #include <list> using namespace std; bool is_prime(long n){ if(n%2==0){ return false; } for (long i=3;i*i<=n;i+=2){ if(n%i==0){ return false; } } return true; } int main(int argc, char *argv[]) { list<long> primes; int sockfd, portno, n; portno = 1300; struct sockaddr_in server_address; struct hostent *server; char buffer[256]; sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0){ cerr << "ERROR opening socket"; return -1; } server = gethostbyname("127.0.0.1"); if (server == NULL) { cerr << "ERROR, no such host\n"; return -1; } bzero((char *) &server_address, sizeof(server_address)); server_address.sin_family = AF_INET; bcopy((char *)server->h_addr, (char *)&server_address.sin_addr.s_addr, server->h_length); server_address.sin_port = htons(portno); if (connect(sockfd,(struct sockaddr *) &server_address,sizeof(server_address)) < 0){ cerr << "ERROR connecting"; return -1; } //cout << "Please enter the message: "; bzero(buffer,256); //fgets(buffer,255,stdin); long start = 0; n = write(sockfd,&start,sizeof(start)); if (n < 0){ cerr << "ERROR writing to socket"; return -1; } //bzero(buffer,256); n = read(sockfd,&start,sizeof(start)); if (n < 0){ cerr << "ERROR reading from socket"; return -1; } close(sockfd); for(long j=start;j<start + 1000000;j+=1){ if(is_prime(j)){ printf("Last calculated prime: %d\n", j); primes.push_back(j); } } sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0){ cerr << "ERROR opening socket"; return -1; } server = gethostbyname("127.0.0.1"); if (server == NULL) { cerr << "ERROR, no such host\n"; return -1; } bzero((char *) &server_address, sizeof(server_address)); server_address.sin_family = AF_INET; bcopy((char *)server->h_addr, (char *)&server_address.sin_addr.s_addr, server->h_length); server_address.sin_port = htons(portno); if (connect(sockfd,(struct sockaddr *) &server_address,sizeof(server_address)) < 0){ cerr << "ERROR connecting"; return -1; } unsigned int number = primes.size(); long data = -1; write(sockfd,&data,sizeof(data)); data = number; write(sockfd,&data,sizeof(data)); for(unsigned int j=0; j<number; j++){ data = primes.front(); write(sockfd,&data,sizeof(data)); primes.pop_front(); } //printf("%d\n",j); close(sockfd); // printf("%s\n",buffer); return 0; } |