Advertisement
ostyleo

SumaA2NumereNeconcurenta

Oct 30th, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.25 KB | None | 0 0
  1. //---------------------------------------------- CLIENT ----------------------------------------------
  2.  
  3. // Un client trimite unui server doua numere.
  4. // Serverul va returna clientului suma celor doua numere.
  5.  
  6. #include <sys/types.h>
  7. #include <sys/socket.h>
  8. #include <stdio.h>
  9. #include <netinet/in.h>
  10. #include <netinet/ip.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include <sys/socket.h>
  14. #include <netinet/in.h>
  15. #include <arpa/inet.h>
  16. #include <stdlib.h>
  17.  
  18. int main(int argc, char* argvs[]) {
  19.   int c,l;
  20.   struct sockaddr_in server;
  21.   uint16_t a, b, r, suma = 0;
  22.   char ip[20];
  23.   strcpy(ip,argvs[1]);
  24.   int port = atoi(argvs[2]);
  25.  
  26.   //VERIFICARI-------------------
  27.   if(port<1024 || port>65535){
  28.     printf("Portul nu convine!\n");
  29.     return 1;
  30.   }
  31.   char verif[]="0123456789.";
  32.   int i=0;
  33.   for(i=0;i<strlen(ip);i++){
  34.     if(strchr(verif,ip[i])==0){
  35.       printf("Ip-ul nu convine!\n");
  36.       return 1; //eroare
  37.     }
  38.   }
  39.   //VERIFICARI------------------
  40.  
  41.   c = socket(AF_INET, SOCK_DGRAM, 0);
  42.   if (c < 0) {
  43.     printf("Eroare la crearea socketului client\n");
  44.     return 1;
  45.   }
  46.   memset(&server, 0, sizeof(server));
  47.   server.sin_port = htons(port);
  48.   server.sin_family = AF_INET;
  49.   server.sin_addr.s_addr = inet_addr(ip);
  50.  
  51.  
  52.   scanf("%hu",&a); a=htons(a);
  53.   sendto(c, &a, sizeof(a), 0, (struct sockaddr *) &server, sizeof(server));
  54.   l = sizeof(server);
  55.  
  56.   scanf("%hu",&b);b=htons(b);
  57.   sendto(c, &b, sizeof(b), 0, (struct sockaddr *) &server, sizeof(server));
  58.  
  59.   recvfrom(c, &suma, sizeof(suma), MSG_WAITALL, (struct sockaddr *) &server, &l);
  60.   suma=ntohs(suma);
  61.   printf("Suma este: %hu\n",suma);
  62.   close(c);
  63. }
  64.  
  65. //---------------------------------------------- SERVER ----------------------------------------------
  66.  
  67. // Un client trimite unui server doua numere.
  68. // Serverul va returna clientului suma celor doua numere.
  69.  
  70. #include <sys/types.h>
  71. #include <sys/socket.h>
  72. #include <stdio.h>
  73. #include <netinet/in.h>
  74. #include <netinet/ip.h>
  75. #include <string.h>
  76. #include <stdlib.h>
  77. #include <unistd.h>
  78. #include <netinet/in.h>
  79. #include <arpa/inet.h>
  80.  
  81. void deservire_client(int s,struct sockaddr_in client){
  82.  
  83. }
  84.  
  85. int main(int argc, char* argvs[]) {
  86.   int s;
  87.   struct sockaddr_in server, client;
  88.   int l;
  89.  
  90.   s = socket(AF_INET, SOCK_DGRAM, 0);
  91.   //VERIFICARE --------
  92.   int port = atoi(argvs[1]);
  93.   if(port<1024 || port>65535){
  94.         printf("Portul nu convine!\n");
  95.         return 1;
  96.     }
  97.   //VERIFICARE --------
  98.  
  99.   if (s < 0) {
  100.     printf("Eroare la crearea socketului server\n");
  101.     return 1;
  102.   }
  103.  
  104.   memset(&server, 0, sizeof(server));
  105.   server.sin_port = htons(port);
  106.   server.sin_family = AF_INET;
  107.   server.sin_addr.s_addr = INADDR_ANY;
  108.  
  109.   if (bind(s, (struct sockaddr *) &server, sizeof(server)) < 0) {
  110.     printf("Eroare la bind\n");
  111.     return 1;
  112.   }
  113.   while(1){
  114.     l = sizeof(client);
  115.     memset(&client, 0, sizeof(client));
  116.     uint16_t a,b,suma;
  117.     int l = sizeof(client);
  118.     recvfrom(s, &a, sizeof(a), MSG_WAITALL, (struct sockaddr *) &client, &l);
  119.     a=ntohs(a);
  120.     recvfrom(s, &b, sizeof(b), MSG_WAITALL, (struct sockaddr *) &client, &l);
  121.     b=ntohs(b);
  122.     suma = a + b; suma=htons(suma);
  123.  
  124.     sendto(s, &suma, sizeof(suma), 0, (struct sockaddr *) &client, sizeof(client));
  125.   }
  126.   close(s);
  127.   return 0;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement